Statistics
| Revision:

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 @ 5135

History | View | Annotate | Download (26.8 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.sql.Savepoint;
29
import java.util.ArrayList;
30

    
31
import org.apache.commons.io.FileUtils;
32
import org.apache.commons.io.FilenameUtils;
33
import org.cresques.cts.ICRSFactory;
34
import org.cresques.cts.IProjection;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.fmap.dal.coverage.RasterLibrary;
39
import org.gvsig.fmap.dal.coverage.RasterLocator;
40
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
41
import org.gvsig.fmap.dal.coverage.datastruct.Params;
42
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
43
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
44
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
45
import org.gvsig.fmap.dal.coverage.store.DataServerWriter;
46
import org.gvsig.jgdal.GdalBuffer;
47
import org.gvsig.jgdal.GdalDataset;
48
import org.gvsig.jgdal.GdalDriver;
49
import org.gvsig.jgdal.GdalException;
50
import org.gvsig.jgdal.GdalRasterBand;
51
import org.gvsig.jgdal.GeoTransform;
52
import org.gvsig.raster.gdal.io.features.BMPFeatures;
53
import org.gvsig.raster.gdal.io.features.GTiffFeatures;
54
import org.gvsig.raster.gdal.io.features.HFAFeatures;
55
import org.gvsig.raster.gdal.io.features.IDRISIFeatures;
56
import org.gvsig.raster.gdal.io.features.ILWIS_MprFeatures;
57
import org.gvsig.raster.gdal.io.features.Jpeg2000Features;
58
import org.gvsig.raster.gdal.io.features.PNM_PgmFeatures;
59
import org.gvsig.raster.gdal.io.features.PNM_PpmFeatures;
60
import org.gvsig.raster.impl.buffer.DefaultDataServerWriter;
61
import org.gvsig.raster.impl.buffer.RasterBuffer;
62
import org.gvsig.raster.impl.process.RasterTask;
63
import org.gvsig.raster.impl.process.RasterTaskQueue;
64
import org.gvsig.raster.impl.store.ParamImpl;
65
import org.gvsig.raster.impl.store.ParamsImpl;
66
import org.gvsig.raster.impl.store.WriteFileFormatFeatures;
67
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
68
import org.gvsig.raster.impl.store.writer.DefaultRasterWriter;
69
import org.gvsig.raster.util.DefaultProviderServices;
70
import org.gvsig.tools.ToolsLocator;
71
import org.gvsig.tools.extensionpoint.ExtensionPoint;
72
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
73

    
74

    
75
/**
76
 * Driver para la escritura a trav?s de Gdal.
77
 * Puede exportar un fichero de un formato a otro desde un GeoRasterFile
78
 * en cualquier formato soportado por la lectura a un formato que este incluido
79
 * en la lista supportedDrv.
80
 *
81
 * Puede salvar a disco en un formato que este incluido en la lista supportedDrv
82
 * obteniendo los datos que van siendo servidos desde el cliente. Este cliente
83
 * debe implementar un DataServerWriter o tener un objeto que lo implemente. Inicialmente
84
 * le pasar? los par?metros de la imagen de salida y cuando el driver comience a
85
 * escribir le ir? solicitando m?s a trav?s del m?todo readData de DataServerWriter.
86
 * El cliente ser? el que lleve el control de lo que va sirviendo y lo que le queda
87
 * por servir.
88
 * @author Nacho Brodin (nachobrodin@gmail.com)
89
 */
90
public class GdalWriter extends DefaultRasterWriter {
91

    
92
    private static final Logger logger = LoggerFactory.getLogger(GdalWriter.class);
93

    
94
        /**
95
         *
96
         */
97
        public static void register() {
98
                DefaultProviderServices pInfo = (DefaultProviderServices)RasterLocator.getManager().getProviderServices();
99
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
100
                ExtensionPoint point = extensionPoints.get("RasterWriter");
101

    
102
                point.append("tif", "", GdalWriter.class);
103
                pInfo.getFileFeature().put("tif", new GTiffFeatures());
104

    
105
                point.append("img", "", GdalWriter.class);
106
                pInfo.getFileFeature().put("img", new HFAFeatures());
107

    
108
                point.append("bmp", "", GdalWriter.class);
109
                pInfo.getFileFeature().put("bmp", new BMPFeatures());
110

    
111
                point.append("pgm", "", GdalWriter.class);
112
                pInfo.getFileFeature().put("pgm", new PNM_PgmFeatures());
113

    
114
                point.append("ppm", "", GdalWriter.class);
115
                pInfo.getFileFeature().put("ppm", new PNM_PpmFeatures());
116

    
117
                point.append("mpl", "", GdalWriter.class);
118
                pInfo.getFileFeature().put("mpl", new ILWIS_MprFeatures());
119

    
120
                point.append("rst", "", GdalWriter.class);
121
                pInfo.getFileFeature().put("rst", new IDRISIFeatures());
122

    
123
                point.append("jp2", "", GdalWriter.class);
124
                pInfo.getFileFeature().put("jp2", new Jpeg2000Features());
125

    
126
                //La exportaci?n no es correcta del todo
127
//                point.append("rmf", "", GdalWriter.class);
128
//                pInfo.getFileFeature().put("rmf", new RMFFeatures());
129

    
130
                //No salva datos. Siempre sale negra la imagen
131
                //point.register("aux", GdalWriter.class);
132
                //fileFeature.put("aux", new PAuxFeatures());
133
        }
134

    
135
        private GdalDriver                                 drv;
136
        private GdalDataset                                                         dstDataset = null;
137
        private GdalRasterBand                                         rband = null;
138
        private GeoTransform                                         geot = null; //Datos de georeferenciaci?n
139
        //private OGRSpatialReference                         oSRS; //Datos de proyecci?n
140
        private GdalBuffer[]                                        bufBands = null;
141
        private int                                                         nBlocks = 0; //N?mero de bloques en Y en el que se divide la imagen para escribir
142
        private int                                                         anchoResto = 0; //Tama?o del ?ltimo bloque de la imagen.
143
        private boolean                                                        write = true; //Cuando est? a true se puede escribir en la imagen de salida. Si est? a false el proceso es interrumpido
144
        private int                                                         dataType = RasterBuffer.TYPE_UNDEFINED;
145

    
146
        /**
147
         * Carga los par?metros de este driver.
148
         * @param ident
149
         */
150
        public void loadParams(String ident) {
151
                WriteFileFormatFeatures wfff = (WriteFileFormatFeatures)pInfo.getFileFeature().get(ident);
152
                wfff.loadParams();
153
                driverParams = (ParamsImpl)wfff.getParams();
154
        }
155

    
156
        public String getProviderName() {
157
                return GdalProvider.NAME;
158
        }
159

    
160
        /**
161
         * Constructor para la obtenci?n de par?metros del driver
162
         * @param fileName
163
         */
164
        public GdalWriter(String fileName) {
165
                ident = fileUtil.getExtensionFromFileName(fileName);
166
                driver = ((WriteFileFormatFeatures)pInfo.getFileFeature().get(ident)).getDriverName();
167

    
168
                loadParams(ident);
169
        }
170

    
171
        /**
172
         * Constructor para salvar datos servidos por el cliente
173
         * @param dataWriter               Objeto servidor de datos para el driver de escritura
174
         * @param outFileName              Fichero de salida
175
         * @param nBands            N?mero de bandas
176
         * @param at                Transformaci?n afin
177
         * @param outSizeX                          Tama?o de salida en X
178
         * @param outSizeY                        Tama?o de salida en Y
179
         * @param dataType                        Tipo de dato
180
         * @param params
181
         * @param proj
182
         * @throws GdalException
183
         * @throws IOException
184
         */
185
        public GdalWriter(        DataServerWriter dataWriter,
186
                        String outFileName,
187
                        Integer nBands,
188
                        AffineTransform at,
189
                        Integer outSizeX,
190
                        Integer outSizeY,
191
                        Integer dataType,
192
                        Params params,
193
                        IProjection proj) throws GdalException, IOException {
194
                this(dataWriter, outFileName, nBands, at, outSizeX, outSizeY, dataType, params, proj, new Boolean(true));
195
        }
196

    
197
        /**
198
         * Constructor para salvar datos servidos por el cliente
199
         * @param dataWriter               Objeto servidor de datos para el driver de escritura
200
         * @param outFileName
201
         * @param nBands
202
         * @param at
203
         * @param outFilename              Fichero de salida
204
         * @param outSizeX                          Tama?o de salida en X
205
         * @param outSizeY                        Tama?o de salida en Y
206
         * @param dataType                        Tipo de dato
207
         * @param params
208
         * @param proj
209
         * @param geo                                Flag que dice si se salva con georreferenciaci?n o sin ella
210
         * @throws GdalException
211
         * @throws IOException
212
         */
213
        public GdalWriter(        DataServerWriter dataWriter,
214
                        String outFileName,
215
                        Integer nBands,
216
                        AffineTransform at,
217
                        Integer outSizeX,
218
                        Integer outSizeY,
219
                        Integer dataType,
220
                        Params params,
221
                        IProjection proj,
222
                        Boolean geo)throws GdalException, IOException {
223

    
224
                this.proj = proj;
225
                ident = outFileName.toLowerCase().substring(outFileName.lastIndexOf(".") + 1);
226
                driver = ((WriteFileFormatFeatures)pInfo.getFileFeature().get(ident)).getDriverName();
227
                this.dataType = dataType.intValue();
228
                this.at = at;
229
                percent = 0;
230

    
231
                this.dataWriter = dataWriter;
232
                this.outFileName = outFileName;
233

    
234
                this.sizeWindowX = outSizeX.intValue();
235
                this.sizeWindowY = outSizeY.intValue();
236

    
237
                if ((sizeWindowX < 0) || (sizeWindowY < 0))
238
                        throw new IOException("Tama?o del fichero de salida erroneo.");
239

    
240
                this.nBands = nBands.intValue();
241

    
242
                //Calculamos la georeferenciaci?n a partir del extend pasado por el cliente.
243

    
244
                geot = new GeoTransform();
245
                geot.adfgeotransform[0] = at.getTranslateX();
246
                geot.adfgeotransform[3] = at.getTranslateY();
247
                geot.adfgeotransform[1] = at.getScaleX();
248
                geot.adfgeotransform[5] = at.getScaleY();
249
                geot.adfgeotransform[2] = at.getShearX();
250
                geot.adfgeotransform[4] = at.getShearY();
251

    
252
                String outRmf = outFileName.substring(0, outFileName.lastIndexOf("."));
253
                if(geo.booleanValue())
254
                        rasterUtil.saveGeoInfo(outRmf, at, new Point2D.Double(sizeWindowX, sizeWindowY));
255

    
256
                if(params == null)
257
                        loadParams(ident);
258
                else
259
                        if(params instanceof ParamsImpl)
260
                                this.driverParams = (ParamsImpl)params;
261

    
262
                init();
263
        }
264

    
265
        /**
266
         * A?ade la proyecci?n Wkt con la que salvar.
267
         * @param wkt
268
         * @throws GdalException
269
         */
270
        public void setWkt(String wkt) {
271
                if(dstDataset != null && wkt != null && wkt.compareTo("unknown") != 0)
272
                        try {
273
                                dstDataset.setProjection(wkt);
274
                        } catch (GdalException e) {
275
                                System.err.println("Proyecci?n Wkt no asignada en GdalWriter");
276
                                return;
277
                        }
278
        }
279

    
280
        /**
281
         * Asigna el tipo de driver con el que se salvar? la imagen
282
         * @param drvType        Tipo de driver
283
         */
284
        public void setDriverType(String drvType) {
285
                this.driver = drvType;
286
        }
287

    
288
        /**
289
         * Creaci?n del dataset de destino.
290
         * @throws EcwException
291
         */
292
        private void init() throws GdalException {
293
                //Controlamos que el tipo de driver sea correcto
294
                if (driver == null)
295
                        throw new GdalException("Tipo de driver sin especificar.");
296

    
297
                boolean okdrvtype = false;
298

    
299
                String[] types = pInfo.getWriteDriversType();
300
                for (int i = 0; i < pInfo.getWriteNTypes(); i++)
301
                        if (driver.equals(types[i]))
302
                                okdrvtype = true;
303

    
304
                if (okdrvtype == false)
305
                        throw new GdalException("El tipo de driver " + driver + " no est? soportado por GdalWriter.");
306

    
307
                //Obtenemos el driver y creamos el dataset del destino
308
                drv = GdalDataset.getDriverByName(driver);
309

    
310
                if (dstDataset != null) {
311
                        dstDataset.close();
312
                        dstDataset = null;
313
                }
314

    
315
                dstDataset = drv.create(outFileName, sizeWindowX, sizeWindowY,
316
                                nBands, GdalDataSource.getGdalTypeFromRasterBufType(dataType), gdalParamsFromRasterParams(driverParams));
317

    
318
                dstDataset.setGeoTransform(geot);
319

    
320
                int blockSize = RasterLibrary.blockHeight;
321
                if(dataWriter.getBuffer() != null && dataWriter.getBuffer().isCached())
322
                        blockSize = dataWriter.getBuffer().getBlockHeight();
323

    
324
                nBlocks = (sizeWindowY / blockSize);
325
                anchoResto = sizeWindowY - (nBlocks * blockSize);
326
        }
327

    
328
        /**
329
         * @param fileName
330
         * @throws GdalException
331
         */
332
        public void anotherFile(String fileName) throws GdalException {
333
                dstDataset = drv.create(fileName, sizeWindowX, sizeWindowY,
334
                                nBands, GdalDataSource.getGdalTypeFromRasterBufType(dataType), gdalParamsFromRasterParams(driverParams));
335
        }
336

    
337
        /**
338
         * Convierte los par?metros obtenidos desde el objeto params a parametros
339
         * comprensibles por la librer?a gdal
340
         * @param p Params
341
         * @return Array de par?metros
342
         */
343
        public String[] gdalParamsFromRasterParams(Params p) {
344
                if (p == null)
345
                        return null;
346
                ArrayList<String> paramList = new ArrayList<String>();
347
                ParamImpl phot = (ParamImpl)p.getParamById("photometric");
348
                if (phot != null)
349
                        paramList.add("PHOTOMETRIC=" + phot.getList()[((Integer) phot.getDefaultValue()).intValue()]);
350
                ParamImpl inter = (ParamImpl)p.getParamById("interleave");
351
                if (inter != null)
352
                        paramList.add("INTERLEAVE=" + inter.getList()[((Integer) inter.getDefaultValue()).intValue()]);
353
                ParamImpl comp = (ParamImpl)p.getParamById("compression");// GIF LZW, ...
354
                if (comp != null)
355
                        paramList.add("COMPRESS=" + comp.getList()[((Integer) comp.getDefaultValue()).intValue()]);
356
                ParamImpl comp1 = (ParamImpl)p.getParamById("compress"); // HFA (YES, NO)
357
                if (comp1 != null)
358
                        paramList.add("COMPRESS=" + comp1.getList()[((Integer) comp1.getDefaultValue()).intValue()]);
359
                ParamImpl rrd = (ParamImpl)p.getParamById("rrd");
360
                if (rrd != null)
361
                        paramList.add("HFA_USE_RRD=" + rrd.getList()[((Integer) rrd.getDefaultValue()).intValue()]);
362
                ParamImpl mtw = (ParamImpl)p.getParamById("Mtw");
363
                if (mtw != null)
364
                        paramList.add("MTW=" + mtw.getList()[((Integer) mtw.getDefaultValue()).intValue()]);
365
                ParamImpl tw = (ParamImpl)p.getParamById("Tile Width");
366
                if (tw != null)
367
                        paramList.add("BLOCKXSIZE=" + tw.getList()[((Integer) tw.getDefaultValue()).intValue()]);
368
                ParamImpl th = (ParamImpl)p.getParamById("Tile Height");
369
                if (th != null)
370
                        paramList.add("BLOCKYSIZE=" + th.getList()[((Integer) th.getDefaultValue()).intValue()]);
371
                ParamImpl qt = (ParamImpl)p.getParamById("quality");
372
                if (qt != null)
373
                        paramList.add("QUALITY=" + qt.getDefaultValue());
374
                ParamImpl prog = (ParamImpl)p.getParamById("progressive");
375
                if (prog != null)
376
                        paramList.add("PROGRESSIVE=" + prog.getDefaultValue());
377

    
378
                if (paramList.size() == 0)
379
                        return null;
380

    
381
                String[] result = new String[paramList.size()];
382
                for (int i = 0; i < result.length; i++)
383
                        result[i] = (String) paramList.get(i);
384
                return result;
385
        }
386

    
387
        /**
388
         * Escritura de datos tipo Byte.
389
         * @param sizeY Alto del bloque que se escribe.
390
         * @param posicionY Posici?ny a partir desde donde se comienza.
391
         */
392
        public void writeByteBand(int sizeY, int posicionY) {
393
                byte[][] buftmp = dataWriter.readByteData(sizeWindowX, sizeY);
394
                for(int iBand = 0; iBand < nBands; iBand ++)
395
                        bufBands[iBand].buffByte = new byte[buftmp[iBand].length];
396

    
397
                //Escribimos el bloque destino
398
                for (int iBand = 0; iBand < buftmp.length; iBand++)
399
                        for (int i = 0; i < buftmp[iBand].length; i++)
400
                                bufBands[iBand].buffByte[i] = buftmp[iBand][i];
401

    
402
                for (int iBand = 0; iBand < buftmp.length; iBand++)
403
                        try {
404
                                rband = dstDataset.getRasterBand(iBand + 1);
405
                                rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Byte);
406
                                bufBands[iBand].buffByte = null;
407
                        } catch (GdalException e) {
408
                                //No se est? escribiendo ...
409
                        }
410
                buftmp = null;
411

    
412
        }
413

    
414
        /**
415
         * Escritura de datos tipo Short.
416
         * @param sizeY Alto del bloque que se escribe.
417
         * @param posicionY Posici?ny a partir desde donde se comienza.
418
         */
419
        public void writeShortBand(int sizeY, int posicionY) {
420
                short[][] buftmp = dataWriter.readShortData(sizeWindowX, sizeY);
421
                for(int iBand = 0; iBand < nBands; iBand ++)
422
                        bufBands[iBand].buffShort = new short[buftmp[iBand].length];
423

    
424
                //Escribimos el bloque destino
425
                for (int iBand = 0; iBand < nBands; iBand++)
426
                        for (int i = 0; i < buftmp[iBand].length; i++)
427
                                bufBands[iBand].buffShort[i] = buftmp[iBand][i];
428

    
429
                for (int iBand = 0; iBand < nBands; iBand++)
430
                        try {
431
                                rband = dstDataset.getRasterBand(iBand + 1);
432
                                rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Int16);
433
                                bufBands[iBand].buffShort = null;
434
                        } catch (GdalException e) {
435
                                //No se est? escribiendo ...
436
                        }
437
                buftmp = null;
438
        }
