Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / dataset / GeoRasterWriter.java @ 29535

History | View | Annotate | Download (14.1 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 29535 nbrodin
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 12470 nacho
import org.cresques.cts.IProjection;
32 18040 nbrodin
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
33 22473 bsanchez
import org.gvsig.raster.dataset.serializer.RmfSerializerException;
34 11284 nacho
import org.gvsig.raster.util.RasterUtilities;
35 26873 vcaballero
import org.gvsig.tools.ToolsLocator;
36
import org.gvsig.tools.extensionpoint.ExtensionPoint;
37
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
38 10939 nacho
39 12567 bsanchez
import es.gva.cit.jgdal.GdalException;
40 22102 bsanchez
import es.gva.cit.jgdal.GdalWarp;
41 10939 nacho
42
/**
43 22102 bsanchez
 * Clase abstracta de la que heredan los drivers de escritura. Tiene los m?todos
44
 * abstractos que debe implementar cualquier driver de escritura y las
45
 * funcionalidades y opciones soportadas comunes a todos ellos.
46 26873 vcaballero
 *
47 10939 nacho
 * @author Nacho Brodin (nachobrodin@gmail.com)
48
 */
49
public abstract class GeoRasterWriter {
50 22102 bsanchez
        public static final int   MODE_FILEWRITE = 1;
51
        public static final int   MODE_DATAWRITE = 2;
52
53
        public static TreeMap     fileFeature    = new TreeMap();
54
        protected String          outFileName    = null;
55
        protected String          inFileName     = null;
56
        protected int             sizeWindowX    = 0;
57
        protected int             sizeWindowY    = 0;
58
        protected int             ulX            = 0;
59
        protected int             ulY            = 0;
60
        protected IDataWriter     dataWriter     = null;
61
        protected int             nBands         = 0;
62
        protected String          ident          = null;
63
        protected String          driver         = null;
64
        protected Params          driverParams   = null;
65
        protected AffineTransform at             = null;
66
        protected int             percent        = 0;
67
        protected int             dataType       = IBuffer.TYPE_BYTE;
68
        protected IProjection     proj           = null;
69
        protected DatasetColorInterpretation colorInterp = null;
70 29510 nbrodin
        protected IExternalCancellable extCancellable = null;
71 26873 vcaballero
72 22102 bsanchez
        /**
73
         * Obtiene la lista de extensiones registradas
74
         * @return Lista de extensiones registradas o null si no hay ninguna
75
         */
76
        public static String[] getDriversExtensions() {
77 28498 nbrodin
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
78
                ExtensionPoint point = extensionPoints.get("RasterWriter");
79 26873 vcaballero
                return (String[])point.getNames().toArray(new String[0]);
80 22102 bsanchez
        }
81
82
        /**
83
         * Obtiene la lista de extensiones de ficheros sobre los que se puede salvar
84
         * en un determinado tipo de datos. Como par?metro de la funci?n se especifica
85
         * el tipo de datos sobre el que se desea consultar. Este m?todo consulta para
86
         * cada driver registrado que extensiones soportan un tipo.
87 26873 vcaballero
         *
88 22102 bsanchez
         * @param dataType Tipo de datos
89
         * @param bands Numero de bandas
90
         * @param reprojectable Especifica si devuelve solo los formatos reproyectables
91
         * @return Lista de extensiones registradas que soportan el tipo de datos
92
         *         pasado por par?metro.
93
         * @throws RasterDriverException
94
         */
95
        public static ArrayList getExtensionsSupported(int dataType, int bands, boolean reprojectable) throws RasterDriverException {
96 28498 nbrodin
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
97
                ExtensionPoint point = extensionPoints.get("RasterWriter");
98 26873 vcaballero
                Iterator iterator = point.iterator();
99 22102 bsanchez
                ArrayList result = new ArrayList();
100 22422 bsanchez
                while (iterator.hasNext()) {
101 26905 jmvivo
                        ExtensionPoint.Extension extension = (ExtensionPoint.Extension) iterator
102
                                        .next();
103
                        String ext = extension.getName();
104
                        Class writerClass = extension.getExtension();
105 22102 bsanchez
                        Class[] args = { String.class };
106
                        try {
107
                                Constructor hazNuevo = writerClass.getConstructor(args);
108
                                Object[] args2 = { ext };
109
                                GeoRasterWriter grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
110 26905 jmvivo
                                if (grw.isSupportedThisExtension(ext, dataType, bands))
111 22102 bsanchez
                                        if (reprojectable) {
112
                                                if (GdalWarp.getDrivers().contains(grw.getDriverName()))
113
                                                        result.add(ext);
114 26905 jmvivo
                                        } else
115 22102 bsanchez
                                                result.add(ext);
116
                        } catch (SecurityException e) {
117
                                throw new RasterDriverException("Error SecurityException in open");
118
                        } catch (NoSuchMethodException e) {
119
                                throw new RasterDriverException("Error NoSuchMethodException in open");
120
                        } catch (IllegalArgumentException e) {
121
                                throw new RasterDriverException("Error IllegalArgumentException in open");
122
                        } catch (InstantiationException e) {
123
                                throw new RasterDriverException("Error InstantiationException in open");
124
                        } catch (IllegalAccessException e) {
125
                                throw new RasterDriverException("Error IllegalAccessException in open");
126
                        } catch (InvocationTargetException e) {
127
                                throw new RasterDriverException("Error in open");
128
                        }
129
                }
130
                return result;
131
        }
132 26873 vcaballero
133 22102 bsanchez
        /**
134
         * Obtiene la lista de tipos de driver
135
         * @return Lista de tipos de driver registradas o null si no hay ninguno
136
         */
137
        public static String[] getDriversType() {
138
                if (fileFeature.size() == 0)
139
                        return null;
140
                String[] list = new String[fileFeature.size()];
141
                Set values = fileFeature.entrySet();
142
                int i = 0;
143
                for (Iterator it = values.iterator(); it.hasNext();) {
144
                        list[i] = ((WriteFileFormatFeatures) ((Map.Entry) it.next()).getValue()).getDriverName();
145
                        i++;
146
                }
147 10939 nacho
148 22102 bsanchez
                return list;
149
        }
150 10939 nacho
151 22102 bsanchez
        /**
152
         * Obtiene el tipo de driver a partir de la extensi?n
153
         * @param ext Extensi?n
154
         * @return Tipo
155
         */
156
        public static String getDriverType(String ext) {
157
                return ((WriteFileFormatFeatures) fileFeature.get(ext)).getDriverName();
158
        }
159 10939 nacho
160 22102 bsanchez
        /**
161
         * Devuelve el n?mero de drivers soportados
162
         * @return N?mero de drivers soportados
163
         */
164
        public static int getNDrivers() {
165 28498 nbrodin
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
166
                ExtensionPoint point = extensionPoints.get("RasterWriter");
167 26873 vcaballero
                return point.getCount();
168 22102 bsanchez
        }
169
170
        /**
171
         * Devuelve el n?mero de tipos de driver registrados
172
         * @return N?mero de tipos de driver soportados
173
         */
174
        public static int getNTypes() {
175
                return fileFeature.size();
176
        }
177
178
        /**
179
         * Devuelve el identificador del driver
180
         * @return Identificador del driver
181
         */
182
        public String getIdent() {
183
                return ident;
184
        }
185
186
        /**
187
         * Obtiene el nombre del driver.
188
         * @return Nombre del driver
189
         */
190
        public String getDriverName() {
191
                return driver;
192
        }
193
194
        /**
195
         * @return
196
         */
197
        public String getDriverType() {
198
                return driver;
199
        }
200
201
        /**
202
         * Asigna el porcentaje de incremento. Esto es usado por el driver para
203
         * actualizar la variable percent
204
         * @param percent
205
         */
206
        public void setPercent(int percent) {
207
                this.percent = percent;
208
        }
209
210
        /**
211
         * Porcentaje de escritura completado.
212
         * @return Entero con el porcentaje de escritura.
213
         */
214
        public int getPercent() {
215 11522 nacho
                return percent;
216
        }
217 22102 bsanchez
218
        /**
219 10939 nacho
         * Factoria para obtener escritores de los distintos tipos de raster.
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 28498 nbrodin
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
227
                ExtensionPoint point = extensionPoints.get("RasterWriter");
228 26873 vcaballero
//                ExtensionPoint extensionPoint = ExtensionPoint.getExtensionPoint("RasterWriter");
229 22102 bsanchez
230 26873 vcaballero
                if (!point.has(ext))
231 10939 nacho
                        return grw;
232 22102 bsanchez
233 26905 jmvivo
                Class clase = point.get(ext).getExtension();
234 22102 bsanchez
                Class[] args = { String.class };
235 10939 nacho
                try {
236
                        Constructor hazNuevo = clase.getConstructor(args);
237 22102 bsanchez
                        Object[] args2 = { fName };
238 10939 nacho
                        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 26873 vcaballero
255 10939 nacho
        /**
256
         * Factoria para obtener escritores de los distintos tipos de raster.
257 26873 vcaballero
         *
258 10939 nacho
         * @param fName Nombre del fichero.
259
         * @return GeoRasterWriter, o null si hay problemas.
260
         */
261 26873 vcaballero
        public static GeoRasterWriter getWriter(IDataWriter dataWriter,
262
                                                                                                 String outFileName,
263 22102 bsanchez
                                                                                                 int nBands,
264
                                                                                                 AffineTransform at,
265
                                                                                                 int outSizeX,
266
                                                                                                 int outSizeY,
267
                                                                                                 int dataType,
268
                                                                                                 Params params,
269
                                                                                                 IProjection proj) throws NotSupportedExtensionException, RasterDriverException {
270 15953 nbrodin
                return GeoRasterWriter.getWriter(dataWriter, outFileName, nBands, at, outSizeX, outSizeY, dataType, params, proj, true);
271
        }
272 26873 vcaballero
273 15953 nbrodin
        /**
274
         * Factoria para obtener escritores de los distintos tipos de raster.
275 26873 vcaballero
         *
276 15953 nbrodin
         * @param fName Nombre del fichero.
277
         * @return GeoRasterWriter, o null si hay problemas.
278
         */
279 26873 vcaballero
        public static GeoRasterWriter getWriter(IDataWriter dataWriter,
280
                                                                                                 String outFileName,
281 22102 bsanchez
                                                                                                 int nBands,
282
                                                                                                 AffineTransform at,
283
                                                                                                 int outSizeX,
284
                                                                                                 int outSizeY,
285
                                                                                                 int dataType,
286
                                                                                                 Params params,
287
                                                                                                 IProjection proj,
288
                                                                                                 boolean geo) throws NotSupportedExtensionException, RasterDriverException {
289
                String ext = outFileName.toLowerCase().substring(outFileName.lastIndexOf('.') + 1);
290 10939 nacho
                GeoRasterWriter grw = null;
291 26873 vcaballero
                ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager();
292
                ExtensionPoint point=extensionPoints.get("RasterWriter");
293
//                ExtensionPoint extensionPoint = ExtensionPoint.getExtensionPoint("RasterWriter");
294 22102 bsanchez
295 26873 vcaballero
                if (!point.has(ext))
296 10939 nacho
                        return grw;
297 22102 bsanchez
298 26952 vcaballero
                Class clase = point.get(ext).getExtension();
299 22102 bsanchez
                Class[] args = { IDataWriter.class, String.class, Integer.class, AffineTransform.class, Integer.class, Integer.class, Integer.class, Params.class, IProjection.class, Boolean.class };
300 10939 nacho
                try {
301
                        Constructor hazNuevo = clase.getConstructor(args);
302 26873 vcaballero
                        Object [] args2 = {dataWriter, outFileName, new Integer(nBands), at,
303
                                                                new Integer(outSizeX), new Integer(outSizeY), new Integer(dataType),
304 15953 nbrodin
                                                                params, proj, new Boolean(geo)};
305 10939 nacho
                        grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
306
                } catch (SecurityException e) {
307 11237 nacho
                        throw new RasterDriverException("Error SecurityException in open");
308 10939 nacho
                } catch (NoSuchMethodException e) {
309 11237 nacho
                        throw new RasterDriverException("Error NoSuchMethodException in open");
310 10939 nacho
                } catch (IllegalArgumentException e) {
311 11237 nacho
                        throw new RasterDriverException("Error IllegalArgumentException in open");
312 10939 nacho
                } catch (InstantiationException e) {
313 11237 nacho
                        throw new RasterDriverException("Error InstantiationException in open");
314 10939 nacho
                } catch (IllegalAccessException e) {
315 11237 nacho
                        throw new RasterDriverException("Error IllegalAccessException in open");
316 10939 nacho
                } catch (InvocationTargetException e) {
317 20495 nbrodin
                        throw new NotSupportedExtensionException("Error in open. Problemas con las librer?as nativas.");
318 10939 nacho
                }
319
                return grw;
320
        }
321 22102 bsanchez
322 11237 nacho
        /**
323 22102 bsanchez
         * Obtiene los par?metros del driver.
324
         * @return WriterParams
325
         */
326
        public Params getParams() {
327 11237 nacho
                return driverParams;
328
        }
329 22102 bsanchez
330
        /**
331
         * Asigna los par?metros del driver modificados por el cliente.
332
         * @param Params
333
         */
334
        public void setParams(Params params) {
335 11237 nacho
                this.driverParams = params;
336
        }
337 10939 nacho
338 22102 bsanchez
        /**
339
         * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
340
         * @throws IOException
341
         */
342
        public abstract void fileWrite() throws IOException, InterruptedException;
343 10939 nacho
344 12494 nacho
        /**
345 22102 bsanchez
         * Realiza la funci?n de compresi?n a partir de los datos pasados por el
346
         * cliente.
347
         * @throws IOException
348 26873 vcaballero
         * @throws RmfSerializerException
349 22102 bsanchez
         */
350
        public abstract void dataWrite() throws IOException, InterruptedException;
351
352
        /**
353
         * Cierra el driver
354
         */
355
        public abstract void writeClose();
356
357
        /**
358
         * Cancela el grabado de datos
359
         */
360
        public abstract void writeCancel();
361
362
        /**
363 12494 nacho
         * A?ade la proyecci?n Wkt con la que salvar.
364
         * @param wkt
365
         * @throws GdalException
366
         */
367
        public abstract void setWkt(String wkt);
368 22102 bsanchez
369 18040 nbrodin
        /**
370
         * Asigna la interpretaci?n de color para el fichero de salida.
371
         * @param colorInterp Interpretaci?n de color
372
         */
373
        public void setColorBandsInterpretation(String[] colorInterp) {
374 22102 bsanchez
                if (colorInterp != null) {
375 18040 nbrodin
                        this.colorInterp = new DatasetColorInterpretation();
376
                        this.colorInterp.initColorInterpretation(colorInterp.length);
377 26905 jmvivo
                        for (int i = 0; i < colorInterp.length; i++)
378 18040 nbrodin
                                this.colorInterp.setColorInterpValue(i, colorInterp[i]);
379
                }
380
        }
381 22102 bsanchez
382
        /**
383
         * M?todo que pregunta si la extensi?n pasada por par?metro est? soportada con
384
         * el tipo y n?mero de bandas indicadas.
385 26873 vcaballero
         *
386 22102 bsanchez
         * @param dataType Tipo de dato
387
         * @param bands N?mero de bandas
388
         * @param extensi?n
389
         * @return true si est? soportada y false si no lo est?
390
         */
391 11967 nacho
        public boolean isSupportedThisExtension(String ext, int dataType, int bands) {
392 21345 bsanchez
                WriteFileFormatFeatures features = (WriteFileFormatFeatures) fileFeature.get(ext);
393
                if (features == null)
394 11967 nacho
                        return false;
395 13629 nacho
                int[] bandsSupported = features.getNBandsSupported();
396
                for (int i = 0; i < bandsSupported.length; i++) {
397 21345 bsanchez
                        if (bandsSupported[i] == -1)
398 13629 nacho
                                break;
399 21345 bsanchez
                        if (bandsSupported[i] >= bands)
400 13629 nacho
                                break;
401 11967 nacho
                        return false;
402 13629 nacho
                }
403 11967 nacho
                int[] dt = features.getDataTypesSupported();
404
                for (int i = 0; i < dt.length; i++)
405 21345 bsanchez
                        if (dataType == dt[i])
406 11967 nacho
                                return true;
407
                return false;
408
        }
409 29510 nbrodin
410
        /**
411
         * Assigns the object to be cancelled
412 29535 nbrodin
         *
413 29510 nbrodin
         * @param cancellable
414
         */
415
        public void setCancellableRasterDriver(IExternalCancellable cancellable) {
416
                this.extCancellable = cancellable;
417
        }
418 21345 bsanchez
}