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