Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / GeoRasterWriter.java @ 21345

History | View | Annotate | Download (14.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.dataset;
20

    
21
import java.awt.geom.AffineTransform;
22
import java.io.IOException;
23
import java.lang.reflect.Constructor;
24
import java.lang.reflect.InvocationTargetException;
25
import java.util.ArrayList;
26
import java.util.Iterator;
27
import java.util.Map;
28
import java.util.Set;
29
import java.util.TreeMap;
30

    
31
import org.cresques.cts.IProjection;
32
import org.gvsig.raster.dataset.io.RasterDriverException;
33
import org.gvsig.raster.dataset.io.features.WriteFileFormatFeatures;
34
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
35
import org.gvsig.raster.util.RasterUtilities;
36
import org.gvsig.raster.util.extensionPoints.ExtensionPoint;
37
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
38
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
39

    
40
import es.gva.cit.jgdal.GdalException;
41

    
42

    
43
/**
44
 * Clase abstracta de la que heredan los drivers de escritura. Tiene los
45
 * m?todos abstractos que debe implementar cualquier driver de escritura
46
 * y las funcionalidades y opciones soportadas comunes a todos ellos.
47
 * 
48
 * @author Nacho Brodin (nachobrodin@gmail.com)
49
 */
50
public abstract class GeoRasterWriter {
51
        
52
        public static final int                 MODE_FILEWRITE = 1;
53
        public static final int                 MODE_DATAWRITE = 2;
54
        
55
    public static TreeMap                             fileFeature = new TreeMap();
56
    protected String                                     outFileName = null;
57
    protected String                                     inFileName = null;
58
    protected int                                             sizeWindowX = 0;
59
    protected int                                             sizeWindowY = 0;
60
    protected int                                             ulX = 0;
61
    protected int                                             ulY = 0;
62
    protected IDataWriter                             dataWriter = null;
63
    protected int                                             nBands = 0;
64
    protected String                                     ident = null;
65
    protected String                                     driver = null;
66
    protected Params                                    driverParams = null;
67
    protected AffineTransform               at = null;
68
    protected int                                            percent = 0;
69
    protected int                                             dataType = IBuffer.TYPE_BYTE;
70
    protected IProjection                            proj = null;
71
    protected DatasetColorInterpretation    colorInterp = null;
72
    
73
    /**
74
     * Obtiene la lista de extensiones registradas
75
     * @return Lista de extensiones registradas o null si no hay ninguna
76
     */
77
    public static String[] getDriversExtensions(){
78
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
79
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
80
                if(extensionPoint == null)
81
                        return null;
82
            
83
            String[] list = new String[extensionPoint.size()];
84
            Set values = extensionPoint.entrySet();
85
            int i = 0;
86
            for (Iterator it = values.iterator(); it.hasNext(); ) {
87
            list[i] = (String)((Map.Entry)it.next()).getKey();
88
            i++;
89
        }
90
            
91
            return list;
92
    }
93
    
94
    /**
95
     * Obtiene la lista de extensiones de ficheros sobre los que se puede salvar en un determinado
96
     * tipo de datos. Como par?metro de la funci?n se especifica el tipo de datos sobre el que se
97
     * desea consultar.
98
     * Este m?todo consulta para cada driver registrado que extensiones soportan un tipoi
99
     * @return Lista de extensiones registradas que soportan el tipo de datos pasado por par?metro.
100
     */
101
    public static ArrayList getExtensionsSupported(int dataType, int bands) throws RasterDriverException {
102
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
103
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
104
                if(extensionPoint == null)
105
                        return null;
106
                Set values = extensionPoint.entrySet();
107
            ArrayList result = new ArrayList();
108
            for (Iterator it = values.iterator(); it.hasNext(); ) {
109
                    Map.Entry entry = ((Map.Entry)it.next());
110
                    String ext = (String)entry.getKey();
111
            Class writerClass = (Class)entry.getValue();
112
            Class [] args = {String.class};
113
                    try {
114
                            Constructor hazNuevo = writerClass.getConstructor(args);
115
                            Object [] args2 = {ext};
116
                            GeoRasterWriter grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
117
                            if(grw.isSupportedThisExtension(ext, dataType, bands))
118
                                    result.add(ext);
119
                    } catch (SecurityException e) {
120
                            throw new RasterDriverException("Error SecurityException in open");
121
                    } catch (NoSuchMethodException e) {
122
                            throw new RasterDriverException("Error NoSuchMethodException in open");
123
                    } catch (IllegalArgumentException e) {
124
                            throw new RasterDriverException("Error IllegalArgumentException in open");
125
                    } catch (InstantiationException e) {
126
                            throw new RasterDriverException("Error InstantiationException in open");
127
                    } catch (IllegalAccessException e) {
128
                            throw new RasterDriverException("Error IllegalAccessException in open");
129
                    } catch (InvocationTargetException e) {
130
                            throw new RasterDriverException("Error in open");
131
                    }
132
        }
133
            return result;
134
    }
135
    
136
    /**
137
     * Obtiene la lista de tipos de driver
138
     * @return Lista de tipos de driver registradas o null si no hay ninguno
139
     */
140
    public static String[] getDriversType(){
141
            if (fileFeature.size() == 0)
142
                    return null;
143
            String[] list = new String[fileFeature.size()];
144
            Set values = fileFeature.entrySet();
145
            int i = 0;
146
            for (Iterator it=values.iterator(); it.hasNext(); ) {
147
            list[i] = ((WriteFileFormatFeatures)((Map.Entry)it.next()).getValue()).getDriverName();
148
            i++;
149
        }
150
            
151
            return list;
152
    }
153
    
154
    /**
155
     * Obtiene el tipo de driver a partir de la extensi?n
156
     * @param ext        Extensi?n
157
     * @return        Tipo
158
     */
159
    public static String getDriverType(String ext){
160
            return ((WriteFileFormatFeatures)fileFeature.get(ext)).getDriverName();
161
    }
162
    
163
    /**
164
     * Devuelve el n?mero de drivers soportados
165
     * @return N?mero de drivers soportados
166
     */
167
    public static int getNDrivers() {
168
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
169
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
170
        return extensionPoint.size();
171
    }
172
        
173
    /**
174
     * Devuelve el n?mero de tipos de driver registrados
175
     * @return N?mero de tipos de driver soportados
176
     */
177
    public static int getNTypes() {
178
        return fileFeature.size();
179
    }
180

    
181
    /**
182
     * Devuelve el identificador del driver
183
     * @return        Identificador del driver
184
     */
185
    public String getIdent() {
186
        return ident;
187
    }
188

    
189
    /**
190
     * Obtiene el nombre del driver.
191
     * @return        Nombre del driver
192
     */
193
    public String getDriverName() {
194
        return driver;
195
    }
196

    
197
    /**
198
     * 
199
     * @return
200
     */
201
    public String getDriverType() {
202
        return driver;
203
    }
204
    
205
    /**
206
     * Asigna el porcentaje de incremento. Esto es usado por el driver para actualizar
207
     * la variable percent
208
     * @param percent
209
     */
210
    public void setPercent(int percent) {
211
            this.percent = percent;
212
    }
213
    
214
    /**
215
     * Porcentaje de escritura completado.
216
     * @return Entero con el porcentaje de escritura.
217
     */
218
    public int getPercent() {
219
                return percent;
220
        }
221
    
222
    /**
223
         * Factoria para obtener escritores de los distintos tipos de raster.
224
         * 
225
         * @param fName Nombre del fichero.
226
         * @return GeoRasterWriter, o null si hay problemas.
227
         */
228
        public static GeoRasterWriter getWriter(String fName) throws NotSupportedExtensionException, RasterDriverException {
229
                String ext = RasterUtilities.getExtensionFromFileName(fName);
230
                GeoRasterWriter grw = null;
231
                
232
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
233
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
234
            
235
            if(extensionPoint.get(ext) == null) 
236
                        return grw;
237
                
238
                Class clase = (Class) extensionPoint.get(ext);
239
                Class [] args = {String.class};
240
                try {
241
                        Constructor hazNuevo = clase.getConstructor(args);
242
                        Object [] args2 = {fName};
243
                        grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
244
                } catch (SecurityException e) {
245
                        throw new RasterDriverException("Error SecurityException in open");
246
                } catch (NoSuchMethodException e) {
247
                        throw new RasterDriverException("Error NoSuchMethodException in open");
248
                } catch (IllegalArgumentException e) {
249
                        throw new RasterDriverException("Error IllegalArgumentException in open");
250
                } catch (InstantiationException e) {
251
                        throw new RasterDriverException("Error InstantiationException in open");
252
                } catch (IllegalAccessException e) {
253
                        throw new RasterDriverException("Error IllegalAccessException in open");
254
                } catch (InvocationTargetException e) {
255
                        throw new NotSupportedExtensionException("Error in open");
256
                }
257
                return grw;
258
        }
259
        
260
        /**
261
         * Factoria para obtener escritores de los distintos tipos de raster.
262
         * 
263
         * @param fName Nombre del fichero.
264
         * @return GeoRasterWriter, o null si hay problemas.
265
         */
266
        public static GeoRasterWriter getWriter(IDataWriter dataWriter, 
267
                                                                                     String outFileName, 
268
                                                                                     int nBands,
269
                                                                                     AffineTransform at,
270
                                                                                     int outSizeX,
271
                                                                                     int outSizeY,
272
                                                                                     int dataType,
273
                                                                                     Params params,
274
                                                                                     IProjection proj) throws NotSupportedExtensionException, RasterDriverException {
275
                return GeoRasterWriter.getWriter(dataWriter, outFileName, nBands, at, outSizeX, outSizeY, dataType, params, proj, true);
276
        }
277
        
278
        /**
279
         * Factoria para obtener escritores de los distintos tipos de raster.
280
         * 
281
         * @param fName Nombre del fichero.
282
         * @return GeoRasterWriter, o null si hay problemas.
283
         */
284
        public static GeoRasterWriter getWriter(IDataWriter dataWriter, 
285
                                                                                     String outFileName, 
286
                                                                                     int nBands,
287
                                                                                     AffineTransform at,
288
                                                                                     int outSizeX,
289
                                                                                     int outSizeY,
290
                                                                                     int dataType,
291
                                                                                     Params params,
292
                                                                                     IProjection proj,
293
                                                                                     boolean geo) throws NotSupportedExtensionException, RasterDriverException {
294
                String ext = outFileName.toLowerCase().substring(outFileName.lastIndexOf('.')+1);
295
                GeoRasterWriter grw = null;
296
                
297
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
298
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
299
            
300
            if(extensionPoint.get(ext) == null) 
301
                        return grw;
302
                                
303
                Class clase = (Class) extensionPoint.get(ext);
304
                Class [] args = {IDataWriter.class, String.class, Integer.class, AffineTransform.class, 
305
                                                Integer.class, Integer.class, Integer.class, Params.class, IProjection.class, Boolean.class};
306
                try {
307
                        Constructor hazNuevo = clase.getConstructor(args);
308
                        Object [] args2 = {dataWriter, outFileName, new Integer(nBands), at, 
309
                                                                new Integer(outSizeX), new Integer(outSizeY), new Integer(dataType), 
310
                                                                params, proj, new Boolean(geo)};
311
                        grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
312
                } catch (SecurityException e) {
313
                        throw new RasterDriverException("Error SecurityException in open");
314
                } catch (NoSuchMethodException e) {
315
                        throw new RasterDriverException("Error NoSuchMethodException in open");
316
                } catch (IllegalArgumentException e) {
317
                        throw new RasterDriverException("Error IllegalArgumentException in open");
318
                } catch (InstantiationException e) {
319
                        throw new RasterDriverException("Error InstantiationException in open");
320
                } catch (IllegalAccessException e) {
321
                        throw new RasterDriverException("Error IllegalAccessException in open");
322
                } catch (InvocationTargetException e) {
323
                        throw new NotSupportedExtensionException("Error in open. Problemas con las librer?as nativas.");
324
                }
325
                return grw;
326
        }
327
        
328
        /**
329
     * Obtiene los par?metros del driver.
330
     * @return WriterParams
331
     */
332
    public Params getParams() {
333
                return driverParams;
334
        }
335
    
336
    /**
337
     * Asigna los par?metros del driver modificados por el cliente.
338
     * @param Params
339
     */
340
    public void setParams(Params params) {
341
                this.driverParams = params;
342
        }
343
        
344
    /**
345
     * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
346
     * @throws IOException
347
     */
348
    public abstract void fileWrite() throws IOException, InterruptedException;
349

    
350
    /**
351
     * Realiza la funci?n de compresi?n a partir de los datos pasados por el cliente.
352
     * @throws IOException
353
     */
354
    public abstract void dataWrite() throws IOException, InterruptedException;
355

    
356
    /**
357
     * Cierra el driver
358
     */
359
    public abstract void writeClose();
360
    
361
    /**
362
     * Cancela el grabado de datos
363
     */
364
    public abstract void writeCancel();
365
    
366
        /**
367
         * A?ade la proyecci?n Wkt con la que salvar.
368
         * @param wkt
369
         * @throws GdalException
370
         */
371
        public abstract void setWkt(String wkt);
372
        
373
        /**
374
         * Asigna la interpretaci?n de color para el fichero de salida.
375
         * @param colorInterp Interpretaci?n de color
376
         */
377
        public void setColorBandsInterpretation(String[] colorInterp) {
378
                if(colorInterp != null) {
379
                        this.colorInterp = new DatasetColorInterpretation();
380
                        this.colorInterp.initColorInterpretation(colorInterp.length);
381
                        for (int i = 0; i < colorInterp.length; i++) {
382
                                this.colorInterp.setColorInterpValue(i, colorInterp[i]);
383
                        }
384
                }
385
        }
386
    
387
    /**
388
     * M?todo que pregunta si la extensi?n pasada por par?metro est? soportada 
389
     * con el tipo y n?mero de bandas indicadas.
390
     * @param dataType Tipo de dato
391
     * @param bands N?mero de bandas
392
     * @param extensi?n
393
     * @return true si est? soportada y false si no lo est?
394
     */
395
        public boolean isSupportedThisExtension(String ext, int dataType, int bands) {
396
                WriteFileFormatFeatures features = (WriteFileFormatFeatures) fileFeature.get(ext);
397
                if (features == null)
398
                        return false;
399
                int[] bandsSupported = features.getNBandsSupported();
400
                for (int i = 0; i < bandsSupported.length; i++) {
401
                        if (bandsSupported[i] == -1)
402
                                break;
403
                        if (bandsSupported[i] >= bands)
404
                                break;
405
                        return false;
406
                }
407
                int[] dt = features.getDataTypesSupported();
408
                for (int i = 0; i < dt.length; i++)
409
                        if (dataType == dt[i])
410
                                return true;
411
                return false;
412
        }
413
}