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