Statistics
| Revision:

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

History | View | Annotate | Download (12.7 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.gvsig.raster.dataset.io.features.WriteFileFormatFeatures;
31
import org.gvsig.raster.shared.Extent;
32
import org.gvsig.raster.util.RasterUtilities;
33
import org.gvsig.raster.util.extensionPoints.ExtensionPoint;
34
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
35
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
36

    
37

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

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

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

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

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

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

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