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