439

    
440
        /**
441
         * Escritura de datos tipo Int.
442
         * @param sizeY Alto del bloque que se escribe.
443
         * @param posicionY Posici?ny a partir desde donde se comienza.
444
         */
445
        public void writeIntBand(int sizeY, int posicionY) {
446
                int[][] buftmp = dataWriter.readIntData(sizeWindowX, sizeY);
447
                for(int iBand = 0; iBand < nBands; iBand ++)
448
                        bufBands[iBand].buffInt = new int[buftmp[iBand].length];
449

    
450
                //Escribimos el bloque destino
451
                for (int iBand = 0; iBand < buftmp.length; iBand++)
452
                        for (int i = 0; i < buftmp[iBand].length; i++)
453
                                bufBands[iBand].buffInt[i] = buftmp[iBand][i];
454

    
455
                for (int iBand = 0; iBand < buftmp.length; iBand++)
456
                        try {
457
                                rband = dstDataset.getRasterBand(iBand + 1);
458
                                rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Int32);
459
                                bufBands[iBand].buffInt = null;
460
                        } catch (GdalException e) {
461
                                //No se est? escribiendo ...
462
                        }
463
                buftmp = null;
464
        }
465

    
466
        /**
467
         * Escritura de datos tipo Float.
468
         * @param sizeY Alto del bloque que se escribe.
469
         * @param posicionY Posici?ny a partir desde donde se comienza.
470
         */
