Statistics
| Revision:

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

History | View | Annotate | Download (13 KB)

1 10939 nacho
/* 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 11960 nacho
import java.util.ArrayList;
25 10939 nacho
import java.util.Iterator;
26
import java.util.Map;
27
import java.util.Set;
28
import java.util.TreeMap;
29
30 12470 nacho
import org.cresques.cts.IProjection;
31 11981 nacho
import org.gvsig.raster.dataset.io.features.WriteFileFormatFeatures;
32 12383 nacho
import org.gvsig.raster.datastruct.Extent;
33 11284 nacho
import org.gvsig.raster.util.RasterUtilities;
34 11237 nacho
import org.gvsig.raster.util.extensionPoints.ExtensionPoint;
35
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
36
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
37 10939 nacho
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 11237 nacho
 *
44 10939 nacho
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public abstract class GeoRasterWriter {
47
48 11237 nacho
        public static final int MODE_FILEWRITE = 1;
49
        public static final int MODE_DATAWRITE = 2;
50
51 11967 nacho
    public static TreeMap                 fileFeature = new TreeMap();
52 10939 nacho
    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 11981 nacho
    protected Params                        driverParams = null;
63 11237 nacho
    protected Extent                        extent = null;
64 11522 nacho
    protected int                                percent = 0;
65 11947 nacho
    protected int                                 dataType = IBuffer.TYPE_BYTE;
66 12470 nacho
    protected IProjection                proj = null;
67 10939 nacho
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 11237 nacho
            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 10939 nacho
            int i = 0;
81 11237 nacho
            for (Iterator it = values.iterator(); it.hasNext(); ) {
82 10939 nacho
            list[i] = (String)((Map.Entry)it.next()).getKey();
83
            i++;
84
        }
85
86
            return list;
87
    }
88
89
    /**
90 11960 nacho
     * 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 11967 nacho
    public static ArrayList getExtensionsSupported(int dataType, int bands) throws RasterDriverException {
97 11960 nacho
            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 11967 nacho
                            if(grw.isSupportedThisExtension(ext, dataType, bands))
113
                                    result.add(ext);
114 11960 nacho
                    } 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 10939 nacho
     * 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 11967 nacho
            if (fileFeature.size() == 0)
137 10939 nacho
                    return null;
138 11967 nacho
            String[] list = new String[fileFeature.size()];
139
            Set values = fileFeature.entrySet();
140 10939 nacho
            int i = 0;
141
            for (Iterator it=values.iterator(); it.hasNext(); ) {
142 11967 nacho
            list[i] = ((WriteFileFormatFeatures)((Map.Entry)it.next()).getValue()).getDriverName();
143 10939 nacho
            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 11967 nacho
            return ((WriteFileFormatFeatures)fileFeature.get(ext)).getDriverName();
156 10939 nacho
    }
157
158
    /**
159
     * Devuelve el n?mero de drivers soportados
160
     * @return N?mero de drivers soportados
161
     */
162
    public static int getNDrivers() {
163 11237 nacho
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
164
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
165
        return extensionPoint.size();
166 10939 nacho
    }
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 11967 nacho
        return fileFeature.size();
174 10939 nacho
    }
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 11522 nacho
     * 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 10939 nacho
         * 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 11237 nacho
        public static GeoRasterWriter getWriter(String fName) throws NotSupportedExtensionException, RasterDriverException {
224 11284 nacho
                String ext = RasterUtilities.getExtensionFromFileName(fName);
225 10939 nacho
                GeoRasterWriter grw = null;
226
227 11237 nacho
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
228
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
229
230
            if(extensionPoint.get(ext) == null)
231 10939 nacho
                        return grw;
232
233 11237 nacho
                Class clase = (Class) extensionPoint.get(ext);
234 10939 nacho
                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 11237 nacho
                        throw new RasterDriverException("Error SecurityException in open");
241 10939 nacho
                } catch (NoSuchMethodException e) {
242 11237 nacho
                        throw new RasterDriverException("Error NoSuchMethodException in open");
243 10939 nacho
                } catch (IllegalArgumentException e) {
244 11237 nacho
                        throw new RasterDriverException("Error IllegalArgumentException in open");
245 10939 nacho
                } catch (InstantiationException e) {
246 11237 nacho
                        throw new RasterDriverException("Error InstantiationException in open");
247 10939 nacho
                } catch (IllegalAccessException e) {
248 11237 nacho
                        throw new RasterDriverException("Error IllegalAccessException in open");
249 10939 nacho
                } catch (InvocationTargetException e) {
250 11237 nacho
                        throw new NotSupportedExtensionException("Error in open");
251 10939 nacho
                }
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 11310 nacho
                                                                                     int dataType,
