svn-gvsig-desktop / tags / v10_RC2c / libraries / libCq CMS for java.old / src / org / cresques / io / GdalWriter.java @ 8745
History | View | Annotate | Download (29.5 KB)
1 | 5990 | luisw2 | /*
|
---|---|---|---|
2 | * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
||
3 | *
|
||
4 | * Copyright (C) 2004-5.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * cresques@gmail.com
|
||
23 | */
|
||
24 | package org.cresques.io; |
||
25 | |||
26 | import java.io.BufferedOutputStream; |
||
27 | import java.io.DataOutputStream; |
||
28 | import java.io.File; |
||
29 | import java.io.FileOutputStream; |
||
30 | import java.io.IOException; |
||
31 | |||
32 | import org.cresques.cts.IProjection; |
||
33 | import org.cresques.geo.ViewPortData; |
||
34 | 7295 | cesar | import org.cresques.i18n.Messages; |
35 | 5991 | nacho | import org.cresques.io.data.WriterSupportOptions; |
36 | 5990 | luisw2 | import org.cresques.px.Extent; |
37 | import org.cresques.px.PxRaster; |
||
38 | |||
39 | import es.gva.cit.jecwcompress.EcwException; |
||
40 | import es.gva.cit.jgdal.Gdal; |
||
41 | import es.gva.cit.jgdal.GdalBuffer; |
||
42 | import es.gva.cit.jgdal.GdalDriver; |
||
43 | import es.gva.cit.jgdal.GdalException; |
||
44 | import es.gva.cit.jgdal.GdalRasterBand; |
||
45 | import es.gva.cit.jgdal.GeoTransform; |
||
46 | import es.gva.cit.jogr.OGRSpatialReference; |
||
47 | |||
48 | |||
49 | /**
|
||
50 | * Driver para la escritura a trav?s de Gdal.
|
||
51 | * Puede exportar un fichero de un formato a otro desde un GeoRasterFile
|
||
52 | * en cualquier formato soportado por la lectura a un formato que este incluido
|
||
53 | * en la lista supportedDrv.
|
||
54 | *
|
||
55 | * Puede salvar a disco en un formato que este incluido en la lista supportedDrv
|
||
56 | * obteniendo los datos que van siendo servidos desde el cliente. Este cliente
|
||
57 | * debe implementar un IDataWriter o tener un objeto que lo implemente. Inicialmente
|
||
58 | * le pasar? los par?metros de la imagen de salida y cuando el driver comience a
|
||
59 | * escribir le ir? solicitando m?s a trav?s del m?todo readData de IDataWriter.
|
||
60 | * El cliente ser? el que lleve el control de lo que va sirviendo y lo que le queda
|
||
61 | * por servir.
|
||
62 | * @author Nacho Brodin (brodin_ign@gva.es)
|
||
63 | */
|
||
64 | public class GdalWriter extends GeoRasterWriter { |
||
65 | |||
66 | //Datos de registro de drivers
|
||
67 | static {
|
||
68 | GeoRasterWriter.registerWriterExtension("tif", GdalWriter.class);
|
||
69 | 6009 | nacho | //GeoRasterWriter.registerWriterExtension("tiff", GdalWriter.class);
|
70 | 5990 | luisw2 | typeList.put("tif", "GTiff"); |
71 | 6009 | nacho | //typeList.put("tiff", "GTiff");
|
72 | 5990 | luisw2 | } |
73 | |||
74 | public final int windowSizeX = 386; |
||
75 | public final int windowSizeY = 195; |
||
76 | public final int panelSizeX = 358; |
||
77 | public final int panelSizeY = 105; |
||
78 | public final String panelLayout = "BorderLayout"; |
||
79 | private GdalDriver drv;
|
||
80 | private Gdal dset_destino = null; |
||
81 | private GdalRasterBand rband = null; |
||
82 | private GeoTransform geot = null; //Datos de georeferenciaci?n |
||
83 | private OGRSpatialReference oSRS; //Datos de proyecci?n |
||
84 | private GdalBuffer buf = null; //Buffer de origen de gdal |
||
85 | private GdalBuffer bufBandR = null; |
||
86 | private GdalBuffer bufBandG = null; |
||
87 | private GdalBuffer bufBandB = null; |
||
88 | private int nBlocks = 0; //N?mero de bloques en Y en el que se divide la imagen para escribir |
||
89 | private int anchoResto = 0; //Tama?o del ?ltimo bloque de la imagen. |
||
90 | private String[] params = null; //Par?metros de creaci?n del dataset. |
||
91 | private GdalSupportOptions support = null; |
||
92 | private boolean consulta = false; |
||
93 | private boolean write = true; //Cuando est? a true se puede escribir en la imagen de salida. Si est? a false el proceso es interrumpido |
||
94 | |||
95 | /**
|
||
96 | * Constructor para la obtenci?n de par?metros del driver
|
||
97 | * @param drvType Tipo de driver
|
||
98 | */
|
||
99 | public GdalWriter(String fileName) { |
||
100 | ident = fileName.toLowerCase().substring(fileName.lastIndexOf(".") + 1); |
||
101 | driver = (String)typeList.get(ident);
|
||
102 | support = new GdalSupportOptions(driver);
|
||
103 | 6006 | nacho | support.setBlockSize(blockSizeDefault); |
104 | 5990 | luisw2 | support.setPhotometric("RGB");
|
105 | support.setInterleave("BAND");
|
||
106 | support.setCompress("NONE");
|
||
107 | support.setWriteGeoref(true);
|
||
108 | support.setTfw(false);
|
||
109 | consulta = true;
|
||
110 | } |
||
111 | |||
112 | /**
|
||
113 | * Constructor para salvar una sola imagen completa
|
||
114 | * @param raster PxRaster de la imagen de origen
|
||
115 | * @param outfilename Nombre del fichero de salida
|
||
116 | * @param infilename Nombre del fichero de entrada
|
||
117 | * @param drvType Tipo de driver
|
||
118 | */
|
||
119 | public GdalWriter(PxRaster raster, String outFileName, String inFileName) throws GdalException, IOException { |
||
120 | ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1); |
||
121 | driver = (String)typeList.get(ident);
|
||
122 | support = new GdalSupportOptions(driver);
|
||
123 | |||
124 | this.outFileName = outFileName;
|
||
125 | this.inFileName = inFileName;
|
||
126 | currentRaster = raster; |
||
127 | |||
128 | sizeWindowX = raster.getFWidth(); |
||
129 | sizeWindowY = raster.getFHeight(); |
||
130 | |||
131 | //Obtenemos la georeferenciaci?n
|
||
132 | if (support.getGeoref()) {
|
||
133 | double maxX = currentRaster.getExtent().maxX();
|
||
134 | double minX = currentRaster.getExtent().minX();
|
||
135 | double maxY = currentRaster.getExtent().maxY();
|
||
136 | double minY = currentRaster.getExtent().minY();
|
||
137 | |||
138 | geot = new GeoTransform();
|
||
139 | geot.adfgeotransform[0] = minX;
|
||
140 | geot.adfgeotransform[3] = maxY;
|
||
141 | geot.adfgeotransform[1] = (maxX - minX) / currentRaster.getFWidth();
|
||
142 | geot.adfgeotransform[5] = (minY - maxY) / currentRaster.getFHeight();
|
||
143 | } |
||
144 | |||
145 | nBands = currentRaster.getBandCount(); |
||
146 | |||
147 | this.support.setBlockSize(64/*currentRaster.getBlockSize()*/); |
||
148 | |||
149 | if ((sizeWindowX < 0) || (sizeWindowY < 0)) { |
||
150 | throw new IOException("Tama?o del fichero de salida erroneo."); |
||
151 | } |
||
152 | |||
153 | if (nBands == 3) { |
||
154 | this.support.setPhotometric("PHOTOMETRIC=RGB"); |
||
155 | } else if (nBands == 1) { |
||
156 | this.support.setPhotometric("PHOTOMETRIC=MINISBLACK"); |
||
157 | } else {
|
||
158 | this.support.setPhotometric(""); |
||
159 | } |
||
160 | |||
161 | params = new String[2]; |
||
162 | params[0] = new String("TILED=YES"); |
||
163 | params[1] = new String(this.support.getPhotometric()); |
||
164 | |||
165 | init(); |
||
166 | |||
167 | /*oSRS = new OGRSpatialReference();
|
||
168 | try{
|
||
169 | oSRS.setUTM(currentRaster.geoFile.getUTM(), currentRaster.geoFile.getZone());
|
||
170 | oSRS.setWellKnownGeogCS(currentRaster.geoFile.getGeogCS());
|
||
171 | //System.out.println(currentRaster.geoFile.getGeogCS()+"Nueva Proyecci?n ==> "+oSRS.exportToWkt());
|
||
172 | dset_destino.setProjection(oSRS.exportToWkt());
|
||
173 | }catch(Exception e){
|
||
174 | e.printStackTrace();
|
||
175 | }*/
|
||
176 | } |
||
177 | |||
178 | /**
|
||
179 | * Constructor para salvar datos servidos por el cliente
|
||
180 | * @param dataWriter Objeto servidor de datos para el driver de escritura
|
||
181 | * @param outSizeX N?mero de pixels en X de la imagen de salida
|
||
182 | * @param outSizeY N?mero de pixels en Y de la imagen de salida
|
||
183 | * @param outFilename Fichero de salida
|
||
184 | * @param extentMaxX Posici?n en X m?xima del extent
|
||
185 | * @param extentMinX Posici?n en X m?nima del extent
|
||
186 | * @param extentMaxY Posici?n en Y m?xima del extent
|
||
187 | * @param extentMinY Posici?n en Y m?nima del extent
|
||
188 | * @param nBands N?mero de bandas
|
||
189 | * @param drvType Tipo de driver
|
||
190 | * @throws GdalException
|
||
191 | * @throws IOException
|
||
192 | */
|
||
193 | public GdalWriter( IDataWriter dataWriter,
|
||
194 | String outFileName,
|
||
195 | Integer blockSize,
|
||
196 | Integer nBands,
|
||
197 | ViewPortData vp, |
||
198 | Integer compresion,
|
||
199 | Integer outSizeX,
|
||
200 | Integer outSizeY)throws GdalException, IOException { |
||
201 | |||
202 | ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1); |
||
203 | driver = (String)typeList.get(ident);
|
||
204 | support = new GdalSupportOptions(driver);
|
||
205 | |||
206 | this.dataWriter = dataWriter;
|
||
207 | this.outFileName = outFileName;
|
||
208 | |||
209 | this.sizeWindowX = outSizeX.intValue();
|
||
210 | this.sizeWindowY = outSizeY.intValue();
|
||
211 | |||
212 | if ((sizeWindowX < 0) || (sizeWindowY < 0)) { |
||
213 | throw new IOException("Tama?o del fichero de salida erroneo."); |
||
214 | } |
||
215 | |||
216 | this.nBands = nBands.intValue();
|
||
217 | |||
218 | //Calculamos la georeferenciaci?n a partir del extend pasado por el cliente.
|
||
219 | if (support.getGeoref()) {
|
||
220 | Extent ex = vp.getExtent(); |
||
221 | double maxX = ex.maxX();
|
||
222 | double minX = ex.minX();
|
||
223 | double maxY = ex.maxY();
|
||
224 | double minY = ex.minY();
|
||
225 | |||
226 | this.support.setBlockSize(blockSize.intValue());
|
||
227 | |||
228 | geot = new GeoTransform();
|
||
229 | geot.adfgeotransform[0] = minX;
|
||
230 | geot.adfgeotransform[3] = maxY; //-((pixelSizeY * outSizeY)-minY); |
||
231 | geot.adfgeotransform[1] = (double) ((maxX - minX) / outSizeX.intValue()); //pixelSizeX; |
||
232 | geot.adfgeotransform[5] = (double) ((minY - maxY) / outSizeY.intValue()); //pixelSizeY; |
||
233 | } |
||
234 | |||
235 | setParams(); |
||
236 | |||
237 | init(); |
||
238 | } |
||
239 | |||
240 | /**
|
||
241 | *Asigna par?metros de creaci?n del dataset de Gdal
|
||
242 | */
|
||
243 | private void setParams() { |
||
244 | params = new String[5]; |
||
245 | |||
246 | params[0] = new String("TILED=YES"); |
||
247 | params[1] = new String("PHOTOMETRIC=" + this.support.getPhotometric()); |
||
248 | params[2] = new String("INTERLEAVE=" + this.support.getInterleave()); |
||
249 | params[3] = new String("COMPRESS=" + this.support.getCompress()); |
||
250 | |||
251 | String tfw = null; |
||
252 | |||
253 | if (this.support.getTfw()) { |
||
254 | tfw = new String("YES"); |
||
255 | } else {
|
||
256 | tfw = new String("NO"); |
||
257 | } |
||
258 | |||
259 | params[4] = new String("TFW=" + tfw); |
||
260 | } |
||
261 | |||
262 | /**
|
||
263 | * Asigna el tipo de driver con el que se salvar? la imagen
|
||
264 | * @param drvType Tipo de driver
|
||
265 | */
|
||
266 | public void setDriverType(String drvType) { |
||
267 | this.driver = drvType;
|
||
268 | } |
||
269 | |||
270 | /**
|
||
271 | * Creaci?n del dataset de destino.
|
||
272 | * @throws EcwException
|
||
273 | */
|
||
274 | private void init() throws GdalException { |
||
275 | //Controlamos que el tipo de driver sea correcto
|
||
276 | if (driver == null) { |
||
277 | throw new GdalException("Tipo de driver sin especificar."); |
||
278 | } |
||
279 | |||
280 | boolean okdrvtype = false; |
||
281 | |||
282 | String[] types = GeoRasterWriter.getDriversType(); |
||
283 | for (int i = 0; i < GeoRasterWriter.getNTypes(); i++) |
||
284 | if (driver.equals(types[i])) {
|
||
285 | okdrvtype = true;
|
||
286 | } |
||
287 | |||
288 | if (okdrvtype == false) { |
||
289 | throw new GdalException("El tipo de driver "+driver+" no est? soportado por GdalWriter."); |
||
290 | } |
||
291 | |||
292 | //Obtenemos el driver y creamos el dataset del destino
|
||
293 | drv = Gdal.getDriverByName(driver); |
||
294 | |||
295 | if (dset_destino != null) { |
||
296 | dset_destino.close(); |
||
297 | dset_destino = null;
|
||
298 | } |
||
299 | |||
300 | dset_destino = drv.create(outFileName, sizeWindowX, sizeWindowY, |
||
301 | nBands, Gdal.GDT_Byte, params); |
||
302 | |||
303 | if (this.support.getGeoref()) { |
||
304 | dset_destino.setGeoTransform(geot); |
||
305 | } |
||
306 | |||
307 | nBlocks = (int) (sizeWindowY / this.support.getBlockSize()); |
||
308 | anchoResto = sizeWindowY - (nBlocks * this.support.getBlockSize());
|
||
309 | } |
||
310 | |||
311 | /**
|
||
312 | * A partir de un elemento que contiene una propiedad y un valor
|
||
313 | * lo parsea y asigna el valor a su variable.
|
||
314 | * @param propValue elemento con la forma propiedad=valor
|
||
315 | */
|
||
316 | private void readProperty(String propValue) { |
||
317 | String prop = propValue.substring(0, propValue.indexOf("=")); |
||
318 | |||
319 | if (propValue.startsWith(prop)) {
|
||
320 | String value = propValue.substring(propValue.indexOf("=") + 1, |
||
321 | propValue.length()); |
||
322 | |||
323 | if ((value != null) && !value.equals("")) { |
||
324 | if (prop.equals("BLOCKSIZE")) { |
||
325 | this.support.setBlockSize(Integer.parseInt(value)); |
||
326 | } |
||
327 | |||
328 | if (prop.equals("GEOREF")) { |
||
329 | boolean georef = true; |
||
330 | |||
331 | if (value.equals("yes")) { |
||
332 | georef = true;
|
||
333 | } else {
|
||
334 | georef = false;
|
||
335 | } |
||
336 | |||
337 | this.support.setWriteGeoref(georef);
|
||
338 | } |
||
339 | |||
340 | if (prop.equals("INTERLEAVE")) { |
||
341 | this.support.setInterleave(value);
|
||
342 | } |
||
343 | |||
344 | if (prop.equals("PHOTOMETRIC")) { |
||
345 | this.support.setPhotometric(value);
|
||
346 | } |
||
347 | |||
348 | if (prop.equals("COMPRESS")) { |
||
349 | this.support.setCompress(value);
|
||
350 | } |
||
351 | |||
352 | if (prop.equals("TFW")) { |
||
353 | boolean tfw = true; |
||
354 | |||
355 | if (value.equals("yes")) { |
||
356 | tfw = true;
|
||
357 | } else {
|
||
358 | tfw = false;
|
||
359 | } |
||
360 | |||
361 | this.support.setTfw(tfw);
|
||
362 | } |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | |||
367 | /**
|
||
368 | * Asigna propiedades al driver a partir de un vector de
|
||
369 | * strings donde cada elemento tiene la estructura de
|
||
370 | * propiedad=valor.
|
||
371 | * @param props Propiedades
|
||
372 | */
|
||
373 | public void setProps(String[] props) { |
||
374 | for (int iProps = 0; iProps < props.length; iProps++) |
||
375 | readProperty(props[iProps]); |
||
376 | |||
377 | setParams(); |
||
378 | |||
379 | try {
|
||
380 | if (!consulta) {
|
||
381 | init(); |
||
382 | } |
||
383 | } catch (GdalException e) {
|
||
384 | e.printStackTrace(); |
||
385 | } |
||
386 | } |
||
387 | |||
388 | /**
|
||
389 | * Escribe tres bandas en el GDALRasterBand desde el IDataWriter con una
|
||
390 | * altura definida por sizeY.
|
||
391 | * @param buftmp Buffer
|
||
392 | * @param sizeY Altura en pixels del bloque leido
|
||
393 | * @param posicionY Posici?n y a partir de la cual se escribe en el GDALRasterBand destino
|
||
394 | */
|
||
395 | private void writeBands(int[] buftmp, int sizeY, int posicionY) { |
||
396 | //leemos el bloque origen
|
||
397 | buftmp = dataWriter.readData(sizeWindowX, sizeY, 0);
|
||
398 | |||
399 | bufBandR.buffByte = new byte[buftmp.length]; |
||
400 | bufBandG.buffByte = new byte[buftmp.length]; |
||
401 | bufBandB.buffByte = new byte[buftmp.length]; |
||
402 | |||
403 | //Escribimos el bloque destino
|
||
404 | for (int i = 0; i < buftmp.length; i++) { |
||
405 | bufBandR.buffByte[i] = (byte) (((buftmp[i] & 0xff0000) >> 16) & |
||
406 | 0xff);
|
||
407 | bufBandG.buffByte[i] = (byte) (((buftmp[i] & 0xff00) >> 8) & 0xff); |
||
408 | bufBandB.buffByte[i] = (byte) ((buftmp[i] & 0xff) & 0xff); |
||
409 | } |
||
410 | |||
411 | try {
|
||
412 | rband = dset_destino.getRasterBand(1);
|
||
413 | rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBandR,
|
||
414 | Gdal.GDT_Byte); |
||
415 | rband = dset_destino.getRasterBand(2);
|
||
416 | rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBandG,
|
||
417 | Gdal.GDT_Byte); |
||
418 | rband = dset_destino.getRasterBand(3);
|
||
419 | rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBandB,
|
||
420 | Gdal.GDT_Byte); |
||
421 | } catch (GdalException e) {
|
||
422 | e.printStackTrace(); |
||
423 | } |
||
424 | } |
||
425 | |||
426 | /**
|
||
427 | * Funci?n que gestiona la lectura desde el origen y la escritura
|
||
428 | * de Gdal sobre el fichero destino.
|
||
429 | * @param mode Modo de escritura
|
||
430 | * @throws IOException
|
||
431 | */
|
||
432 | private void write(int mode) throws IOException { |
||
433 | buf = new GdalBuffer();
|
||
434 | bufBandR = new GdalBuffer();
|
||
435 | bufBandG = new GdalBuffer();
|
||
436 | bufBandB = new GdalBuffer();
|
||
437 | |||
438 | int[] buftmp = null; |
||
439 | |||
440 | //long t1 = System.currentTimeMillis();
|
||
441 | try {
|
||
442 | if (mode == Mode.fileWrite) {
|
||
443 | for (int iBand = 0; iBand < this.nBands; iBand++) { |
||
444 | rband = dset_destino.getRasterBand(iBand + 1);
|
||
445 | |||
446 | for (int iBlock = 0; iBlock < nBlocks; iBlock++) { |
||
447 | if(write){
|
||
448 | //leemos el bloque origen
|
||
449 | buf.buffByte = currentRaster.getGeoFile().getWindow(0,
|
||
450 | iBlock * this.support.getBlockSize(),
|
||
451 | sizeWindowX, |
||
452 | this.support.getBlockSize(),
|
||
453 | iBand + |
||
454 | 1);
|
||
455 | |||
456 | //Escribimos el bloque destino
|
||
457 | rband.writeRaster(0,
|
||
458 | iBlock * this.support.getBlockSize(),
|
||
459 | sizeWindowX, |
||
460 | this.support.getBlockSize(), buf,
|
||
461 | Gdal.GDT_Byte); |
||
462 | }/*else
|
||
463 | this.writeClose();*/
|
||
464 | } |
||
465 | } |
||
466 | } else if (mode == Mode.dataWrite) { |
||
467 | //for(int iBlock=1;iBlock<=nBlocks;iBlock++){
|
||
468 | for (int iBlock = 0; iBlock < nBlocks; iBlock++) { |
||
469 | int posicionY = iBlock * this.support.getBlockSize(); |
||
470 | if(write)
|
||
471 | writeBands(buftmp, this.support.getBlockSize(), posicionY);
|
||
472 | /*else
|
||
473 | this.writeClose();*/
|
||
474 | } |
||
475 | } |
||
476 | |||
477 | if (anchoResto != 0) { |
||
478 | if (mode == Mode.fileWrite) {
|
||
479 | for (int iBand = 0; iBand < this.nBands; iBand++) { |
||
480 | rband = dset_destino.getRasterBand(iBand + 1);
|
||
481 | if(write){
|
||
482 | //leemos el bloque origen
|
||
483 | buf.buffByte = currentRaster.getGeoFile().getWindow(0,
|
||
484 | nBlocks * this.support.getBlockSize(),
|
||
485 | sizeWindowX, |
||
486 | anchoResto, |
||
487 | iBand + |
||
488 | 1);
|
||
489 | |||
490 | //Escribimos el bloque destino
|
||
491 | rband.writeRaster(0,
|
||
492 | nBlocks * this.support.getBlockSize(),
|
||
493 | sizeWindowX, anchoResto, buf, |
||
494 | Gdal.GDT_Byte); |
||
495 | }/*else
|
||
496 | this.writeClose();*/
|
||
497 | } |
||
498 | } else if (mode == Mode.dataWrite) { |
||
499 | int posicionY = nBlocks * this.support.getBlockSize(); |
||
500 | if(write)
|
||
501 | writeBands(buftmp, anchoResto, posicionY); |
||
502 | /*else
|
||
503 | this.writeClose();*/
|
||
504 | } |
||
505 | } |
||
506 | } catch (GdalException e) {
|
||
507 | e.printStackTrace(); |
||
508 | } |
||
509 | } |
||
510 | |||
511 | /**
|
||
512 | * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
|
||
513 | * @throws IOException
|
||
514 | */
|
||
515 | public void fileWrite() throws IOException { |
||
516 | if (currentRaster == null) { |
||
517 | throw new IOException("No se ha asignado un fichero de entrada."); |
||
518 | } |
||
519 | |||
520 | this.write(Mode.fileWrite);
|
||
521 | } |
||
522 | |||
523 | /**
|
||
524 | * Realiza una copia en el formato especificado.
|
||
525 | * @throws IOException
|
||
526 | */
|
||
527 | public static void createCopy(GdalDriver driverDst, String dst, String src, |
||
528 | boolean bstrict, String[] params, IProjection proj) throws IOException, GdalException { |
||
529 | if (dst == null || src == null) { |
||
530 | throw new IOException("No se ha asignado un fichero de entrada."); |
||
531 | } |
||
532 | |||
533 | GdalFile gdalFile = new GdalFile(proj, src);
|
||
534 | driverDst.createCopy(dst, gdalFile.file, bstrict, params); |
||
535 | if(dst.endsWith(".jpg") || dst.endsWith(".jpeg")) |
||
536 | GdalWriter.createWorldFile(dst, gdalFile); |
||
537 | gdalFile.close(); |
||
538 | } |
||
539 | |||
540 | /**
|
||
541 | * Crea un fichero de georeferenciaci?n
|
||
542 | * @param img
|
||
543 | * @param name
|
||
544 | * @return
|
||
545 | * @throws IOException
|
||
546 | */
|
||
547 | private static void createWorldFile(String name, GdalFile gdalFile) throws IOException{ |
||
548 | File tfw = null; |
||
549 | |||
550 | String extWorldFile = ".wld"; |
||
551 | if(name.endsWith("tif")) |
||
552 | extWorldFile = ".tfw";
|
||
553 | if(name.endsWith("jpg") || name.endsWith("jpeg")) |
||
554 | extWorldFile = ".jpgw";
|
||
555 | |||
556 | tfw = new File(name.substring(0, name.lastIndexOf(".")) + extWorldFile); |
||
557 | |||
558 | //Generamos un world file para gdal
|
||
559 | DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(tfw)) ); |
||
560 | dos.writeBytes((gdalFile.getExtent().getMax().getX() - gdalFile.getExtent().getMin().getX())/gdalFile.getWidth()+"\n");
|
||
561 | dos.writeBytes("0.0\n");
|
||
562 | dos.writeBytes("0.0\n");
|
||
563 | 8609 | nacho | dos.writeBytes("-"+Math.abs((gdalFile.getExtent().getMax().getY() - gdalFile.getExtent().getMin().getY())/gdalFile.getHeight())+"\n"); |
564 | 5990 | luisw2 | dos.writeBytes(""+gdalFile.getExtent().getMin().getX()+"\n"); |
565 | 8609 | nacho | dos.writeBytes(""+gdalFile.getExtent().getMax().getY()+"\n"); |
566 | 5990 | luisw2 | dos.close(); |
567 | } |
||
568 | |||
569 | |||
570 | /**
|
||
571 | * Realiza la escritura de datos con los datos que le pasa el cliente.
|
||
572 | * @throws IOException
|
||
573 | */
|
||
574 | public void dataWrite() throws IOException { |
||
575 | if (dataWriter == null) { |
||
576 | throw new IOException("No se ha obtenido un objeto de entrada para la escritura valido."); |
||
577 | } |
||
578 | |||
579 | this.write(Mode.dataWrite);
|
||
580 | } |
||
581 | |||
582 | /**
|
||
583 | * Cancela el salvado de datos.
|
||
584 | * @throws GdalException
|
||
585 | */
|
||
586 | public void writeClose() { |
||
587 | try {
|
||
588 | if(dset_destino != null) |
||
589 | dset_destino.close(); |
||
590 | oSRS = null;
|
||
591 | } catch (GdalException e) {
|
||
592 | e.printStackTrace(); |
||
593 | } |
||
594 | } |
||
595 | |||
596 | /**
|
||
597 | * Cancela el salvado de datos.
|
||
598 | */
|
||
599 | public void writeCancel() { |
||
600 | write = false;
|
||
601 | } |
||
602 | |||
603 | /**
|
||
604 | * Devuelve la configuraci?n de la ventana de dialogo
|
||
605 | * para las propiedades del driver de escritura de Gdal.
|
||
606 | * @return XML de configuraci?n del dialogo.
|
||
607 | */
|
||
608 | public String getXMLPropertiesDialog() { |
||
609 | StringBuffer options = null; |
||
610 | options = new StringBuffer(); |
||
611 | options.append("<window sizex=\"" + this.windowSizeX + "\" sizey=\"" + |
||
612 | this.windowSizeY + "\">"); |
||
613 | options.append("<panel sizex=\"" + this.panelSizeX + "\" sizey=\"" + |
||
614 | this.panelSizeY + "\" layout=\"" + this.panelLayout + |
||
615 | "\" border=\"yes\">");
|
||
616 | |||
617 | options.append("<panel layout=\"FlowLayout\" position=\"North\" align=\"left\">");
|
||
618 | 7295 | cesar | options.append("<label>"+Messages.getText("Block_Size_")+"</label>"); |
619 | 5990 | luisw2 | options.append("<combo ident=\"BLOCKSIZE\" selected=\"" +
|
620 | this.support.getBlockSize() + "\">"); |
||
621 | |||
622 | for (int i = 0; i < this.support.getBlockSizeList().length; i++) |
||
623 | options.append("<elem>" + this.support.getBlockSizeList()[i] + |
||
624 | "</elem>");
|
||
625 | |||
626 | options.append("</combo>");
|
||
627 | //options.append("<label>Georef Si/No:</label>");
|
||
628 | |||
629 | String sel = null; |
||
630 | |||
631 | if (this.support.getGeoref()) { |
||
632 | sel = new String("yes"); |
||
633 | } else {
|
||
634 | sel = new String("no"); |
||
635 | } |
||
636 | |||
637 | /*options.append("<check ident=\"GEOREF\" selected=\"" + sel +
|
||
638 | "\" text=\"\">");
|
||
639 | options.append("</check>");*/
|
||
640 | options.append("</panel>");
|
||
641 | |||
642 | options.append("<panel layout=\"FlowLayout\" position=\"Center\" align=\"left\">");
|
||
643 | 7295 | cesar | options.append("<label>"+Messages.getText("Photometric_")+"</label>"); |
644 | 5990 | luisw2 | options.append("<combo ident=\"PHOTOMETRIC\" selected=\"" +
|
645 | this.support.getPhotometric() + "\">"); |
||
646 | |||
647 | for (int i = 0; i < this.support.getPhotometricList().length; i++) |
||
648 | options.append("<elem>" + this.support.getPhotometricList()[i] + |
||
649 | "</elem>");
|
||
650 | |||
651 | options.append("</combo>");
|
||
652 | 7295 | cesar | options.append("<label>"+Messages.getText("Interleave_")+"</label>"); |
653 | 5990 | luisw2 | options.append("<combo ident=\"INTERLEAVE\" selected=\"" +
|
654 | this.support.getInterleave() + "\">"); |
||
655 | |||
656 | for (int i = 0; i < this.support.getInterleaveList().length; i++) |
||
657 | options.append("<elem>" + this.support.getInterleaveList()[i] + |
||
658 | "</elem>");
|
||
659 | |||
660 | options.append("</combo>");
|
||
661 | options.append("</panel>");
|
||
662 | |||
663 | options.append("<panel layout=\"FlowLayout\" position=\"South\" align=\"left\">");
|
||
664 | 7295 | cesar | options.append("<label>"+Messages.getText("Compression_")+"</label>"); |
665 | 5990 | luisw2 | options.append("<combo ident=\"COMPRESS\" selected=\"" +
|
666 | this.support.getCompress() + "\">"); |
||
667 | |||
668 | for (int i = 0; i < this.support.getCompressList().length; i++) |
||
669 | options.append("<elem>" + this.support.getCompressList()[i] + |
||
670 | "</elem>");
|
||
671 | |||
672 | options.append("</combo>");
|
||
673 | 7295 | cesar | options.append("<label>"+Messages.getText("Generate_Tfw_")+"</label>"); |
674 | 5990 | luisw2 | sel = null;
|
675 | |||
676 | if (this.support.getTfw()) { |
||
677 | sel = new String("yes"); |
||
678 | } else {
|
||
679 | sel = new String("no"); |
||
680 | } |
||
681 | |||
682 | options.append("<check ident=\"TFW\" selected=\"" + sel +
|
||
683 | "\" text=\"\">");
|
||
684 | options.append("</check>");
|
||
685 | options.append("</panel>");
|
||
686 | |||
687 | options.append("</panel>");
|
||
688 | options.append("</window>");
|
||
689 | |||
690 | return options.toString();
|
||
691 | } |
||
692 | |||
693 | /**
|
||
694 | * Obtiene el valor a la variable write que estar? a true cuando se est? escribiendo
|
||
695 | * o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
|
||
696 | * pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
|
||
697 | * de salida.
|
||
698 | * @return True si puede escribirse y false si no puede
|
||
699 | */
|
||
700 | public boolean isWrite() { |
||
701 | return write;
|
||
702 | } |
||
703 | |||
704 | /**
|
||
705 | * Asigna el valor a la variable write que estar? a true cuando se est? escribiendo
|
||
706 | * o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
|
||
707 | * pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
|
||
708 | * de salida.
|
||
709 | * @param write Variable booleana. True si puede escribirse y false si no puede
|
||
710 | */
|
||
711 | public void setWrite(boolean write) { |
||
712 | this.write = write;
|
||
713 | } |
||
714 | |||
715 | /**
|
||
716 | * Obtiene las opciones de salvado.
|
||
717 | * @return GdalSupportOptions
|
||
718 | */
|
||
719 | public GdalSupportOptions getSupport(){
|
||
720 | return this.support; |
||
721 | } |
||
722 | |||
723 | /**
|
||
724 | *
|
||
725 | * @author Nacho Brodin (brodin_ign@gva.es)
|
||
726 | *
|
||
727 | * Opciones que soporta el driver de escritura de Gdal.
|
||
728 | */
|
||
729 | public class GdalSupportOptions extends WriterSupportOptions { |
||
730 | private String[] photometric = { |
||
731 | "YCBR", "MINISBLACK", "MINISWHITE", |
||
732 | "RGB", "CMYK", "CIELAB", "ICCLAB", |
||
733 | "ITULAB", "CBCR" |
||
734 | }; |
||
735 | private String[] interleave = { "BAND", "PIXEL" }; |
||
736 | private String[] compress = { "LZW", "PACKBITS", "DEFLATE", "NONE" }; |
||
737 | private String photometricDefault = "RGB"; |
||
738 | private String interleaveDefault = "BAND"; |
||
739 | private String compressDefault = "NONE"; |
||
740 | |||
741 | private boolean tfw = false; |
||
742 | |||
743 | public GdalSupportOptions(String ext) { |
||
744 | super(ext);
|
||
745 | } |
||
746 | |||
747 | /**
|
||
748 | * @param defaultPhot Tipo de imagen
|
||
749 | */
|
||
750 | public void setPhotometric(String defaultPhot) { |
||
751 | this.photometricDefault = defaultPhot;
|
||
752 | } |
||
753 | |||
754 | /**
|
||
755 | * @param defaultInt
|
||
756 | */
|
||
757 | public void setInterleave(String defaultInt) { |
||
758 | this.interleaveDefault = defaultInt;
|
||
759 | } |
||
760 | |||
761 | /**
|
||
762 | * @param defaultComp
|
||
763 | */
|
||
764 | public void setCompress(String defaultComp) { |
||
765 | this.compressDefault = defaultComp;
|
||
766 | } |
||
767 | |||
768 | /**
|
||
769 | * Asigna true o false si se desea generar un fichero tfw con la
|
||
770 | * georeferenciaci?n o no;
|
||
771 | * @param tfw true se genera el fichero tfw y false no se genera
|
||
772 | */
|
||
773 | public void setTfw(boolean tfw) { |
||
774 | this.tfw = tfw;
|
||
775 | } |
||
776 | |||
777 | /**
|
||
778 | * @return
|
||
779 | */
|
||
780 | public String[] getPhotometricList() { |
||
781 | return photometric;
|
||
782 | } |
||
783 | |||
784 | /**
|
||
785 | * @return
|
||
786 | */
|
||
787 | public String[] getInterleaveList() { |
||
788 | return interleave;
|
||
789 | } |
||
790 | |||
791 | /**
|
||
792 | * @return
|
||
793 | */
|
||
794 | public String[] getCompressList() { |
||
795 | return compress;
|
||
796 | } |
||
797 | |||
798 | /**
|
||
799 | * @return
|
||
800 | */
|
||
801 | public String getPhotometric() { |
||
802 | return photometricDefault;
|
||
803 | } |
||
804 | |||
805 | /**
|
||
806 | * @return
|
||
807 | */
|
||
808 | public String getInterleave() { |
||
809 | return interleaveDefault;
|
||
810 | } |
||
811 | |||
812 | /**
|
||
813 | * Obtiene el par?metro de compresi?n
|
||
814 | * @return
|
||
815 | */
|
||
816 | public String getCompress() { |
||
817 | return compressDefault;
|
||
818 | } |
||
819 | |||
820 | /**
|
||
821 | * Devuelve true o false si se genera un fichero tfw con la
|
||
822 | * georeferenciaci?n o no;
|
||
823 | * @param tfw true se genera el fichero tfw y false no se genera
|
||
824 | */
|
||
825 | public boolean getTfw() { |
||
826 | return tfw;
|
||
827 | } |
||
828 | } |
||
829 | |||
830 | private class Mode { |
||
831 | public final static int fileWrite = 0; |
||
832 | public final static int dataWrite = 1; |
||
833 | } |
||
834 | } |