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