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