Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1004 / libraries / libCq CMS for java.old / src / org / cresques / io / GeoRasterWriter.java @ 12319

History | View | Annotate | Download (8.49 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
    
46
        public static int                        blockSizeDefault = 256;
47
        
48
        private static TreeMap                 supportedExtensions = null;
49
    protected static TreeMap         typeList = new TreeMap();
50
    protected String                         outFileName = null;
51
    protected String                         inFileName = null;
52
    protected int                                 sizeWindowX = 0;
53
    protected int                                 sizeWindowY = 0;
54
    protected int                                 ulX = 0;
55
    protected int                                 ulY = 0;
56
    protected PxRaster                         currentRaster;
57
    protected IDataWriter                 dataWriter = null;
58
    protected int                                 nBands = 0;
59
    protected String                         ident = null;
60
    protected String                         driver = null;
61
    protected static boolean         writeTfw = true;
62
    
63
    /**
64
     * Registra un formato de escritura
65
     * @param ext        Extensi?n del fichero registrado
66
     * @param clase        Clase que maneja el formato registrado
67
     */
68
    public static void registerWriterExtension(String ext, Class clase) {
69
        if (supportedExtensions == null) {
70
            supportedExtensions = new TreeMap();
71
        }
72

    
73
        ext = ext.toLowerCase();
74
        System.out.println("Write RASTER: extension '" + ext + "' supported.");
75
        supportedExtensions.put(ext, clase);
76
    }
77

    
78
    /**
79
     * Pone a true o false el flag que dice si se escribir? o no 
80
     * el fichero de georreferenciaci?n.
81
     * @param write
82
     */
83
    public void setWriteTfw(boolean write) {
84
            writeTfw = write;
85
    }
86
    
87
    /**
88
     * Obtiene la lista de extensiones registradas
89
     * @return Lista de extensiones registradas o null si no hay ninguna
90
     */
91
    public static String[] getDriversExtensions(){
92
            if (supportedExtensions.size() == 0)
93
                    return null;
94
            String[] list = new String[supportedExtensions.size()];
95
            Set values = supportedExtensions.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 tipos de driver
107
     * @return Lista de tipos de driver registradas o null si no hay ninguno
108
     */
109
    public static String[] getDriversType(){
110
            if (typeList.size() == 0)
111
                    return null;
112
            String[] list = new String[typeList.size()];
113
            Set values = typeList.entrySet();
114
            int i = 0;
115
            for (Iterator it=values.iterator(); it.hasNext(); ) {
116
            list[i] = (String)((Map.Entry)it.next()).getValue();
117
            i++;
118
        }
119
            
120
            return list;
121
    }
122
    
123
    /**
124
     * Obtiene el tipo de driver a partir de la extensi?n
125
     * @param ext        Extensi?n
126
     * @return        Tipo
127
     */
128
    public static String getDriverType(String ext){
129
            return (String)typeList.get(ext);
130
    }
131
    
132
    /**
133
     * Devuelve el n?mero de drivers soportados
134
     * @return N?mero de drivers soportados
135
     */
136
    public static int getNDrivers() {
137
        return supportedExtensions.size();
138
    }
139
    
140
    /**
141
     * Devuelve el n?mero de tipos de driver registrados
142
     * @return N?mero de tipos de driver soportados
143
     */
144
    public static int getNTypes() {
145
        return typeList.size();
146
    }
147

    
148
    /**
149
     * Devuelve el identificador del driver
150
     * @return        Identificador del driver
151
     */
152
    public String getIdent() {
153
        return ident;
154
    }
155

    
156
    /**
157
     * Obtiene el nombre del driver.
158
     * @return        Nombre del driver
159
     */
160
    public String getDriverName() {
161
        return driver;
162
    }
163

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

    
260
    /**
261
     * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
262
     * @throws IOException
263
     */
264
    public abstract void fileWrite() throws IOException;
265

    
266
    /**
267
     * Realiza la funci?n de compresi?n a partir de los datos pasados por el cliente.
268
     * @throws IOException
269
     */
270
    public abstract void dataWrite() throws IOException;
271

    
272
    /**
273
     * Cierra el driver
274
     */
275
    public abstract void writeClose();
276
    
277
    /**
278
     * Cancela el grabado de datos
279
     */
280
    public abstract void writeCancel();
281

    
282
    /**
283
     * Devuelve la configuraci?n de la ventana de dialogo
284
     * para las propiedades del driver de escritura que se est? tratando.
285
     * @return Texto XML con las propiedades
286
     */
287
    public abstract String getXMLPropertiesDialog();
288
}