gvsig-raster / org.gvsig.raster.gdal / branches / org.gvsig.raster.gdal_dataaccess_refactoring / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / raster / gdal / io / GdalWriter.java @ 2440
History | View | Annotate | Download (26.2 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.gdal.io; |
23 |
|
24 |
import java.awt.geom.AffineTransform; |
25 |
import java.awt.geom.Point2D; |
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
import java.util.ArrayList; |
29 |
|
30 |
import org.cresques.cts.IProjection; |
31 |
import org.gvsig.fmap.dal.coverage.RasterLibrary; |
32 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
33 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
34 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
35 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
36 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
37 |
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException; |
38 |
import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
39 |
import org.gvsig.jgdal.Gdal; |
40 |
import org.gvsig.jgdal.GdalBuffer; |
41 |
import org.gvsig.jgdal.GdalDriver; |
42 |
import org.gvsig.jgdal.GdalException; |
43 |
import org.gvsig.jgdal.GdalRasterBand; |
44 |
import org.gvsig.jgdal.GeoTransform; |
45 |
import org.gvsig.raster.gdal.io.features.BMPFeatures; |
46 |
import org.gvsig.raster.gdal.io.features.GTiffFeatures; |
47 |
import org.gvsig.raster.gdal.io.features.HFAFeatures; |
48 |
import org.gvsig.raster.gdal.io.features.IDRISIFeatures; |
49 |
import org.gvsig.raster.gdal.io.features.ILWIS_MprFeatures; |
50 |
import org.gvsig.raster.gdal.io.features.Jpeg2000Features; |
51 |
import org.gvsig.raster.gdal.io.features.PNM_PgmFeatures; |
52 |
import org.gvsig.raster.gdal.io.features.PNM_PpmFeatures; |
53 |
import org.gvsig.raster.impl.buffer.DefaultDataServerWriter; |
54 |
import org.gvsig.raster.impl.buffer.RasterBuffer; |
55 |
import org.gvsig.raster.impl.process.RasterTask; |
56 |
import org.gvsig.raster.impl.process.RasterTaskQueue; |
57 |
import org.gvsig.raster.impl.store.ParamImpl; |
58 |
import org.gvsig.raster.impl.store.ParamsImpl; |
59 |
import org.gvsig.raster.impl.store.WriteFileFormatFeatures; |
60 |
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation; |
61 |
import org.gvsig.raster.impl.store.writer.DefaultRasterWriter; |
62 |
import org.gvsig.raster.util.DefaultProviderServices; |
63 |
import org.gvsig.tools.ToolsLocator; |
64 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
65 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
66 |
|
67 |
|
68 |
/**
|
69 |
* Driver para la escritura a trav?s de Gdal.
|
70 |
* Puede exportar un fichero de un formato a otro desde un GeoRasterFile
|
71 |
* en cualquier formato soportado por la lectura a un formato que este incluido
|
72 |
* en la lista supportedDrv.
|
73 |
*
|
74 |
* Puede salvar a disco en un formato que este incluido en la lista supportedDrv
|
75 |
* obteniendo los datos que van siendo servidos desde el cliente. Este cliente
|
76 |
* debe implementar un DataServerWriter o tener un objeto que lo implemente. Inicialmente
|
77 |
* le pasar? los par?metros de la imagen de salida y cuando el driver comience a
|
78 |
* escribir le ir? solicitando m?s a trav?s del m?todo readData de DataServerWriter.
|
79 |
* El cliente ser? el que lleve el control de lo que va sirviendo y lo que le queda
|
80 |
* por servir.
|
81 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
82 |
*/
|
83 |
public class GdalWriter extends DefaultRasterWriter { |
84 |
|
85 |
public static void register() { |
86 |
DefaultProviderServices pInfo = (DefaultProviderServices)RasterLocator.getManager().getProviderServices(); |
87 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
88 |
ExtensionPoint point = extensionPoints.get("RasterWriter");
|
89 |
|
90 |
point.append("tif", "", GdalWriter.class); |
91 |
pInfo.getFileFeature().put("tif", new GTiffFeatures()); |
92 |
|
93 |
point.append("img", "", GdalWriter.class); |
94 |
pInfo.getFileFeature().put("img", new HFAFeatures()); |
95 |
|
96 |
point.append("bmp", "", GdalWriter.class); |
97 |
pInfo.getFileFeature().put("bmp", new BMPFeatures()); |
98 |
|
99 |
point.append("pgm", "", GdalWriter.class); |
100 |
pInfo.getFileFeature().put("pgm", new PNM_PgmFeatures()); |
101 |
|
102 |
point.append("ppm", "", GdalWriter.class); |
103 |
pInfo.getFileFeature().put("ppm", new PNM_PpmFeatures()); |
104 |
|
105 |
point.append("mpl", "", GdalWriter.class); |
106 |
pInfo.getFileFeature().put("mpl", new ILWIS_MprFeatures()); |
107 |
|
108 |
point.append("rst", "", GdalWriter.class); |
109 |
pInfo.getFileFeature().put("rst", new IDRISIFeatures()); |
110 |
|
111 |
/*point.append("jp2", "", GdalWriter.class);
|
112 |
pInfo.getFileFeature().put("jp2", new Jpeg2000Features());*/
|
113 |
|
114 |
//La exportaci?n no es correcta del todo
|
115 |
//point.register("rmf", GdalWriter.class);
|
116 |
//fileFeature.put("rmf", new RMFFeatures());
|
117 |
|
118 |
//No salva datos. Siempre sale negra la imagen
|
119 |
//point.register("aux", GdalWriter.class);
|
120 |
//fileFeature.put("aux", new PAuxFeatures());
|
121 |
} |
122 |
|
123 |
private GdalDriver drv;
|
124 |
private Gdal dstDataset = null; |
125 |
private GdalRasterBand rband = null; |
126 |
private GeoTransform geot = null; //Datos de georeferenciaci?n |
127 |
//private OGRSpatialReference oSRS; //Datos de proyecci?n
|
128 |
private GdalBuffer[] bufBands = null; |
129 |
private int nBlocks = 0; //N?mero de bloques en Y en el que se divide la imagen para escribir |
130 |
private int anchoResto = 0; //Tama?o del ?ltimo bloque de la imagen. |
131 |
private boolean write = true; //Cuando est? a true se puede escribir en la imagen de salida. Si est? a false el proceso es interrumpido |
132 |
private int dataType = RasterBuffer.TYPE_UNDEFINED; |
133 |
|
134 |
/**
|
135 |
* Carga los par?metros de este driver.
|
136 |
*/
|
137 |
public void loadParams(String ident) { |
138 |
WriteFileFormatFeatures wfff = (WriteFileFormatFeatures)pInfo.getFileFeature().get(ident); |
139 |
wfff.loadParams(); |
140 |
driverParams = (ParamsImpl)wfff.getParams(); |
141 |
} |
142 |
|
143 |
public String getProviderName() { |
144 |
return GdalProvider.NAME;
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* Constructor para la obtenci?n de par?metros del driver
|
149 |
* @param drvType Tipo de driver
|
150 |
*/
|
151 |
public GdalWriter(String fileName) { |
152 |
ident = fileUtil.getExtensionFromFileName(fileName); |
153 |
driver = ((WriteFileFormatFeatures)pInfo.getFileFeature().get(ident)).getDriverName(); |
154 |
|
155 |
loadParams(ident); |
156 |
} |
157 |
|
158 |
/**
|
159 |
* Constructor para salvar datos servidos por el cliente
|
160 |
* @param dataWriter Objeto servidor de datos para el driver de escritura
|
161 |
* @param outFilename Fichero de salida
|
162 |
* @param blockSize Tama?o de bloque
|
163 |
* @param Extent extent
|
164 |
* @param compresion Compresi?n si la tiene
|
165 |
* @param outSizeX Tama?o de salida en X
|
166 |
* @param outSizeY Tama?o de salida en Y
|
167 |
* @param dataType Tipo de dato
|
168 |
* @throws GdalException
|
169 |
* @throws IOException
|
170 |
*/
|
171 |
public GdalWriter( DataServerWriter dataWriter,
|
172 |
String outFileName,
|
173 |
Integer nBands,
|
174 |
AffineTransform at,
|
175 |
Integer outSizeX,
|
176 |
Integer outSizeY,
|
177 |
Integer dataType,
|
178 |
Params params, |
179 |
IProjection proj) throws GdalException, IOException { |
180 |
this(dataWriter, outFileName, nBands, at, outSizeX, outSizeY, dataType, params, proj, new Boolean(true)); |
181 |
} |
182 |
|
183 |
/**
|
184 |
* Constructor para salvar datos servidos por el cliente
|
185 |
* @param dataWriter Objeto servidor de datos para el driver de escritura
|
186 |
* @param outFilename Fichero de salida
|
187 |
* @param blockSize Tama?o de bloque
|
188 |
* @param Extent extent
|
189 |
* @param compresion Compresi?n si la tiene
|
190 |
* @param outSizeX Tama?o de salida en X
|
191 |
* @param outSizeY Tama?o de salida en Y
|
192 |
* @param dataType Tipo de dato
|
193 |
* @param geo Flag que dice si se salva con georreferenciaci?n o sin ella
|
194 |
* @throws GdalException
|
195 |
* @throws IOException
|
196 |
*/
|
197 |
public GdalWriter( DataServerWriter dataWriter,
|
198 |
String outFileName,
|
199 |
Integer nBands,
|
200 |
AffineTransform at,
|
201 |
Integer outSizeX,
|
202 |
Integer outSizeY,
|
203 |
Integer dataType,
|
204 |
Params params, |
205 |
IProjection proj, |
206 |
Boolean geo)throws GdalException, IOException { |
207 |
|
208 |
this.proj = proj;
|
209 |
ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1); |
210 |
driver = ((WriteFileFormatFeatures)pInfo.getFileFeature().get(ident)).getDriverName(); |
211 |
this.dataType = dataType.intValue();
|
212 |
this.at = at;
|
213 |
percent = 0;
|
214 |
|
215 |
this.dataWriter = dataWriter;
|
216 |
this.outFileName = outFileName;
|
217 |
|
218 |
this.sizeWindowX = outSizeX.intValue();
|
219 |
this.sizeWindowY = outSizeY.intValue();
|
220 |
|
221 |
if ((sizeWindowX < 0) || (sizeWindowY < 0)) |
222 |
throw new IOException("Tama?o del fichero de salida erroneo."); |
223 |
|
224 |
this.nBands = nBands.intValue();
|
225 |
|
226 |
//Calculamos la georeferenciaci?n a partir del extend pasado por el cliente.
|
227 |
|
228 |
geot = new GeoTransform();
|
229 |
geot.adfgeotransform[0] = at.getTranslateX();
|
230 |
geot.adfgeotransform[3] = at.getTranslateY();
|
231 |
geot.adfgeotransform[1] = at.getScaleX();
|
232 |
geot.adfgeotransform[5] = at.getScaleY();
|
233 |
geot.adfgeotransform[2] = at.getShearX();
|
234 |
geot.adfgeotransform[4] = at.getShearY();
|
235 |
|
236 |
String outRmf = outFileName.substring(0, outFileName.lastIndexOf(".")); |
237 |
if(geo.booleanValue())
|
238 |
rasterUtil.saveGeoInfo(outRmf, at, new Point2D.Double(sizeWindowX, sizeWindowY)); |
239 |
|
240 |
if(params == null) |
241 |
loadParams(ident); |
242 |
else
|
243 |
if(params instanceof ParamsImpl) |
244 |
this.driverParams = (ParamsImpl)params;
|
245 |
|
246 |
init(); |
247 |
} |
248 |
|
249 |
/**
|
250 |
* A?ade la proyecci?n Wkt con la que salvar.
|
251 |
* @param wkt
|
252 |
* @throws GdalException
|
253 |
*/
|
254 |
public void setWkt(String wkt) { |
255 |
if(dstDataset != null && wkt != null && wkt.compareTo("unknown") != 0) |
256 |
try {
|
257 |
dstDataset.setProjection(wkt); |
258 |
} catch (GdalException e) {
|
259 |
System.err.println("Proyecci?n Wkt no asignada en GdalWriter"); |
260 |
return;
|
261 |
} |
262 |
} |
263 |
|
264 |
/**
|
265 |
* Asigna el tipo de driver con el que se salvar? la imagen
|
266 |
* @param drvType Tipo de driver
|
267 |
*/
|
268 |
public void setDriverType(String drvType) { |
269 |
this.driver = drvType;
|
270 |
} |
271 |
|
272 |
/**
|
273 |
* Creaci?n del dataset de destino.
|
274 |
* @throws EcwException
|
275 |
*/
|
276 |
private void init() throws GdalException { |
277 |
//Controlamos que el tipo de driver sea correcto
|
278 |
if (driver == null) |
279 |
throw new GdalException("Tipo de driver sin especificar."); |
280 |
|
281 |
boolean okdrvtype = false; |
282 |
|
283 |
String[] types = pInfo.getWriteDriversType(); |
284 |
for (int i = 0; i < pInfo.getWriteNTypes(); i++) |
285 |
if (driver.equals(types[i]))
|
286 |
okdrvtype = true;
|
287 |
|
288 |
if (okdrvtype == false) |
289 |
throw new GdalException("El tipo de driver " + driver + " no est? soportado por GdalWriter."); |
290 |
|
291 |
//Obtenemos el driver y creamos el dataset del destino
|
292 |
drv = Gdal.getDriverByName(driver); |
293 |
|
294 |
if (dstDataset != null) { |
295 |
dstDataset.close(); |
296 |
dstDataset = null;
|
297 |
} |
298 |
|
299 |
dstDataset = drv.create(outFileName, sizeWindowX, sizeWindowY, |
300 |
nBands, GdalNative.getGdalTypeFromRasterBufType(dataType), gdalParamsFromRasterParams(driverParams)); |
301 |
|
302 |
dstDataset.setGeoTransform(geot); |
303 |
|
304 |
int blockSize = RasterLibrary.blockHeight;
|
305 |
if(dataWriter.getBuffer() != null && dataWriter.getBuffer().isCached()) |
306 |
blockSize = dataWriter.getBuffer().getBlockHeight(); |
307 |
|
308 |
nBlocks = (sizeWindowY / blockSize); |
309 |
anchoResto = sizeWindowY - (nBlocks * blockSize); |
310 |
} |
311 |
|
312 |
public void anotherFile(String fileName)throws GdalException { |
313 |
dstDataset = drv.create(fileName, sizeWindowX, sizeWindowY, |
314 |
nBands, GdalNative.getGdalTypeFromRasterBufType(dataType), gdalParamsFromRasterParams(driverParams)); |
315 |
} |
316 |
|
317 |
/**
|
318 |
* Convierte los par?metros obtenidos desde el objeto params a parametros
|
319 |
* comprensibles por la librer?a gdal
|
320 |
* @param p Params
|
321 |
* @return Array de par?metros
|
322 |
*/
|
323 |
public String[] gdalParamsFromRasterParams(Params p) { |
324 |
if (p == null) |
325 |
return null; |
326 |
ArrayList<String> paramList = new ArrayList<String>(); |
327 |
ParamImpl phot = (ParamImpl)p.getParamById("photometric");
|
328 |
if (phot != null) |
329 |
paramList.add("PHOTOMETRIC=" + phot.getList()[((Integer) phot.getDefaultValue()).intValue()]); |
330 |
ParamImpl inter = (ParamImpl)p.getParamById("interleave");
|
331 |
if (inter != null) |
332 |
paramList.add("INTERLEAVE=" + inter.getList()[((Integer) inter.getDefaultValue()).intValue()]); |
333 |
ParamImpl comp = (ParamImpl)p.getParamById("compression");// GIF LZW, ... |
334 |
if (comp != null) |
335 |
paramList.add("COMPRESS=" + comp.getList()[((Integer) comp.getDefaultValue()).intValue()]); |
336 |
ParamImpl comp1 = (ParamImpl)p.getParamById("compress"); // HFA (YES, NO) |
337 |
if (comp1 != null) |
338 |
paramList.add("COMPRESS=" + comp1.getList()[((Integer) comp1.getDefaultValue()).intValue()]); |
339 |
ParamImpl rrd = (ParamImpl)p.getParamById("rrd");
|
340 |
if (rrd != null) |
341 |
paramList.add("HFA_USE_RRD=" + rrd.getList()[((Integer) rrd.getDefaultValue()).intValue()]); |
342 |
ParamImpl mtw = (ParamImpl)p.getParamById("Mtw");
|
343 |
if (mtw != null) |
344 |
paramList.add("MTW=" + mtw.getList()[((Integer) mtw.getDefaultValue()).intValue()]); |
345 |
ParamImpl tw = (ParamImpl)p.getParamById("Tile Width");
|
346 |
if (tw != null) |
347 |
paramList.add("BLOCKXSIZE=" + tw.getList()[((Integer) tw.getDefaultValue()).intValue()]); |
348 |
ParamImpl th = (ParamImpl)p.getParamById("Tile Height");
|
349 |
if (th != null) |
350 |
paramList.add("BLOCKYSIZE=" + th.getList()[((Integer) th.getDefaultValue()).intValue()]); |
351 |
ParamImpl qt = (ParamImpl)p.getParamById("quality");
|
352 |
if (qt != null) |
353 |
paramList.add("QUALITY=" + qt.getDefaultValue());
|
354 |
ParamImpl prog = (ParamImpl)p.getParamById("progressive");
|
355 |
if (prog != null) |
356 |
paramList.add("PROGRESSIVE=" + prog.getDefaultValue());
|
357 |
|
358 |
if (paramList.size() == 0) |
359 |
return null; |
360 |
|
361 |
String[] result = new String[paramList.size()]; |
362 |
for (int i = 0; i < result.length; i++) |
363 |
result[i] = (String) paramList.get(i);
|
364 |
return result;
|
365 |
} |
366 |
|
367 |
/**
|
368 |
* Escritura de datos tipo Byte.
|
369 |
* @param sizeY Alto del bloque que se escribe.
|
370 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
371 |
*/
|
372 |
public void writeByteBand(int sizeY, int posicionY) { |
373 |
byte[][] buftmp = dataWriter.readByteData(sizeWindowX, sizeY); |
374 |
for(int iBand = 0; iBand < nBands; iBand ++) |
375 |
bufBands[iBand].buffByte = new byte[buftmp[iBand].length]; |
376 |
|
377 |
//Escribimos el bloque destino
|
378 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
379 |
for (int i = 0; i < buftmp[iBand].length; i++) |
380 |
bufBands[iBand].buffByte[i] = buftmp[iBand][i]; |
381 |
|
382 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
383 |
try {
|
384 |
rband = dstDataset.getRasterBand(iBand + 1);
|
385 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Byte);
|
386 |
bufBands[iBand].buffByte = null;
|
387 |
} catch (GdalException e) {
|
388 |
//No se est? escribiendo ...
|
389 |
} |
390 |
buftmp = null;
|
391 |
|
392 |
} |
393 |
|
394 |
/**
|
395 |
* Escritura de datos tipo Short.
|
396 |
* @param sizeY Alto del bloque que se escribe.
|
397 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
398 |
*/
|
399 |
public void writeShortBand(int sizeY, int posicionY) { |
400 |
short[][] buftmp = dataWriter.readShortData(sizeWindowX, sizeY); |
401 |
for(int iBand = 0; iBand < nBands; iBand ++) |
402 |
bufBands[iBand].buffShort = new short[buftmp[iBand].length]; |
403 |
|
404 |
//Escribimos el bloque destino
|
405 |
for (int iBand = 0; iBand < nBands; iBand++) |
406 |
for (int i = 0; i < buftmp[iBand].length; i++) |
407 |
bufBands[iBand].buffShort[i] = buftmp[iBand][i]; |
408 |
|
409 |
for (int iBand = 0; iBand < nBands; iBand++) |
410 |
try {
|
411 |
rband = dstDataset.getRasterBand(iBand + 1);
|
412 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Int16);
|
413 |
bufBands[iBand].buffShort = null;
|
414 |
} catch (GdalException e) {
|
415 |
//No se est? escribiendo ...
|
416 |
} |
417 |
buftmp = null;
|
418 |
} |
419 |
|
420 |
/**
|
421 |
* Escritura de datos tipo Int.
|
422 |
* @param sizeY Alto del bloque que se escribe.
|
423 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
424 |
*/
|
425 |
public void writeIntBand(int sizeY, int posicionY) { |
426 |
int[][] buftmp = dataWriter.readIntData(sizeWindowX, sizeY); |
427 |
for(int iBand = 0; iBand < nBands; iBand ++) |
428 |
bufBands[iBand].buffInt = new int[buftmp[iBand].length]; |
429 |
|
430 |
//Escribimos el bloque destino
|
431 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
432 |
for (int i = 0; i < buftmp[iBand].length; i++) |
433 |
bufBands[iBand].buffInt[i] = buftmp[iBand][i]; |
434 |
|
435 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
436 |
try {
|
437 |
rband = dstDataset.getRasterBand(iBand + 1);
|
438 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Int32);
|
439 |
bufBands[iBand].buffInt = null;
|
440 |
} catch (GdalException e) {
|
441 |
//No se est? escribiendo ...
|
442 |
} |
443 |
buftmp = null;
|
444 |
} |
445 |
|
446 |
/**
|
447 |
* Escritura de datos tipo Float.
|
448 |
* @param sizeY Alto del bloque que se escribe.
|
449 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
450 |
*/
|
451 |
public void writeFloatBand(int sizeY, int posicionY) { |
452 |
float[][] buftmp = dataWriter.readFloatData(sizeWindowX, sizeY); |
453 |
for(int iBand = 0; iBand < nBands; iBand ++) |
454 |
bufBands[iBand].buffFloat = new float[buftmp[iBand].length]; |
455 |
|
456 |
//Escribimos el bloque destino
|
457 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
458 |
for (int i = 0; i < buftmp[iBand].length; i++) |
459 |
bufBands[iBand].buffFloat[i] = buftmp[iBand][i]; |
460 |
|
461 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
462 |
try {
|
463 |
rband = dstDataset.getRasterBand(iBand + 1);
|
464 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Float32);
|
465 |
bufBands[iBand].buffFloat = null;
|
466 |
} catch (GdalException e) {
|
467 |
//No se est? escribiendo ...
|
468 |
} |
469 |
buftmp = null;
|
470 |
} |
471 |
|
472 |
/**
|
473 |
* Escritura de datos tipo Double.
|
474 |
* @param sizeY Alto del bloque que se escribe.
|
475 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
476 |
*/
|
477 |
public void writeDoubleBand(int sizeY, int posicionY) { |
478 |
double[][] buftmp = dataWriter.readDoubleData(sizeWindowX, sizeY); |
479 |
for(int iBand = 0; iBand < nBands; iBand ++) |
480 |
bufBands[iBand].buffDouble = new double[buftmp[iBand].length]; |
481 |
|
482 |
//Escribimos el bloque destino
|
483 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
484 |
for (int i = 0; i < buftmp[iBand].length; i++) |
485 |
bufBands[iBand].buffDouble[i] = buftmp[iBand][i]; |
486 |
|
487 |
for (int iBand = 0; iBand < buftmp.length; iBand++) |
488 |
try {
|
489 |
rband = dstDataset.getRasterBand(iBand + 1);
|
490 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], Gdal.GDT_Float64);
|
491 |
bufBands[iBand].buffDouble = null;
|
492 |
} catch (GdalException e) {
|
493 |
//No se est? escribiendo ...
|
494 |
} |
495 |
buftmp = null;
|
496 |
} |
497 |
/**
|
498 |
* Escritura para tipo de dato ARGB.
|
499 |
* @param sizeY Alto del bloque que se escribe.
|
500 |
* @param posicionY Posici?ny a partir desde donde se comienza.
|
501 |
*/
|
502 |
public void writeARGBBand(int sizeY, int posicionY) |
503 |
throws ProcessInterruptedException, OutOfMemoryError { |
504 |
int[] buftmp = dataWriter.readARGBData(sizeWindowX, sizeY, 0); |
505 |
for(int iBand = 0; iBand < nBands; iBand ++) |
506 |
bufBands[iBand].buffByte = new byte[buftmp.length]; |
507 |
|
508 |
//Escribimos el bloque destino
|
509 |
for (int i = 0; i < buftmp.length; i++) { |
510 |
bufBands[0].buffByte[i] = (byte) (((buftmp[i] & 0xff0000) >> 16) & |
511 |
0xff);
|
512 |
bufBands[1].buffByte[i] = (byte) (((buftmp[i] & 0xff00) >> 8) & 0xff); |
513 |
bufBands[2].buffByte[i] = (byte) ((buftmp[i] & 0xff) & 0xff); |
514 |
} |
515 |
|
516 |
try {
|
517 |
rband = dstDataset.getRasterBand(1);
|
518 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[0], |
519 |
Gdal.GDT_Byte); |
520 |
rband = dstDataset.getRasterBand(2);
|
521 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[1], |
522 |
Gdal.GDT_Byte); |
523 |
rband = dstDataset.getRasterBand(3);
|
524 |
rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[2], |
525 |
Gdal.GDT_Byte); |
526 |
} catch (GdalException e) {
|
527 |
e.printStackTrace(); |
528 |
} |
529 |
|
530 |
bufBands[0].buffByte = null; |
531 |
bufBands[1].buffByte = null; |
532 |
bufBands[2].buffByte = null; |
533 |
} |
534 |
|
535 |
/**
|
536 |
* Escribe tres bandas en el GDALRasterBand desde el DataServerWriter con una
|
537 |
* altura definida por sizeY.
|
538 |
* @param buftmp Buffer
|
539 |
* @param sizeY Altura en pixels del bloque leido
|
540 |
* @param posicionY Posici?n y a partir de la cual se escribe en el GDALRasterBand destino
|
541 |
*/
|
542 |
private void writeBands(int sizeY, int posicionY) |
543 |
throws ProcessInterruptedException, OutOfMemoryError { |
544 |
//leemos el bloque origen
|
545 |
|
546 |
switch(dataType){
|
547 |
case RasterBuffer.TYPE_IMAGE:
|
548 |
writeARGBBand(sizeY, posicionY); |
549 |
break;
|
550 |
case RasterBuffer.TYPE_BYTE:
|
551 |
writeByteBand(sizeY, posicionY); |
552 |
break;
|
553 |
case RasterBuffer.TYPE_SHORT:
|
554 |
writeShortBand(sizeY, posicionY); |
555 |
break;
|
556 |
case RasterBuffer.TYPE_INT:
|
557 |
writeIntBand(sizeY, posicionY); |
558 |
break;
|
559 |
case RasterBuffer.TYPE_FLOAT:
|
560 |
writeFloatBand(sizeY, posicionY); |
561 |
break;
|
562 |
case RasterBuffer.TYPE_DOUBLE:
|
563 |
writeDoubleBand(sizeY, posicionY); |
564 |
break;
|
565 |
} |
566 |
} |
567 |
|
568 |
/**
|
569 |
* Funci?n que gestiona la lectura desde el origen y la escritura
|
570 |
* de Gdal sobre el fichero destino.
|
571 |
* @param mode Modo de escritura
|
572 |
* @throws IOException
|
573 |
*/
|
574 |
private void write(int mode) throws IOException, ProcessInterruptedException, |
575 |
OutOfMemoryError {
|
576 |
RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + ""); |
577 |
bufBands = new GdalBuffer[nBands];
|
578 |
for(int iBand = 0; iBand < nBands; iBand ++) |
579 |
bufBands[iBand] = new GdalBuffer();
|
580 |
|
581 |
int blockSize = RasterLibrary.blockHeight;
|
582 |
if(dataWriter instanceof DefaultDataServerWriter) { |
583 |
Buffer source = ((DefaultDataServerWriter)dataWriter).getSource();
|
584 |
if(source.isCached()) {
|
585 |
blockSize = source.getBlockHeight(); |
586 |
} |
587 |
} |
588 |
|
589 |
percent = 0;
|
590 |
nBlocks = (sizeWindowY / blockSize); |
591 |
double increment = (blockSize * 100) / (double)sizeWindowY; |
592 |
|
593 |
if (mode == DefaultRasterWriter.MODE_DATAWRITE)
|
594 |
for (int iBlock = 0; iBlock < nBlocks; iBlock++) { |
595 |
if(task.getEvent() != null) |
596 |
task.manageEvent(task.getEvent()); |
597 |
int posicionY = iBlock * blockSize;
|
598 |
if(write)
|
599 |
writeBands( blockSize, posicionY); |
600 |
percent = (int)((iBlock + 1) * increment); |
601 |
} |
602 |
|
603 |
if (anchoResto != 0) |
604 |
if (mode == DefaultRasterWriter.MODE_DATAWRITE) {
|
605 |
if(task.getEvent() != null) |
606 |
task.manageEvent(task.getEvent()); |
607 |
int posicionY = nBlocks * blockSize;
|
608 |
if(write)
|
609 |
writeBands(anchoResto, posicionY); |
610 |
percent = (int)(nBlocks * increment);
|
611 |
} |
612 |
|
613 |
} |
614 |
|
615 |
/**
|
616 |
* Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
|
617 |
* @throws IOException
|
618 |
*/
|
619 |
public void fileWrite() throws IOException, ProcessInterruptedException { |
620 |
write(DefaultRasterWriter.MODE_FILEWRITE); |
621 |
} |
622 |
|
623 |
/**
|
624 |
* Realiza una copia en el formato especificado.
|
625 |
* @throws IOException
|
626 |
*/
|
627 |
public static void createCopy(GdalDriver driverDst, String dst, String src, |
628 |
boolean bstrict, String[] params) throws IOException, GdalException { |
629 |
if (dst == null || src == null) |
630 |
throw new IOException("No se ha asignado un fichero de entrada."); |
631 |
|
632 |
GdalProvider gdalFile; |
633 |
try {
|
634 |
GdalDataParameters par = new GdalDataParameters();
|
635 |
par.setFile(new File(src)); |
636 |
gdalFile = new GdalProvider(par, null); |
637 |
Gdal dstDataset = driverDst.createCopy(dst, gdalFile.getNative(), bstrict, params); |
638 |
if( dst.endsWith(".jpg") || |
639 |
dst.endsWith(".jpeg") ||
|
640 |
dst.endsWith(".png"))
|
641 |
RasterLocator.getManager().getFileUtils().createWorldFile(dst, gdalFile.getExtent(), (int)gdalFile.getWidth(), (int)gdalFile.getHeight()); |
642 |
gdalFile.close(); |
643 |
dstDataset.close(); |
644 |
} catch (NotSupportedExtensionException e) {
|
645 |
e.printStackTrace(); |
646 |
} |
647 |
} |
648 |
|
649 |
/**
|
650 |
* Realiza la escritura de datos con los datos que le pasa el cliente.
|
651 |
* @throws IOException
|
652 |
* @throws RmfSerializerException
|
653 |
*/
|
654 |
public void dataWrite() throws IOException, ProcessInterruptedException, |
655 |
OutOfMemoryError {
|
656 |
if (dataWriter == null) |
657 |
throw new IOException("No se ha obtenido un objeto de entrada para la escritura valido."); |
658 |
|
659 |
write(DefaultRasterWriter.MODE_DATAWRITE); |
660 |
|
661 |
if (driverParams.getParamById("tfw") != null && |
662 |
((ParamImpl)driverParams.getParamById("tfw")).getDefaultValue() instanceof Boolean && |
663 |
((Boolean) ((ParamImpl)driverParams.getParamById("tfw")).getDefaultValue()).booleanValue() == true) |
664 |
if (at != null) |
665 |
fileUtil.createWorldFile(this.outFileName, at, sizeWindowX, sizeWindowY);
|
666 |
|
667 |
if (colorInterp != null) |
668 |
try {
|
669 |
RasterLocator.getManager().getProviderServices().saveObjectToRmfFile(outFileName, DataStoreColorInterpretation.class, colorInterp); |
670 |
} catch (RmfSerializerException e) {
|
671 |
throw new IOException("No se ha podido guardar la interpretacion de color"); |
672 |
} |
673 |
} |
674 |
|
675 |
/**
|
676 |
* Cancela el salvado de datos.
|
677 |
* @throws GdalException
|
678 |
*/
|
679 |
public void writeClose() { |
680 |
try {
|
681 |
if(bufBands != null) { |
682 |
for (int i = 0; i < bufBands.length; i++) { |
683 |
bufBands[i].buffAPalette = null;
|
684 |
bufBands[i].buffBPalette = null;
|
685 |
bufBands[i].buffByte = null;
|
686 |
bufBands[i].buffShort = null;
|
687 |
bufBands[i].buffInt = null;
|
688 |
bufBands[i].buffFloat = null;
|
689 |
bufBands[i].buffDouble = null;
|
690 |
} |
691 |
} |
692 |
|
693 |
if(dstDataset != null) |
694 |
dstDataset.close(); |
695 |
} catch (GdalException e) {
|
696 |
e.printStackTrace(); |
697 |
} |
698 |
} |
699 |
|
700 |
/**
|
701 |
* Cancela el salvado de datos.
|
702 |
*/
|
703 |
public void writeCancel() { |
704 |
write = false;
|
705 |
} |
706 |
|
707 |
/**
|
708 |
* Obtiene el valor a la variable write que estar? a true cuando se est? escribiendo
|
709 |
* o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
|
710 |
* pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
|
711 |
* de salida.
|
712 |
* @return True si puede escribirse y false si no puede
|
713 |
*/
|
714 |
public boolean isWrite() { |
715 |
return write;
|
716 |
} |
717 |
|
718 |
/**
|
719 |
* Asigna el valor a la variable write que estar? a true cuando se est? escribiendo
|
720 |
* o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
|
721 |
* pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
|
722 |
* de salida.
|
723 |
* @param write Variable booleana. True si puede escribirse y false si no puede
|
724 |
*/
|
725 |
public void setWrite(boolean write) { |
726 |
this.write = write;
|
727 |
} |
728 |
|
729 |
/**
|
730 |
* Asigna los par?metros del driver modificados por el cliente.
|
731 |
* @param Params
|
732 |
*/
|
733 |
public void setParams(Params params) { |
734 |
if(params instanceof ParamsImpl) |
735 |
this.driverParams = (ParamsImpl)params;
|
736 |
else
|
737 |
return;
|
738 |
|
739 |
int blockSize = 256; |
740 |
try {
|
741 |
ParamImpl param = (ParamImpl)driverParams.getParamById("blocksize");
|
742 |
blockSize = Integer.parseInt(param.getList()[((Integer)param.getDefaultValue()).intValue()]); |
743 |
nBlocks = (sizeWindowY / blockSize); |
744 |
anchoResto = sizeWindowY - (nBlocks * blockSize); |
745 |
}catch(NumberFormatException e) { |
746 |
//Se queda con el valor de inicializaci?n
|
747 |
} |
748 |
} |
749 |
|
750 |
/*
|
751 |
* (non-Javadoc)
|
752 |
* @see java.lang.Object#finalize()
|
753 |
*/
|
754 |
protected void finalize() throws Throwable { |
755 |
drv = null;
|
756 |
dstDataset = null;
|
757 |
rband = null;
|
758 |
geot = null;
|
759 |
if(bufBands != null) { |
760 |
for (int i = 0; i < bufBands.length; i++) { |
761 |
bufBands[i].buffAPalette = null;
|
762 |
bufBands[i].buffBPalette = null;
|
763 |
bufBands[i].buffByte = null;
|
764 |
bufBands[i].buffShort = null;
|
765 |
bufBands[i].buffInt = null;
|
766 |
bufBands[i].buffFloat = null;
|
767 |
bufBands[i].buffDouble = null;
|
768 |
} |
769 |
} |
770 |
super.finalize();
|
771 |
} |
772 |
} |