471
        public void writeFloatBand(int sizeY, int posicionY) {
472
                float[][] buftmp = dataWriter.readFloatData(sizeWindowX, sizeY);
473
                for(int iBand = 0; iBand < nBands; iBand ++)
474
                        bufBands[iBand].buffFloat = new float[buftmp[iBand].length];
475

    
476
                //Escribimos el bloque destino
477
                for (int iBand = 0; iBand < buftmp.length; iBand++)
478
                        for (int i = 0; i < buftmp[iBand].length; i++)
479
                                bufBands[iBand].buffFloat[i] = buftmp[iBand][i];
480

    
481
                for (int iBand = 0; iBand < buftmp.length; iBand++)
482
                        try {
483
                                rband = dstDataset.getRasterBand(iBand + 1);
484
                                rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Float32);
485
                                bufBands[iBand].buffFloat = null;
486
                        } catch (GdalException e) {
487
                                //No se est? escribiendo ...
488
                        }
489
                buftmp = null;
490
        }
491

    
492
        /**
493
         * Escritura de datos tipo Double.
494
         * @param sizeY Alto del bloque que se escribe.
495
         * @param posicionY Posici?ny a partir desde donde se comienza.
496
         */
497
        public void writeDoubleBand(int sizeY, int posicionY) {
498
                double[][] buftmp = dataWriter.readDoubleData(sizeWindowX, sizeY);
499
                for(int iBand = 0; iBand < nBands; iBand ++)
500
                        bufBands[iBand].buffDouble = new double[buftmp[iBand].length];
501

    
502
                //Escribimos el bloque destino
503
                for (int iBand = 0; iBand < buftmp.length; iBand++)
504
                        for (int i = 0; i < buftmp[iBand].length; i++)
505
                                bufBands[iBand].buffDouble[i] = buftmp[iBand][i];
506

    
507
                for (int iBand = 0; iBand < buftmp.length; iBand++)
508
                        try {
509
                                rband = dstDataset.getRasterBand(iBand + 1);
510
                                rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[iBand], GdalDataset.GDT_Float64);
511
                                bufBands[iBand].buffDouble = null;
512
                        } catch (GdalException e) {
513
                                //No se est? escribiendo ...
514
                        }
