Statistics
| Revision:

gvsig-raster / org.gvsig.raster.gdal / tags / pre-remove-jgdal / org.gvsig.raster.gdal / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / jgdal / GdalDriver.java @ 3741

History | View | Annotate | Download (5.71 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.jgdal;
23

    
24
import java.io.File;
25
import java.nio.charset.Charset;
26
import java.util.StringTokenizer;
27
import java.util.Vector;
28

    
29
import org.gdal.gdal.Dataset;
30
import org.gdal.gdal.Driver;
31
import org.gdal.gdal.gdal;
32
/**
33
 * Representa un driver de un tipo de im?gen 
34
 * 
35
 * @author Nacho Brodin (nachobrodin@gmail.com).<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
36
 * @version 0.0
37
 * @link http://www.gvsig.gva.es
38
 */
39
public class GdalDriver{
40
        private Driver driver;
41

    
42
//        private native long createCopyNat(long cPtr, String file, long src, int bstrict);
43
//        private native long createCopyParamsNat(long cPtr, String file, long src, int bstrict, Options opc);
44
//        private native long createNat(long cPtr, String filename, int nXSize, int nYSize, int nBands, int nType, Options opc);
45
        
46

    
47
        public GdalDriver(Driver drv) {
48
                this.driver = drv;
49
        }
50

    
51
        /**
52
         * Crea una copia de una im?gen a partir de un dataset de origen especificado.
53
         * @param file        Nombre del fichero sobre el cual se guardar? la copia
54
         * @param src        Dataset fuente a copiar
55
         * @param bstrict        TRUE si la copia debe ser estrictamente equivalente y FALSE indica que la copia puede
56
         * adaptarse a las necesidades del formato de salida
57
         * @return Gdal        Dataset de la im?gen de salida
58
         * @throws GdalException
59
         */
60
        public Gdal createCopy(String file, Gdal src, boolean bstrict) throws GdalException {
61
                String path = file.substring(0, file.lastIndexOf(File.separator));
62
                File f = new File(path);
63
                if (!f.canWrite())
64
                        throw new GdalException("Ruta de archivo incorrecta.");
65
                f = null;
66

    
67
                if (src == null)
68
                        throw new GdalException("El objeto Gdal es null");
69

    
70
//                if (cPtr == 0)
71
//                        throw new GdalException("No se ha podido acceder al archivo.");
72

    
73
                Dataset data = gdal.Open(file, bstrict ? 1 : 0);
74
                Dataset copyDataset = this.driver.CreateCopy(file, data, bstrict ? 1 : 0);
75
        
76
//                if (ptr == 0)
77
//                        throw new GdalException("No se ha podido crear la copia");
78

    
79
                return new Gdal(copyDataset);
80
        }
81
        
82
        
83
        /**
84
         * A partir de las opciones en forma de vector de Strings pasadas por el usuario donde cada
85
         * elemento del vector tiene la forma VARIABLE=VALOR crea el objeto Options para que sea accesible
86
         * a las funciones JNI desde C.
87
         * @param params        Vector de strigs con las opciones
88
         * @return Options        Objeto de opciones
89
         */
90
        private Options selectOptions(String[] params) {
91
                if (params == null)
92
                        return null;
93

    
94
                Options opc = new Options(params.length);
95
                StringTokenizer st;
96
                for (int i = 0; i < params.length; i++) {
97
                        st = new StringTokenizer(params[i], "=");
98
                        String var = st.nextToken();
99
                        String dato = st.nextToken();
100
                        opc.addOption(var, dato);
101
                }
102
                return opc;
103
        }
104
        
105
        /**
106
         * Crea una copia de una im?gen a partir de un dataset de origen especificado y unos par?metros dados.
107
         * @param file        Nombre del fichero sobre el cual se guardar? la copia
108
         * @param src        Dataset fuente a copiar
109
         * @param bstrict        TRUE si la copia debe ser estrictamente equivalente y FALSE indica que la copia puede
110
         * adaptarse a las necesidades del formato de salida
111
         * @param params        Vector de strigs con las opciones de la copia
112
         * @return Gdal        Dataset de la im?gen de salida
113
         * @throws GdalException
114
         */
115
        public Gdal createCopy(String file, Gdal src, boolean bstrict, String[] params) throws GdalException {
116
                String path = file.substring(0, file.lastIndexOf(File.separator));
117
                File f = new File(path);
118
                if (!f.canWrite())
119
                        throw new GdalException("Ruta de archivo incorrecta.");
120
                f = null;
121

    
122
//                if (cPtr == 0)
123
//                        throw new GdalException("No se ha podido acceder al archivo.");
124

    
125
//                if (src == null)
126
//                        throw new GdalException("El objeto Gdal es null");
127

    
128
                Dataset data = gdal.Open(file, bstrict ? 1 : 0);
129
                Dataset copyDataset = this.driver.CreateCopy(file, data, bstrict ? 1 : 0, params);
130

    
131
                if (copyDataset == null)
132
                        throw new GdalException("No se ha podido crear la copia");
133

    
134
                return new Gdal(copyDataset);
135
        }
136
        
137
        
138
        /**
139
         * Crea un nuevo dataset con el driver actual
140
         * 
141
         * @param filename        Nombre del dataset a crear
142
         * @param nXSize        Ancho en pixels
143
         * @param nYSize        Alto en pixels
144
         * @param nBands        N?mero de bandas
145
         * @param nType        Tipo de raster
146
         * @param params        lista de par?metros especificos del driver
147
         */
148
        public Gdal create(String filename, int nXSize, int nYSize, int nBands, int nType, String[] params) throws GdalException {
149
//                if (cPtr == 0)
150
//                        throw new GdalException("No se ha podido acceder al archivo.");
151

    
152
                byte ptext[] = filename.getBytes(Charset.forName("ISO_8859_1")); 
153
                String value = new String(ptext, Charset.forName("UTF-8")); 
154
                Dataset data = this.driver.Create(value, nXSize, nYSize, nBands, nType, StringArrayToVector(params));
155

    
156
                return new Gdal(data);
157
        }
158
        
159
        private static Vector StringArrayToVector(String[] options)
160
          {
161
              if (options == null)
162
                return null;
163
              Vector v = new Vector();
164
              for(int i=0;i<options.length;i++)
165
                v.addElement(options[i]);
166
              return v;
167
          }
168
        
169
}