root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / io / GdalWriter.java @ 11981
History | View | Annotate | Download (25.8 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.dataset.io; |
20 |
|
21 |
import java.io.IOException; |
22 |
import java.util.ArrayList; |
23 |
|
24 |
import org.cresques.cts.IProjection; |
25 |
import org.gvsig.raster.buffer.RasterBuffer; |
26 |
import org.gvsig.raster.dataset.GeoRasterWriter; |
27 |
import org.gvsig.raster.dataset.IDataWriter; |
28 |
import org.gvsig.raster.dataset.NotSupportedExtensionException; |
29 |
import org.gvsig.raster.dataset.Params; |
30 |
import org.gvsig.raster.dataset.Params.Param; |
31 |
import org.gvsig.raster.dataset.io.features.GTiffFeatures; |
32 |
import org.gvsig.raster.dataset.io.features.HFAFeatures; |
33 |
import org.gvsig.raster.dataset.io.features.WriteFileFormatFeatures; |
34 |
import org.gvsig.raster.shared.Extent; |
35 |
import org.gvsig.raster.util.RasterUtilities; |
36 |
import org.gvsig.raster.util.extensionPoints.ExtensionPoints; |
37 |
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton; |
38 |
|
39 |
import es.gva.cit.jecwcompress.EcwException; |
40 |
import es.gva.cit.jgdal.Gdal; |
41 |
import es.gva.cit.jgdal.GdalBuffer; |
42 |
import es.gva.cit.jgdal.GdalException; |
43 |
import es.gva.cit.jgdal.GdalRasterBand; |
44 |
import es.gva.cit.jgdal.GeoTransform; |
45 |
|
46 |
|
47 |
/**
|
48 |
* Driver para la escritura a trav?s de Gdal.
|
49 |
* Puede exportar un fichero de un formato a otro desde un GeoRasterFile
|
50 |
* en cualquier formato soportado por la lectura a un formato que este incluido
|
51 |
* en la lista supportedDrv.
|
52 |
*
|
53 |
* Puede salvar a disco en un formato que este incluido en la lista supportedDrv
|
54 |
* obteniendo los datos que van siendo servidos desde el cliente. Este cliente
|
55 |
* debe implementar un IDataWriter o tener un objeto que lo implemente. Inicialmente
|
56 |
* le pasar? los par?metros de la imagen de salida y cuando el driver comience a
|
57 |
* escribir le ir? solicitando m?s a trav?s del m?todo readData de IDataWriter.
|
58 |
* El cliente ser? el que lleve el control de lo que va sirviendo y lo que le queda
|
59 |
* por servir.
|
60 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
61 |
*/
|
62 |
public class GdalWriter extends GeoRasterWriter { |
63 |
|
64 |
static {
|
65 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
66 |
|
67 |
extensionPoints.add("RasterWriter", "tif", GdalWriter.class); |
68 |
fileFeature.put("tif", new GTiffFeatures()); |
69 |
|
70 |
extensionPoints.add("RasterWriter", "img", GdalWriter.class); |
71 |
fileFeature.put("img", new HFAFeatures()); |
72 |
|
73 |
//Imagenes 1 banda (monocromo) o 3 (RGB) en 8 bits. Georef = .wld
|
74 |
/*features = new WriteFileFormatFeatures("BMP", "bmp", -1, null, GdalWriter.class);
|
75 |
extensionPoints.add("RasterWriter", "bmp", GdalWriter.class);
|
76 |
fileFeature.put("bmp", features);*/
|
77 |
} |
78 |
|
79 |
private es.gva.cit.jgdal.GdalDriver drv;
|
80 |
private Gdal dset_destino = null; |
81 |
private GdalRasterBand rband = null; |
82 |
private GeoTransform geot = null; //Datos de georeferenciaci?n |
83 |
//private OGRSpatialReference oSRS; //Datos de proyecci?n
|
84 |
private GdalBuffer[] bufBands = null; |
85 |
private int nBlocks = 0; //N?mero de bloques en Y en el que se divide la imagen para escribir |
86 |
private int anchoResto = 0; //Tama?o del ?ltimo bloque de la imagen. |
87 |
private boolean write = true; //Cuando est? a true se puede escribir en la imagen de salida. Si est? a false el proceso es interrumpido |
88 |
private int dataType = RasterBuffer.TYPE_UNDEFINED; |
89 |
|
90 |
/**
|
91 |
* Carga los par?metros de este driver.
|
92 |
*/
|
93 |
public void loadParams(String ident) { |
94 |
WriteFileFormatFeatures wfff = (WriteFileFormatFeatures)fileFeature.get(ident); |
95 |
wfff.loadParams(); |
96 |
driverParams = wfff.getParams(); |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Constructor para la obtenci?n de par?metros del driver
|
101 |
* @param drvType Tipo de driver
|
102 |
*/
|
103 |
public GdalWriter(String fileName) { |
104 |
ident = RasterUtilities.getExtensionFromFileName(fileName); |
105 |
driver = ((WriteFileFormatFeatures)fileFeature.get(ident)).getDriverName(); |
106 |
|
107 |
loadParams(ident); |
108 |
} |
109 |
|
110 |
/**
|
111 |
* Constructor para salvar datos servidos por el cliente
|
112 |
* @param dataWriter Objeto servidor de datos para el driver de escritura
|
113 |
* @param outFilename Fichero de salida
|
114 |
* @param blockSize Tama?o de bloque
|
115 |
* @param Extent extent
|
116 |
* @param compresion Compresi?n si la tiene
|
117 |
* @param outSizeX Tama?o de salida en X
|
118 |
* @param outSizeY Tama?o de salida en Y
|
119 |
* @param dataType Tipo de dato
|
120 |
* @throws GdalException
|
121 |
* @throws IOException
|
122 |
*/
|
123 |
public GdalWriter( IDataWriter dataWriter,
|
124 |
String outFileName,
|
125 |
Integer nBands,
|
126 |
Extent ex, |
127 |
Integer outSizeX,
|
128 |
Integer outSizeY,
|
129 |
Integer dataType,
|
130 |
Params params)throws GdalException, IOException { |
131 |
|
132 |
ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1); |
133 |
driver = ((WriteFileFormatFeatures)fileFeature.get(ident)).getDriverName(); |
134 |
this.dataType = dataType.intValue();
|
135 |
this.extent = ex;
|
136 |
percent = 0;
|
137 |
|
138 |
this.dataWriter = dataWriter;
|
139 |
this.outFileName = outFileName;
|
140 |
|
141 |
this.sizeWindowX = outSizeX.intValue();
|
142 |
this.sizeWindowY = outSizeY.intValue();
|
143 |
|
144 |
if ((sizeWindowX < 0) || (sizeWindowY < 0)) |
145 |
throw new IOException("Tama?o del fichero de salida erroneo."); |
146 |
|
147 |
this.nBands = nBands.intValue();
|
148 |
|
149 |
//Calculamos la georeferenciaci?n a partir del extend pasado por el cliente.
|
150 |
|
151 |
double maxX = ex.maxX();
|
152 |
double minX = ex.minX();
|
153 |
double maxY = ex.maxY();
|
154 |
double minY = ex.minY();
|
155 |
|
156 |
geot = new GeoTransform();
|
157 |
geot.adfgeotransform[0] = minX;
|
158 |
geot.adfgeotransform[3] = maxY; //-((pixelSizeY * outSizeY)-minY); |
159 |
geot.adfgeotransform[1] = (double) ((maxX - minX) / outSizeX.intValue()); //pixelSizeX; |
160 |
geot.adfgeotransform[5] = (double) ((minY - maxY) / outSizeY.intValue()); //pixelSizeY; |
161 |
|
162 |
if(params == null) |
163 |
loadParams(ident); |
164 |
else
|
165 |
this.driverParams = params;
|
166 |
|
167 |
init(); |
168 |
} |
169 |
|
170 |
/**
|
171 |
* Asigna el tipo de driver con el que se salvar? la imagen
|
172 |
* @param drvType Tipo de driver
|
173 |
*/
|
174 |
public void setDriverType(String drvType) { |
175 |
this.driver = drvType;
|
176 |
} |
177 |
|
178 |
/**
|
179 |
* Creaci?n del dataset de destino.
|
180 |
* @throws EcwException
|
181 |
*/
|
182 |
private void init() throws GdalException { |
183 |
//Controlamos que el tipo de driver sea correcto
|
184 |
if (driver == null) { |
185 |
throw new GdalException("Tipo de driver sin especificar."); |
186 |
} |
187 |
|
188 |
boolean okdrvtype = false; |
189 |
|
190 |
String[] types = GeoRasterWriter.getDriversType(); |
191 |
for (int i = 0; i < GeoRasterWriter.getNTypes(); i++) |
192 |
if (driver.equals(types[i]))
|
193 |
okdrvtype = true;
|
194 |
|
195 |
if (okdrvtype == false) |
196 |
throw new GdalException("El tipo de driver " + driver + " no est? soportado por GdalWriter."); |
197 |
|
198 |
//Obtenemos el driver y creamos el dataset del destino
|
199 |
drv = Gdal.getDriverByName(driver); |
200 |
|
201 |
if (dset_destino != null) { |
202 |
dset_destino.close(); |
203 |
dset_destino = null;
|
204 |
} |
205 |
|
206 |
dset_destino = drv.create(outFileName, sizeWindowX, sizeWindowY, |
207 |
nBands, RasterUtilities.getGdalTypeFromRasterBufType(dataType), gdalParamsFromRasterParams(driverParams)); |
208 |
|
209 |
dset_destino.setGeoTransform(geot); |
210 |
|
211 |
int blockSize = 256; |
212 |
try {
|
213 |
blockSize = Integer.parseInt(driverParams.getParamById("blocksize").defaultValue); |
214 |
}catch(NumberFormatException e) { |
215 |
//Se queda con el valor de inicializaci?n
|
216 |
} |
217 |
|
218 |
nBlocks = (int) (sizeWindowY / blockSize);
|
219 |
anchoResto = sizeWindowY - (nBlocks * blockSize); |
220 |
} |
221 |
|
222 |
public void anotherFile(String fileName)throws GdalException { |
223 |
dset_destino = drv.create(fileName, sizeWindowX, sizeWindowY, |
224 |
nBands, RasterUtilities.getGdalTypeFromRasterBufType(dataType), gdalParamsFromRasterParams(driverParams)); |
225 |
} |
226 |
|
227 |
/**
|
228 |
* Convierte los par?metros obtenidos desde el objeto params a parametros
|
229 |
* comprensibles por la librer?a gdal
|
230 |
* @param p Params
|
231 |
* @return Array de par?metros
|
232 |
*/
|
233 |
private String[] gdalParamsFromRasterParams(Params p) { |
234 |
if(p == null) |
235 |
return null; |
236 |
ArrayList paramList = new ArrayList(); |
237 |
Param phot = (Param)p.getParamById("photometric");
|
238 |
if(phot != null) |
239 |
paramList.add("PHOTOMETRIC=" + phot.defaultValue);
|
240 |
Param inter = (Param)p.getParamById("interleave");
|
241 |
if(inter != null) |
242 |
paramList.add("INTERLEAVE=" + inter.defaultValue);
|
243 |
Param comp = (Param)p.getParamById("compression");
|
244 |
if(comp != null) |
245 |
paramList.add("COMPRESS=" + comp.defaultValue);
|
246 |
Param comp1 = (Param)p.getParamById("compress");
|
247 |
if(comp1 != null) |
248 |
paramList.add("COMPRESS=" + comp1.defaultValue);
|
249 |
Param rrd = (Param)p.getParamById("rrd");
|
250 |
if(rrd != null) |
251 |
paramList.add("HFA_USE_RRD=" + rrd.defaultValue);
|
252 |
|
253 |
String[] result = new String[paramList.size()]; |
254 |
for (int i = 0; i < result.length; i++) |
255 |
result[i] = (String)paramList.get(i);
|
256 |
return result;
|
257 |
} |
258 |
|
259 |
/**
|
260 |
* A partir de un elemento que contiene una propiedad y un valor
|
261 |
* lo parsea y asigna el valor a su variable.
|
262 |
* @param propValue elemento con la forma propiedad=valor
|
263 |
*/
|
264 |
/* private void readProperty(String propValue) {
|
265 |
String prop = propValue.substring(0, propValue.indexOf("="));
|
266 |
|
267 |
if (propValue.startsWith(prop)) {
|
268 |
String value = propValue.substring(propValue.indexOf("=") + 1, propValue.length());
|
269 |
|
270 |
if ((value != null) && !value.equals("")) {
|
271 |
if (prop.equals("BLOCKSIZE"))
|
272 |
driverParams.changeParamValue("blocksize", value);
|
273 |
if (prop.equals("INTERLEAVE"))
|
274 |
driverParams.changeParamValue("interleave", value);
|
275 |
if (prop.equals("PHOTOMETRIC"))
|
276 |
driverParams.changeParamValue("photometric", value);
|
277 |
if (prop.equals("COMPRESS"))
|
278 |
driverParams.changeParamValue("compression", value);
|
279 |
if (prop.equals("TFW"))
|
280 |
driverParams.changeParamValue("tfw", value);
|
281 |
}
|
282 |
}
|
283 |
}*/
|
284 |
|
285 |
/**
|
286 |
* Asigna propiedades al driver a partir de un vector de
|
287 |
* strings donde cada elemento tiene la estructura de
|
288 |
* propiedad=valor.
|
289 |
* @param props Propiedades
|
290 |
*/
|
291 |
/*public void setProps(String[] props) {
|
292 |
for (int iProps = 0; iProps < props.length; iProps++)
|
293 |
readProperty(props[iProps]);
|
294 |
|
295 |
loadParams();
|
296 |
|
297 |
try {
|
298 |
if (!consulta)
|
299 |
init();
|
300 |
} catch (GdalException e) {
|
301 |
e.printStackTrace();
|
302 |
}
|
303 |
}*/
|
304 |
|
305 |
/**
|
306 |
* Escritura de datos tipo Byte.
|
307 |
* @param sizeY Alto del bloque que se escribe.
|
308 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
309 |
*/
|
310 |
public void writeByteBand(int sizeY, int posicionY) { |
311 |
byte[][] buftmp = dataWriter.readByteData(sizeWindowX, sizeY); |
312 |
for(int iBand = 0; iBand < nBands; iBand ++) |
313 |
bufBands[iBand].buffByte = new byte[buftmp[iBand].length]; |
314 |
|
315 |
//Escribimos el bloque destino
|
316 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
317 |
for (int i = 0; i < buftmp[iBand].length; i++) |
318 |
bufBands[iBand].buffByte[i] = buftmp[iBand][i]; |
319 |
|
320 |
for (int iBand = 0; iBand < buftmp.length; iBand++){ |
321 |
try {
|
322 |
rband = dset_destino.getRasterBand(iBand + 1);
|
323 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Byte);
|
324 |
} catch (GdalException e) {
|
325 |
//No se est? escribiendo ...
|
326 |
} |
327 |
} |
328 |
} |
329 |
|
330 |
/**
|
331 |
* Escritura de datos tipo Short.
|
332 |
* @param sizeY Alto del bloque que se escribe.
|
333 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
334 |
*/
|
335 |
public void writeShortBand(int sizeY, int posicionY) { |
336 |
short[][] buftmp = dataWriter.readShortData(sizeWindowX, sizeY); |
337 |
for(int iBand = 0; iBand < nBands; iBand ++) |
338 |
bufBands[iBand].buffShort = new short[buftmp[iBand].length]; |
339 |
|
340 |
//Escribimos el bloque destino
|
341 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
342 |
for (int i = 0; i < buftmp[iBand].length; i++) |
343 |
bufBands[iBand].buffShort[i] = buftmp[iBand][i]; |
344 |
|
345 |
for (int iBand = 0; iBand < buftmp.length; iBand++){ |
346 |
try {
|
347 |
rband = dset_destino.getRasterBand(iBand + 1);
|
348 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Int16);
|
349 |
} catch (GdalException e) {
|
350 |
//No se est? escribiendo ...
|
351 |
} |
352 |
} |
353 |
} |
354 |
|
355 |
/**
|
356 |
* Escritura de datos tipo Int.
|
357 |
* @param sizeY Alto del bloque que se escribe.
|
358 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
359 |
*/
|
360 |
public void writeIntBand(int sizeY, int posicionY) { |
361 |
int[][] buftmp = dataWriter.readIntData(sizeWindowX, sizeY); |
362 |
for(int iBand = 0; iBand < nBands; iBand ++) |
363 |
bufBands[iBand].buffInt = new int[buftmp[iBand].length]; |
364 |
|
365 |
//Escribimos el bloque destino
|
366 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
367 |
for (int i = 0; i < buftmp[iBand].length; i++) |
368 |
bufBands[iBand].buffInt[i] = buftmp[iBand][i]; |
369 |
|
370 |
for (int iBand = 0; iBand < buftmp.length; iBand++){ |
371 |
try {
|
372 |
rband = dset_destino.getRasterBand(iBand + 1);
|
373 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Int32);
|
374 |
} catch (GdalException e) {
|
375 |
//No se est? escribiendo ...
|
376 |
} |
377 |
} |
378 |
} |
379 |
|
380 |
/**
|
381 |
* Escritura de datos tipo Float.
|
382 |
* @param sizeY Alto del bloque que se escribe.
|
383 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
384 |
*/
|
385 |
public void writeFloatBand(int sizeY, int posicionY) { |
386 |
float[][] buftmp = dataWriter.readFloatData(sizeWindowX, sizeY); |
387 |
for(int iBand = 0; iBand < nBands; iBand ++) |
388 |
bufBands[iBand].buffFloat = new float[buftmp[iBand].length]; |
389 |
|
390 |
//Escribimos el bloque destino
|
391 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
392 |
for (int i = 0; i < buftmp[iBand].length; i++) |
393 |
bufBands[iBand].buffFloat[i] = buftmp[iBand][i]; |
394 |
|
395 |
for (int iBand = 0; iBand < buftmp.length; iBand++){ |
396 |
try {
|
397 |
rband = dset_destino.getRasterBand(iBand + 1);
|
398 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Float32);
|
399 |
} catch (GdalException e) {
|
400 |
//No se est? escribiendo ...
|
401 |
} |
402 |
} |
403 |
} |
404 |
|
405 |
/**
|
406 |
* Escritura de datos tipo Double.
|
407 |
* @param sizeY Alto del bloque que se escribe.
|
408 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
409 |
*/
|
410 |
public void writeDoubleBand(int sizeY, int posicionY) { |
411 |
double[][] buftmp = dataWriter.readDoubleData(sizeWindowX, sizeY); |
412 |
for(int iBand = 0; iBand < nBands; iBand ++) |
413 |
bufBands[iBand].buffDouble = new double[buftmp[iBand].length]; |
414 |
|
415 |
//Escribimos el bloque destino
|
416 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
417 |
for (int i = 0; i < buftmp[iBand].length; i++) |
418 |
bufBands[iBand].buffDouble[i] = buftmp[iBand][i]; |
419 |
|
420 |
for (int iBand = 0; iBand < buftmp.length; iBand++){ |
421 |
try {
|
422 |
rband = dset_destino.getRasterBand(iBand + 1);
|
423 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Float64);
|
424 |
} catch (GdalException e) {
|
425 |
//No se est? escribiendo ...
|
426 |
} |
427 |
} |
428 |
} |
429 |
/**
|
430 |
* Escritura para tipo de dato ARGB.
|
431 |
* @param sizeY Alto del bloque que se escribe.
|
432 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
433 |
*/
|
434 |
public void writeARGBBand(int sizeY, int posicionY) { |
435 |
int[] buftmp = dataWriter.readARGBData(sizeWindowX, sizeY, 0); |
436 |
for(int iBand = 0; iBand < nBands; iBand ++) |
437 |
bufBands[iBand].buffByte = new byte[buftmp.length]; |
438 |
|
439 |
//Escribimos el bloque destino
|
440 |
for (int i = 0; i < buftmp.length; i++) { |
441 |
bufBands[0].buffByte[i] = (byte) (((buftmp[i] & 0xff0000) >> 16) & |
442 |
0xff);
|
443 |
bufBands[1].buffByte[i] = (byte) (((buftmp[i] & 0xff00) >> 8) & 0xff); |
444 |
bufBands[2].buffByte[i] = (byte) ((buftmp[i] & 0xff) & 0xff); |
445 |
} |
446 |
|
447 |
try {
|
448 |
rband = dset_destino.getRasterBand(1);
|
449 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[0], |
450 |
Gdal.GDT_Byte); |
451 |
rband = dset_destino.getRasterBand(2);
|
452 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[1], |
453 |
Gdal.GDT_Byte); |
454 |
rband = dset_destino.getRasterBand(3);
|
455 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[2], |
456 |
Gdal.GDT_Byte); |
457 |
} catch (GdalException e) {
|
458 |
e.printStackTrace(); |
459 |
} |
460 |
} |
461 |
|
462 |
/**
|
463 |
* Escribe tres bandas en el GDALRasterBand desde el IDataWriter con una
|
464 |
* altura definida por sizeY.
|
465 |
* @param buftmp Buffer
|
466 |
* @param sizeY Altura en pixels del bloque leido
|
467 |
* @param posicionY Posici?n y a partir de la cual se escribe en el GDALRasterBand destino
|
468 |
*/
|
469 |
private void writeBands(int sizeY, int posicionY) { |
470 |
//leemos el bloque origen
|
471 |
|
472 |
switch(dataType){
|
473 |
case RasterBuffer.TYPE_IMAGE:
|
474 |
writeARGBBand(sizeY, posicionY); |
475 |
break;
|
476 |
case RasterBuffer.TYPE_BYTE:
|
477 |
writeByteBand(sizeY, posicionY); |
478 |
break;
|
479 |
case RasterBuffer.TYPE_SHORT:
|
480 |
writeShortBand(sizeY, posicionY); |
481 |
break;
|
482 |
case RasterBuffer.TYPE_INT:
|
483 |
writeIntBand(sizeY, posicionY); |
484 |
break;
|
485 |
case RasterBuffer.TYPE_FLOAT:
|
486 |
writeFloatBand(sizeY, posicionY); |
487 |
break;
|
488 |
case RasterBuffer.TYPE_DOUBLE:
|
489 |
writeDoubleBand(sizeY, posicionY); |
490 |
break;
|
491 |
} |
492 |
} |
493 |
|
494 |
/**
|
495 |
* Funci?n que gestiona la lectura desde el origen y la escritura
|
496 |
* de Gdal sobre el fichero destino.
|
497 |
* @param mode Modo de escritura
|
498 |
* @throws IOException
|
499 |
*/
|
500 |
private void write(int mode) throws IOException { |
501 |
bufBands = new GdalBuffer[nBands];
|
502 |
for(int iBand = 0; iBand < nBands; iBand ++) |
503 |
bufBands[iBand] = new GdalBuffer();
|
504 |
|
505 |
int blockSize = 256; |
506 |
try {
|
507 |
blockSize = Integer.parseInt(driverParams.getParamById("blocksize").defaultValue); |
508 |
}catch(NumberFormatException e) { |
509 |
//Se queda con el valor de inicializaci?n
|
510 |
} |
511 |
|
512 |
percent = 0;
|
513 |
nBlocks = (int) (sizeWindowY / blockSize);
|
514 |
int increment = (blockSize * 100) / sizeWindowY; |
515 |
|
516 |
if (mode == GeoRasterWriter.MODE_FILEWRITE) {
|
517 |
/*for (int iBand = 0; iBand < this.nBands; iBand++) {
|
518 |
rband = dset_destino.getRasterBand(iBand + 1);
|
519 |
|
520 |
for (int iBlock = 0; iBlock < nBlocks; iBlock++) {
|
521 |
if(write){
|
522 |
//leemos el bloque origen
|
523 |
buf.buffByte = currentRaster.getGeoFile().getWindow(0,
|
524 |
iBlock * this.support.getBlockSize(),
|
525 |
sizeWindowX,
|
526 |
this.support.getBlockSize(),
|
527 |
iBand +
|
528 |
1);
|
529 |
|
530 |
//Escribimos el bloque destino
|
531 |
rband.writeRaster(0,
|
532 |
iBlock * this.support.getBlockSize(),
|
533 |
sizeWindowX,
|
534 |
this.support.getBlockSize(), buf,
|
535 |
Gdal.GDT_Byte);
|
536 |
}//else
|
537 |
//this.writeClose();
|
538 |
}
|
539 |
}*/
|
540 |
} else if (mode == GeoRasterWriter.MODE_DATAWRITE) { |
541 |
for (int iBlock = 0; iBlock < nBlocks; iBlock++) { |
542 |
int posicionY = iBlock * blockSize;
|
543 |
if(write)
|
544 |
writeBands( blockSize, posicionY); |
545 |
percent = (iBlock + 1) * increment;
|
546 |
} |
547 |
} |
548 |
|
549 |
if (anchoResto != 0) { |
550 |
if (mode == GeoRasterWriter.MODE_FILEWRITE) {
|
551 |
/*for (int iBand = 0; iBand < this.nBands; iBand++) {
|
552 |
rband = dset_destino.getRasterBand(iBand + 1);
|
553 |
if(write){
|
554 |
//leemos el bloque origen
|
555 |
buf.buffByte = currentRaster.getGeoFile().getWindow(0,
|
556 |
nBlocks * this.support.getBlockSize(),
|
557 |
sizeWindowX,
|
558 |
anchoResto,
|
559 |
iBand +
|
560 |
1);
|
561 |
|
562 |
//Escribimos el bloque destino
|
563 |
rband.writeRaster(0,
|
564 |
nBlocks * this.support.getBlockSize(),
|
565 |
sizeWindowX, anchoResto, buf,
|
566 |
Gdal.GDT_Byte);
|
567 |
}//else
|
568 |
//this.writeClose();
|
569 |
}*/
|
570 |
} else if (mode == GeoRasterWriter.MODE_DATAWRITE) { |
571 |
int posicionY = nBlocks * blockSize;
|
572 |
if(write)
|
573 |
writeBands(anchoResto, posicionY); |
574 |
percent = nBlocks * increment; |
575 |
} |
576 |
} |
577 |
|
578 |
} |
579 |
|
580 |
/**
|
581 |
* Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
|
582 |
* @throws IOException
|
583 |
*/
|
584 |
public void fileWrite() throws IOException { |
585 |
write(GeoRasterWriter.MODE_FILEWRITE); |
586 |
} |
587 |
|
588 |
/**
|
589 |
* Realiza una copia en el formato especificado.
|
590 |
* @throws IOException
|
591 |
*/
|
592 |
public static void createCopy(es.gva.cit.jgdal.GdalDriver driverDst, String dst, String src, |
593 |
boolean bstrict, String[] params, IProjection proj) throws IOException, GdalException { |
594 |
if (dst == null || src == null) { |
595 |
throw new IOException("No se ha asignado un fichero de entrada."); |
596 |
} |
597 |
|
598 |
GdalDriver gdalFile; |
599 |
try {
|
600 |
gdalFile = new GdalDriver(proj, src);
|
601 |
driverDst.createCopy(dst, gdalFile.getNative(), bstrict, params); |
602 |
if(dst.endsWith(".jpg") || dst.endsWith(".jpeg")) |
603 |
RasterUtilities.createWorldFile(dst, gdalFile.getExtent(), gdalFile.getWidth(), gdalFile.getHeight()); |
604 |
gdalFile.close(); |
605 |
} catch (NotSupportedExtensionException e) {
|
606 |
e.printStackTrace(); |
607 |
} |
608 |
} |
609 |
|
610 |
/**
|
611 |
* Realiza la escritura de datos con los datos que le pasa el cliente.
|
612 |
* @throws IOException
|
613 |
*/
|
614 |
public void dataWrite() throws IOException { |
615 |
if (dataWriter == null) |
616 |
throw new IOException("No se ha obtenido un objeto de entrada para la escritura valido."); |
617 |
|
618 |
write(GeoRasterWriter.MODE_DATAWRITE); |
619 |
|
620 |
if(driverParams.getParamById("tfw") != null && driverParams.getParamById("tfw").defaultValue.equals("true")) { |
621 |
if(extent != null) |
622 |
RasterUtilities.createWorldFile(this.outFileName, extent, sizeWindowX, sizeWindowY);
|
623 |
} |
624 |
} |
625 |
|
626 |
/**
|
627 |
* Cancela el salvado de datos.
|
628 |
* @throws GdalException
|
629 |
*/
|
630 |
public void writeClose() { |
631 |
try {
|
632 |
if(dset_destino != null) |
633 |
dset_destino.close(); |
634 |
//oSRS = null;
|
635 |
} catch (GdalException e) {
|
636 |
e.printStackTrace(); |
637 |
} |
638 |
} |
639 |
|
640 |
/**
|
641 |
* Cancela el salvado de datos.
|
642 |
*/
|
643 |
public void writeCancel() { |
644 |
write = false;
|
645 |
} |
646 |
|
647 |
/**
|
648 |
* Obtiene el valor a la variable write que estar? a true cuando se est? escribiendo
|
649 |
* o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
|
650 |
* pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
|
651 |
* de salida.
|
652 |
* @return True si puede escribirse y false si no puede
|
653 |
*/
|
654 |
public boolean isWrite() { |
655 |
return write;
|
656 |
} |
657 |
|
658 |
/**
|
659 |
* Asigna el valor a la variable write que estar? a true cuando se est? escribiendo
|
660 |
* o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
|
661 |
* pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
|
662 |
* de salida.
|
663 |
* @param write Variable booleana. True si puede escribirse y false si no puede
|
664 |
*/
|
665 |
public void setWrite(boolean write) { |
666 |
this.write = write;
|
667 |
} |
668 |
|
669 |
/**
|
670 |
* Asigna los par?metros del driver modificados por el cliente.
|
671 |
* @param Params
|
672 |
*/
|
673 |
public void setParams(Params params) { |
674 |
this.driverParams = params;
|
675 |
|
676 |
int blockSize = 256; |
677 |
try {
|
678 |
blockSize = Integer.parseInt(driverParams.getParamById("blocksize").defaultValue); |
679 |
nBlocks = (int) (sizeWindowY / blockSize);
|
680 |
anchoResto = sizeWindowY - (nBlocks * blockSize); |
681 |
}catch(NumberFormatException e) { |
682 |
//Se queda con el valor de inicializaci?n
|
683 |
} |
684 |
} |
685 |
|
686 |
} |