515
                buftmp = null;
516
        }
517
        /**
518
         * Escritura para tipo de dato ARGB.
519
         * @param sizeY Alto del bloque que se escribe.
520
         * @param posicionY Posici?ny a partir desde donde se comienza.
521
         * @throws ProcessInterruptedException
522
         * @throws OutOfMemoryError
523
         */
524
        public void writeARGBBand(int sizeY, int posicionY)
525
                        throws ProcessInterruptedException, OutOfMemoryError {
526
                int[] buftmp = dataWriter.readARGBData(sizeWindowX, sizeY, 0);
527
                for(int iBand = 0; iBand < nBands; iBand ++)
528
                        bufBands[iBand].buffByte = new byte[buftmp.length];
529

    
530
                //Escribimos el bloque destino
531
                for (int i = 0; i < buftmp.length; i++) {
532
                        bufBands[0].buffByte[i] = (byte) (((buftmp[i] & 0xff0000) >> 16) &
533
                                        0xff);
534
                        bufBands[1].buffByte[i] = (byte) (((buftmp[i] & 0xff00) >> 8) & 0xff);
535
                        bufBands[2].buffByte[i] = (byte) ((buftmp[i] & 0xff) & 0xff);
536
                }
537

    
538
                try {
539
                        rband = dstDataset.getRasterBand(1);
540
                        rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[0],
541
                                        GdalDataset.GDT_Byte);
542
                        rband = dstDataset.getRasterBand(2);
543
                        rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[1],
544
                                        GdalDataset.GDT_Byte);
