Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / io / GeoRasterWriter.java @ 4578

History | View | Annotate | Download (8.15 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.io;
25

    
26
import java.io.IOException;
27
import java.lang.reflect.Constructor;
28
import java.lang.reflect.InvocationTargetException;
29
import java.util.Iterator;
30
import java.util.Map;
31
import java.util.Set;
32
import java.util.TreeMap;
33

    
34
import org.cresques.geo.ViewPortData;
35
import org.cresques.px.PxRaster;
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
 * @author Nacho Brodin (brodin_ign@gva.es)
43
 */
44
public abstract class GeoRasterWriter {
45
    private static TreeMap                 supportedExtensions = null;
46
    protected static TreeMap         typeList = new TreeMap();
47
    protected String                         outFileName = null;
48
    protected String                         inFileName = null;
49
    protected int                                 sizeWindowX = 0;
50
    protected int                                 sizeWindowY = 0;
51
    protected int                                 ulX = 0;
52
    protected int                                 ulY = 0;
53
    protected PxRaster                         currentRaster;
54
    protected IDataWriter                 dataWriter = null;
55
    protected int                                 nBands = 0;
56
    protected String                         ident = null;
57
    protected String                         driver = null;
58

    
59
    /**
60
     * Registra un formato de escritura
61
     * @param ext        Extensi?n del fichero registrado
62
     * @param clase        Clase que maneja el formato registrado
63
     */
64
    public static void registerWriterExtension(String ext, Class clase) {
65
        if (supportedExtensions == null) {
66
            supportedExtensions = new TreeMap();
67
        }
68

    
69
        ext = ext.toLowerCase();
70
        System.out.println("Write RASTER: extension '" + ext + "' supported.");
71
        supportedExtensions.put(ext, clase);
72
    }
73

    
74
    /**
75
     * Obtiene la lista de extensiones registradas
76
     * @return Lista de extensiones registradas o null si no hay ninguna
77
     */
78
    public static String[] getDriversExtensions(){
79
            if (supportedExtensions.size() == 0)
80
                    return null;
81
            String[] list = new String[supportedExtensions.size()];
82
            Set values = supportedExtensions.entrySet();
83
            int i = 0;
84
            for (Iterator it=values.iterator(); it.hasNext(); ) {
85
            list[i] = (String)((Map.Entry)it.next()).getKey();
86
            i++;
87
        }
88
            
89
            return list;
90
    }
91
    
92
    /**
93
     * Obtiene la lista de tipos de driver
94
     * @return Lista de tipos de driver registradas o null si no hay ninguno
95
     */
96
    public static String[] getDriversType(){
97
            if (typeList.size() == 0)
98
                    return null;
99
            String[] list = new String[typeList.size()];
100
            Set values = typeList.entrySet();
101
            int i = 0;
102
            for (Iterator it=values.iterator(); it.hasNext(); ) {
103
            list[i] = (String)((Map.Entry)it.next()).getValue();
104
            i++;
105
        }
106
            
107
            return list;
108
    }
109
    
110
    /**
111
     * Obtiene el tipo de driver a partir de la extensi?n
112
     * @param ext        Extensi?n
113
     * @return        Tipo
114
     */
115
    public static String getDriverType(String ext){
116
            return (String)typeList.get(ext);
117
    }
118
    
119
    /**
120
     * Devuelve el n?mero de drivers soportados
121
     * @return N?mero de drivers soportados
122
     */
123
    public static int getNDrivers() {
124
        return supportedExtensions.size();
125
    }
126
    
127
    /**
128
     * Devuelve el n?mero de tipos de driver registrados
129
     * @return N?mero de tipos de driver soportados
130
     */
131
    public static int getNTypes() {
132
        return typeList.size();
133
    }
134

    
135
    /**
136
     * Devuelve el identificador del driver
137
     * @return        Identificador del driver
138
     */
139
    public String getIdent() {
140
        return ident;
141
    }
142

    
143
    /**
144
     * Obtiene el nombre del driver.
145
     * @return        Nombre del driver
146
     */
147
    public String getDriverName() {
148
        return driver;
149
    }
150

    
151
    /**
152
     * 
153
     * @return
154
     */
155
    public String getDriverType() {
156
        return driver;
157
    }
158
    
159
    /**
160
         * Factoria para obtener escritores de los distintos tipos de raster.
161
         * 
162
         * @param fName Nombre del fichero.
163
         * @return GeoRasterWriter, o null si hay problemas.
164
         */
165
        public static GeoRasterWriter getWriter(String fName) {
166
                String ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1);
167
                GeoRasterWriter grw = null;
168
                
169
                if (!supportedExtensions.containsKey(ext)) 
170
                        return grw;
171
                
172
                Class clase = (Class) supportedExtensions.get(ext);
173
                Class [] args = {String.class};
174
                try {
175
                        Constructor hazNuevo = clase.getConstructor(args);
176
                        Object [] args2 = {fName};
177
                        grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
178
                } catch (SecurityException e) {
179
                        e.printStackTrace();
180
                } catch (NoSuchMethodException e) {
181
                        e.printStackTrace();
182
                } catch (IllegalArgumentException e) {
183
                        e.printStackTrace();
184
                } catch (InstantiationException e) {
185
                        e.printStackTrace();
186
                } catch (IllegalAccessException e) {
187
                        e.printStackTrace();
188
                } catch (InvocationTargetException e) {
189
                        e.printStackTrace();
190
                }
191
                return grw;
192
        }
193
        
194
        /**
195
         * Factoria para obtener escritores de los distintos tipos de raster.
196
         * 
197
         * @param fName Nombre del fichero.
198
         * @return GeoRasterWriter, o null si hay problemas.
199
         */
200
        public static GeoRasterWriter getWriter(IDataWriter dataWriter, 
201
                                                                                     String outFileName, 
202
                                                                                     int blockSize, 
203
                                                                                     int nBands,
204
                                                                                     ViewPortData vp,
205
                                                                                     int compresion,
206
                                                                                     int outSizeX,
207
                                                                                     int outSizeY) {
208
                String ext = outFileName.toLowerCase().substring(outFileName.lastIndexOf('.')+1);
209
                GeoRasterWriter grw = null;
210
                
211
                if (!supportedExtensions.containsKey(ext)) 
212
                        return grw;
213
                
214
                Class clase = (Class) supportedExtensions.get(ext);
215
                Class [] args = {IDataWriter.class, String.class, Integer.class, Integer.class, 
216
                                                 ViewPortData.class, Integer.class, Integer.class, Integer.class};
217
                try {
218
                        Constructor hazNuevo = clase.getConstructor(args);
219
                        Object [] args2 = {dataWriter, outFileName, new Integer(blockSize), new Integer(nBands), 
220
                                                                vp, new Integer(compresion), new Integer(outSizeX), new Integer(outSizeY)};
221
                        grw = (GeoRasterWriter) hazNuevo.newInstance(args2);
222
                } catch (SecurityException e) {
223
                        e.printStackTrace();
224
                } catch (NoSuchMethodException e) {
225
                        e.printStackTrace();
226
                } catch (IllegalArgumentException e) {
227
                        e.printStackTrace();
228
                } catch (InstantiationException e) {
229
                        e.printStackTrace();
230
                } catch (IllegalAccessException e) {
231
                        e.printStackTrace();
232
                } catch (InvocationTargetException e) {
233
                        e.printStackTrace();
234
                }
235
                return grw;
236
        }
237
        
238
    /**
239
     * Asigna propiedades al driver a partir de un vector de
240
     * strings donde cada elemento tiene la estructura de
241
     * propiedad=valor.
242
     * @param props        Propiedades
243
     */
244
    public abstract void setProps(String[] props);
245

    
246
    /**
247
     * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
248
     * @throws IOException
249
     */
250
    public abstract void fileWrite() throws IOException;
251

    
252
    /**
253
     * Realiza la funci?n de compresi?n a partir de los datos pasados por el cliente.
254
     * @throws IOException
255
     */
256
    public abstract void dataWrite() throws IOException;
257

    
258
    /**
259
     * Cierra el driver
260
     */
261
    public abstract void writeClose();
262
    
263
    /**
264
     * Cancela el grabado de datos
265
     */
266
    public abstract void writeCancel();
267

    
268
    /**
269
     * Devuelve la configuraci?n de la ventana de dialogo
270
     * para las propiedades del driver de escritura que se est? tratando.
271
     * @return Texto XML con las propiedades
272
     */
273
    public abstract String getXMLPropertiesDialog();
274
}