268 12470 nacho
                                                                                     Params params,
269
                                                                                     IProjection proj) throws NotSupportedExtensionException, RasterDriverException {
270 10939 nacho
                String ext = outFileName.toLowerCase().substring(outFileName.lastIndexOf('.')+1);
271
                GeoRasterWriter grw = null;
272
273 11237 nacho
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
274
            ExtensionPoint extensionPoint = (ExtensionPoint)extensionPoints.get("RasterWriter");
275
276
            if(extensionPoint.get(ext) == null)
277 10939 nacho
                        return grw;
278 11237 nacho
279
                Class clase = (Class) extensionPoint.get(ext);
280 11310 nacho
                Class [] args = {IDataWriter.class, String.class, Integer.class, Extent.class,
281 12470 nacho
                                                Integer.class, Integer.class, Integer.class, Params.class, IProjection.class};
282 10939 nacho
                try {
283
                        Constructor hazNuevo = clase.getConstructor(args);
284 11310 nacho
                        Object [] args2 = {dataWriter, outFileName, new Integer(nBands), ex,
285 12470 nacho
                                                                new Integer(outSizeX), new Integer(outSizeY), new Integer(dataType), params, proj};
286 10939 nacho
                        grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
287
                } catch (SecurityException e) {
288 11237 nacho
                        throw new RasterDriverException("Error SecurityException in open");
289 10939 nacho
                } catch (NoSuchMethodException e) {
290 11237 nacho
                        throw new RasterDriverException("Error NoSuchMethodException in open");
291 10939 nacho
                } catch (IllegalArgumentException e) {
292 11237 nacho
                        throw new RasterDriverException("Error IllegalArgumentException in open");
293 10939 nacho
                } catch (InstantiationException e) {
294 11237 nacho
                        throw new RasterDriverException("Error InstantiationException in open");
295 10939 nacho
                } catch (IllegalAccessException e) {
296 11237 nacho
                        throw new RasterDriverException("Error IllegalAccessException in open");
297 10939 nacho
                } catch (InvocationTargetException e) {
298 11237 nacho
                        throw new NotSupportedExtensionException("Error in open");
299 10939 nacho
                }
300
                return grw;
301
        }
302
303 11237 nacho
        /**
304
     * Obtiene los par?metros del driver.
305
     * @return WriterParams
306
     */
307 11652 bsanchez
    public Params getParams() {
308 11237 nacho
                return driverParams;
309
        }
310
311 10939 nacho
    /**
312 11237 nacho
     * Asigna los par?metros del driver modificados por el cliente.
313 11652 bsanchez
     * @param Params
314 11237 nacho
     */
315 11652 bsanchez
    public void setParams(Params params) {
316 11237 nacho
                this.driverParams = params;
317
        }
318
319
    /**
320 10939 nacho
     * 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 11981 nacho
    //public abstract void setProps(String[] props);
326 10939 nacho
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 11960 nacho
349 12494 nacho
        /**
350
         * A?ade la proyecci?n Wkt con la que salvar.
351
         * @param wkt
352
         * @throws GdalException
353
         */
354
        public abstract void setWkt(String wkt);
355
356 11960 nacho
    /**
357 11967 nacho
     * M?todo que pregunta si la extensi?n pasada por par?metro est? soportada
358
     * con el tipo y n?mero de bandas indicadas.
359
     * @param dataType Tipo de dato
360
     * @param bands N?mero de bandas
361
     * @param extensi?n
362
     * @return true si est? soportada y false si no lo est?
363 11960 nacho
     */
364 11967 nacho
        public boolean isSupportedThisExtension(String ext, int dataType, int bands) {
365
                WriteFileFormatFeatures features = (WriteFileFormatFeatures)fileFeature.get(ext);
366
                if(features == null)
367
                        return false;
368
                int bandsSupported = features.getNBandsSupported();
369
                if(bandsSupported != -1 && bandsSupported < bands)
370
                        return false;
371
                int[] dt = features.getDataTypesSupported();
372
                for (int i = 0; i < dt.length; i++)
373
                        if(dataType == dt[i])
374
                                return true;
375
                return false;
376
        }
377 10939 nacho
}