545
                        rband = dstDataset.getRasterBand(3);
546
                        rband.writeRaster(0, posicionY, sizeWindowX, sizeY, bufBands[2],
547
                                        GdalDataset.GDT_Byte);
548
                } catch (GdalException e) {
549
                        e.printStackTrace();
550
                }
551

    
552
                bufBands[0].buffByte = null;
553
                bufBands[1].buffByte = null;
554
                bufBands[2].buffByte = null;
555
        }
556

    
557
        /**
558
         * Escribe tres bandas en el GDALRasterBand desde el DataServerWriter con una
559
         * altura definida por sizeY.
560
         * @param buftmp        Buffer
561
         * @param sizeY        Altura en pixels del bloque leido
562
         * @param posicionY        Posici?n y a partir de la cual se escribe en el GDALRasterBand destino
563
         */
564
        private void writeBands(int sizeY, int posicionY)
565
                        throws ProcessInterruptedException, OutOfMemoryError {
566
                //leemos el bloque origen
567

    
568
                switch(dataType){
569
                case RasterBuffer.TYPE_IMAGE:
570
                        writeARGBBand(sizeY, posicionY);
571
                        break;
572
                case RasterBuffer.TYPE_BYTE:
573
                        writeByteBand(sizeY, posicionY);
574
                        break;
575
                case RasterBuffer.TYPE_SHORT:
576
                        writeShortBand(sizeY, posicionY);
577
                        break;
578
                case RasterBuffer.TYPE_INT:
579
                        writeIntBand(sizeY, posicionY);
580
                        break;
581
                case RasterBuffer.TYPE_FLOAT:
582
                        writeFloatBand(sizeY, posicionY);
583
                        break;
584
                case RasterBuffer.TYPE_DOUBLE:
585
                        writeDoubleBand(sizeY, posicionY);
586
                        break;
587
                }
588
        }
