Statistics
| Revision:

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

History | View | Annotate | Download (13.3 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.RasterLibrary;
31
import org.gvsig.raster.dataset.io.features.WriteFileFormatFeatures;
32
import org.gvsig.raster.shared.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
    
67
    /**
68
     * Carga los par?metros comunes a todos los drivers en el objeto WriterParams.
69
     */
70
    /*public void loadParams() {
71
            driverParams.clear();
72
            
73
            driverParams.setParam(        "blocksize", 
74
                                                            String.valueOf(RasterLibrary.blockHeight), 
75
                                                            Params.CHOICE, 
76
                                                            new String[]{ "1", "8", "16", "32", "64", "128", "256", "512", "1024"});
77
            
78
            driverParams.setParam(        "georef", 
79
                                                                "true", 
80
                                                                Params.CHECK, 
81
                                                                null);
82
    }*/
83
    
84
    /**
85
     * Obtiene la lista de extensiones registradas
86
     * @return Lista de extensiones registradas o null si no hay ninguna
87
     */
88
    public static String[] getDriversExtensions(){
89
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
90
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
91
                if(extensionPoint == null)
92
                        return null;
93
            
94
            String[] list = new String[extensionPoint.size()];
95
            Set values = extensionPoint.entrySet();
96
            int i = 0;
97
            for (Iterator it = values.iterator(); it.hasNext(); ) {
98
            list[i] = (String)((Map.Entry)it.next()).getKey();
99
            i++;
100
        }
101
            
102
            return list;
103
    }
104
    
105
    /**
106
     * Obtiene la lista de extensiones de ficheros sobre los que se puede salvar en un determinado
107
     * tipo de datos. Como par?metro de la funci?n se especifica el tipo de datos sobre el que se
108
     * desea consultar.
109
     * Este m?todo consulta para cada driver registrado que extensiones soportan un tipoi
110
     * @return Lista de extensiones registradas que soportan el tipo de datos pasado por par?metro.
111
     */
112
    public static ArrayList getExtensionsSupported(int dataType, int bands) throws RasterDriverException {
113
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
114
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
115
                if(extensionPoint == null)
116
                        return null;
117
                Set values = extensionPoint.entrySet();
118
            ArrayList result = new ArrayList();
119
            for (Iterator it = values.iterator(); it.hasNext(); ) {
120
                    Map.Entry entry = ((Map.Entry)it.next());
121
                    String ext = (String)entry.getKey();
122
            Class writerClass = (Class)entry.getValue();
123
            Class [] args = {String.class};
124
                    try {
125
                            Constructor hazNuevo = writerClass.getConstructor(args);
126
                            Object [] args2 = {ext};
127
                            GeoRasterWriter grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
128
                            if(grw.isSupportedThisExtension(ext, dataType, bands))
129
                                    result.add(ext);
130
                    } catch (SecurityException e) {
131
                            throw new RasterDriverException("Error SecurityException in open");
132
                    } catch (NoSuchMethodException e) {
133
                            throw new RasterDriverException("Error NoSuchMethodException in open");
134
                    } catch (IllegalArgumentException e) {
135
                            throw new RasterDriverException("Error IllegalArgumentException in open");
136
                    } catch (InstantiationException e) {
137
                            throw new RasterDriverException("Error InstantiationException in open");
138
                    } catch (IllegalAccessException e) {
139
                            throw new RasterDriverException("Error IllegalAccessException in open");
140
                    } catch (InvocationTargetException e) {
141
                            throw new RasterDriverException("Error in open");
142
                    }
143
        }
144
            return result;
145
    }
146
    
147
    /**
148
     * Obtiene la lista de tipos de driver
149
     * @return Lista de tipos de driver registradas o null si no hay ninguno
150
     */
151
    public static String[] getDriversType(){
152
            if (fileFeature.size() == 0)
153
                    return null;
154
            String[] list = new String[fileFeature.size()];
155
            Set values = fileFeature.entrySet();
156
            int i = 0;
157
            for (Iterator it=values.iterator(); it.hasNext(); ) {
158
            list[i] = ((WriteFileFormatFeatures)((Map.Entry)it.next()).getValue()).getDriverName();
159
            i++;
160
        }
161
            
162
            return list;
163
    }
164
    
165
    /**
166
     * Obtiene el tipo de driver a partir de la extensi?n
167
     * @param ext        Extensi?n
168
     * @return        Tipo
169
     */
170
    public static String getDriverType(String ext){
171
            return ((WriteFileFormatFeatures)fileFeature.get(ext)).getDriverName();
172
    }
173
    
174
    /**
175
     * Devuelve el n?mero de drivers soportados
176
     * @return N?mero de drivers soportados
177
     */
178
    public static int getNDrivers() {
179
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
180
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
181
        return extensionPoint.size();
182
    }
183
    
184
    /**
185
     * Devuelve el n?mero de tipos de driver registrados
186
     * @return N?mero de tipos de driver soportados
187
     */
188
    public static int getNTypes() {
189
        return fileFeature.size();
190
    }
191

    
192
    /**
193
     * Devuelve el identificador del driver
194
     * @return        Identificador del driver
195
     */
196
    public String getIdent() {
197
        return ident;
198
    }
199

    
200
    /**
201
     * Obtiene el nombre del driver.
202
     * @return        Nombre del driver
203
     */
204
    public String getDriverName() {
205
        return driver;
206
    }
207

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

    
342
    /**
343
     * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
344
     * @throws IOException
345
     */
346
    public abstract void fileWrite() throws IOException;
347

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

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