Statistics
| Revision:

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

History | View | Annotate | Download (13.1 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.features.WriteFileFormatFeatures;
33
import org.gvsig.raster.datastruct.Extent;
34
import org.gvsig.raster.util.RasterUtilities;
35
import org.gvsig.raster.util.extensionPoints.ExtensionPoint;
36
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
37
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
38

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

    
41

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

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

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

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

    
330
    /**
331
     * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
332
     * @throws IOException
333
     */
334
    public abstract void fileWrite() throws IOException;
335

    
336
    /**
337
     * Realiza la funci?n de compresi?n a partir de los datos pasados por el cliente.
338
     * @throws IOException
339
     */
340
    public abstract void dataWrite() throws IOException;
341

    
342
    /**
343
     * Cierra el driver
344
     */
345
    public abstract void writeClose();
346
    
347
    /**
348
     * Cancela el grabado de datos
349
     */
350
    public abstract void writeCancel();
351
    
352
        /**
353
         * A?ade la proyecci?n Wkt con la que salvar.
354
         * @param wkt
355
         * @throws GdalException
356
         */
357
        public abstract void setWkt(String wkt);
358
    
359
    /**
360
     * M?todo que pregunta si la extensi?n pasada por par?metro est? soportada 
361
     * con el tipo y n?mero de bandas indicadas.
362
     * @param dataType Tipo de dato
363
     * @param bands N?mero de bandas
364
     * @param extensi?n
365
     * @return true si est? soportada y false si no lo est?
366
     */
367
        public boolean isSupportedThisExtension(String ext, int dataType, int bands) {
368
                WriteFileFormatFeatures features = (WriteFileFormatFeatures)fileFeature.get(ext);
369
                if(features == null)
370
                        return false;
371
                int bandsSupported = features.getNBandsSupported();
372
                if(bandsSupported != -1 && bandsSupported < bands)
373
                        return false;
374
                int[] dt = features.getDataTypesSupported();
375
                for (int i = 0; i < dt.length; i++)
376
                        if(dataType == dt[i])
377
                                return true;
378
                return false;
379
        }
380
}