589

    
590
        /**
591
         * Funci?n que gestiona la lectura desde el origen y la escritura
592
         * de Gdal sobre el fichero destino.
593
         * @param mode        Modo de escritura
594
         * @throws IOException
595
         */
596
        private void write(int mode) throws IOException, ProcessInterruptedException,
597
                        OutOfMemoryError {
598
                RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + "");
599
                bufBands = new GdalBuffer[nBands];
600
                for(int iBand = 0; iBand < nBands; iBand ++)
601
                        bufBands[iBand] = new GdalBuffer();
602

    
603
                int blockSize = RasterLibrary.blockHeight;
604
                if(dataWriter instanceof DefaultDataServerWriter) {
605
                        Buffer source = ((DefaultDataServerWriter)dataWriter).getSource();
606
                        if(source.isCached()) {
607
                                blockSize = source.getBlockHeight();
608
                        }
609
                }
610

    
611
                percent = 0;
612
                nBlocks = (sizeWindowY / blockSize);
613
                double increment = (blockSize * 100) / (double)sizeWindowY;
614

    
615
                if (mode == DefaultRasterWriter.MODE_DATAWRITE)
616
                        for (int iBlock = 0; iBlock < nBlocks; iBlock++) {
617
                                if(task.getEvent() != null)
618
                                        task.manageEvent(task.getEvent());
619
                                int posicionY = iBlock * blockSize;
620
                                if(write)
621
                                        writeBands( blockSize, posicionY);
622
                                percent = (int)((iBlock + 1) * increment);
623
                        }
