Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / GeoRasterWriter.java @ 12470

History | View | Annotate | Download (12.9 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.io.IOException;
22
import java.lang.reflect.Constructor;
23
import java.lang.reflect.InvocationTargetException;
24
import java.util.ArrayList;
25
import java.util.Iterator;
26
import java.util.Map;
27
import java.util.Set;
28
import java.util.TreeMap;
29

    
30
import org.cresques.cts.IProjection;
31
import org.gvsig.raster.dataset.io.features.WriteFileFormatFeatures;
32
import org.gvsig.raster.datastruct.Extent;
33
import org.gvsig.raster.util.RasterUtilities;
34
import org.gvsig.raster.util.extensionPoints.ExtensionPoint;
35
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
36
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
37

    
38

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

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

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

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

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

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

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