624

    
625
                if (anchoResto != 0)
626
                        if (mode == DefaultRasterWriter.MODE_DATAWRITE) {
627
                                if(task.getEvent() != null)
628
                                        task.manageEvent(task.getEvent());
629
                                int posicionY = nBlocks * blockSize;
630
                                if(write)
631
                                        writeBands(anchoResto, posicionY);
632
                                percent = (int)(nBlocks * increment);
633
                        }
634

    
635
        }
636

    
637
        /**
638
         * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
639
         * @throws IOException
640
         */
641
        public void fileWrite() throws IOException, ProcessInterruptedException {
642
                write(DefaultRasterWriter.MODE_FILEWRITE);
643
        }
644

    
645
        /**
646
         * Realiza una copia en el formato especificado.
647
         * @param driverDst
648
         * @param dst
649
         * @param src
650
         * @param bstrict
651
         * @param params
652
         * @throws IOException
653
         * @throws GdalException
654
         */
655
        public static void createCopy(GdalDriver driverDst, String dst, String src,
656
                        boolean bstrict, String[] params) throws IOException, GdalException {
657
                if (dst == null || src == null)
658
                        throw new IOException("No se ha asignado un fichero de entrada.");
659

    
660
                GdalProvider gdalFile;
661
                try {
662
                        GdalDataParameters par = new GdalDataParameters();
663
                        par.setFile(new File(src));
664
                        gdalFile = new GdalProvider(par, null);
665
                        GdalDataset dstDataset = driverDst.createCopy(dst, gdalFile.getNative(), bstrict, params);
666
                        if(        dst.endsWith(".jpg") ||
667
                                dst.endsWith(".jpeg") ||
668
                                dst.endsWith(".png"))
669
                                RasterLocator.getManager().getFileUtils().createWorldFile(dst, gdalFile.getExtent(), (int)gdalFile.getWidth(), (int)gdalFile.getHeight());
670
                        gdalFile.close();
671
                        dstDataset.close();
672
                } catch (NotSupportedExtensionException e) {
673
                        e.printStackTrace();
674
                }
675
        }
676

    
677
        /**
678
         * Realiza la escritura de datos con los datos que le pasa el cliente.
679
         * @throws IOException
680
         * @throws RmfSerializerException
681
         */
682
        public void dataWrite() throws IOException, ProcessInterruptedException,
683
                        OutOfMemoryError {
684
                if (dataWriter == null)
685
                        throw new IOException("No se ha obtenido un objeto de entrada para la escritura valido.");
686

    
687
                write(DefaultRasterWriter.MODE_DATAWRITE);
688

    
689
                if (driverParams.getParamById("tfw") != null &&
690
                                ((ParamImpl)driverParams.getParamById("tfw")).getDefaultValue() instanceof Boolean /*&&
691
                                ((Boolean) ((ParamImpl)driverParams.getParamById("tfw")).getDefaultValue()).booleanValue() == true*/)
692
                        if (at != null)
693
                                fileUtil.createWorldFile(this.outFileName, at, sizeWindowX, sizeWindowY);
694

    
695
                if (colorInterp != null)
696
                        try {
697
                RasterLocator.getManager().getProviderServices()
698
                    .saveObjectToRmfFile(outFileName, DataStoreColorInterpretation.class, colorInterp);
699
                        } catch (RmfSerializerException e) {
700
                                throw new IOException("No se ha podido guardar la interpretacion de color");
701
                        }
702

    
703
                this.savePrjFile(new File(this.outFileName), proj);
704
        }
705

    
706
        public void writeClose() {
707
                try {
708
                        if(bufBands != null) {
709
                                for (int i = 0; i < bufBands.length; i++) {
710
                                        bufBands[i].buffAPalette = null;
711
                                        bufBands[i].buffBPalette = null;
712
                                        bufBands[i].buffByte = null;
713
                                        bufBands[i].buffShort = null;
714
                                        bufBands[i].buffInt = null;
715
                                        bufBands[i].buffFloat = null;
716
                                        bufBands[i].buffDouble = null;
717
                                }
718
                        }
719

    
720
                        if(dstDataset != null)
721
                                dstDataset.close();
722
                } catch (GdalException e) {
723
                        e.printStackTrace();
724
                }
725
        }
726

    
727
        /**
728
         * Cancela el salvado de datos.
729
         */
730
        public void writeCancel() {
731
                write = false;
732
        }
733

    
734
        /**
735
         * Obtiene el valor a la variable write que estar? a true cuando se est? escribiendo
736
         *  o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
737
         * pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
738
         * de salida.
739
         * @return True si puede escribirse y false si no puede
740
         */
741
        public boolean isWrite() {
742
                return write;
743
        }
744

    
745
        /**
746
         * Asigna el valor a la variable write que estar? a true cuando se est? escribiendo
747
         *  o puede escribirse la imagen de salida. El cancelar la operaci?n de escritura
748
         * pondr? esta variable a false deteniendose la escritura y cerrandose el dataset
749
         * de salida.
750
         * @param write Variable booleana. True si puede escribirse y false si no puede
751
         */
752
        public void setWrite(boolean write) {
753
                this.write = write;
754
        }
755

    
756
        /**
757
         * Asigna los par?metros del driver modificados por el cliente.
758
         * @param Params
759
         */
760
        public void setParams(Params params) {
761
                if(params instanceof ParamsImpl)
762
                        this.driverParams = (ParamsImpl)params;
763
                else
764
                        return;
765

    
766
                int blockSize = 256;
767
                try {
768
                        ParamImpl param = (ParamImpl)driverParams.getParamById("blocksize");
769
                        blockSize = Integer.parseInt(param.getList()[((Integer)param.getDefaultValue()).intValue()]);
770
                        nBlocks = (sizeWindowY / blockSize);
771
                        anchoResto = sizeWindowY - (nBlocks * blockSize);
772
                }catch(NumberFormatException e) {
773
                        //Se queda con el valor de inicializaci?n
774
                }
775
        }
776

    
777
        /*
778
         * (non-Javadoc)
779
         * @see java.lang.Object#finalize()
780
         */
781
        protected void finalize() throws Throwable {
782
                drv        = null;
783
                dstDataset = null;
784
                rband      = null;
785
                geot       = null;
786
                if(bufBands != null) {
787
                        for (int i = 0; i < bufBands.length; i++) {
788
                                bufBands[i].buffAPalette = null;
789
                                bufBands[i].buffBPalette = null;
790
                                bufBands[i].buffByte = null;
791
                                bufBands[i].buffShort = null;
792
                                bufBands[i].buffInt = null;
793
                                bufBands[i].buffFloat = null;
794
                                bufBands[i].buffDouble = null;
795
                        }
796
                }
797
                super.finalize();
798
        }
799
}