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 @ 3739
History | View | Annotate | Download (5.93 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 extends Driver { |
40 |
// private native long createCopyNat(long cPtr, String file, long src, int bstrict);
|
41 |
// private native long createCopyParamsNat(long cPtr, String file, long src, int bstrict, Options opc);
|
42 |
// private native long createNat(long cPtr, String filename, int nXSize, int nYSize, int nBands, int nType, Options opc);
|
43 |
|
44 |
/**
|
45 |
* Constructor de Driver pasandole como par?metro la referencia al objeto
|
46 |
* GdalDriver en C
|
47 |
*
|
48 |
* @param cPtr direcci?n de memoria del objeto
|
49 |
*/
|
50 |
public GdalDriver(long cPtr) { |
51 |
super(cPtr, true); |
52 |
} |
53 |
|
54 |
public GdalDriver(Driver ptrdrv) { |
55 |
this(Driver.getCPtr(ptrdrv)); |
56 |
} |
57 |
|
58 |
/**
|
59 |
* Crea una copia de una im?gen a partir de un dataset de origen especificado.
|
60 |
* @param file Nombre del fichero sobre el cual se guardar? la copia
|
61 |
* @param src Dataset fuente a copiar
|
62 |
* @param bstrict TRUE si la copia debe ser estrictamente equivalente y FALSE indica que la copia puede
|
63 |
* adaptarse a las necesidades del formato de salida
|
64 |
* @return Gdal Dataset de la im?gen de salida
|
65 |
* @throws GdalException
|
66 |
*/
|
67 |
public Gdal createCopy(String file, Gdal src, boolean bstrict) throws GdalException { |
68 |
String path = file.substring(0, file.lastIndexOf(File.separator)); |
69 |
File f = new File(path); |
70 |
if (!f.canWrite())
|
71 |
throw new GdalException("Ruta de archivo incorrecta."); |
72 |
f = null;
|
73 |
|
74 |
if (src == null) |
75 |
throw new GdalException("El objeto Gdal es null"); |
76 |
|
77 |
// if (cPtr == 0)
|
78 |
// throw new GdalException("No se ha podido acceder al archivo.");
|
79 |
|
80 |
Dataset data = gdal.Open(file, bstrict ? 1 : 0); |
81 |
Dataset copyDataset = CreateCopy(file, data, bstrict ? 1 : 0); |
82 |
|
83 |
// if (ptr == 0)
|
84 |
// throw new GdalException("No se ha podido crear la copia");
|
85 |
|
86 |
return new Gdal(copyDataset); |
87 |
} |
88 |
|
89 |
|
90 |
/**
|
91 |
* A partir de las opciones en forma de vector de Strings pasadas por el usuario donde cada
|
92 |
* elemento del vector tiene la forma VARIABLE=VALOR crea el objeto Options para que sea accesible
|
93 |
* a las funciones JNI desde C.
|
94 |
* @param params Vector de strigs con las opciones
|
95 |
* @return Options Objeto de opciones
|
96 |
*/
|
97 |
private Options selectOptions(String[] params) { |
98 |
if (params == null) |
99 |
return null; |
100 |
|
101 |
Options opc = new Options(params.length);
|
102 |
StringTokenizer st;
|
103 |
for (int i = 0; i < params.length; i++) { |
104 |
st = new StringTokenizer(params[i], "="); |
105 |
String var = st.nextToken();
|
106 |
String dato = st.nextToken();
|
107 |
opc.addOption(var, dato); |
108 |
} |
109 |
return opc;
|
110 |
} |
111 |
|
112 |
/**
|
113 |
* Crea una copia de una im?gen a partir de un dataset de origen especificado y unos par?metros dados.
|
114 |
* @param file Nombre del fichero sobre el cual se guardar? la copia
|
115 |
* @param src Dataset fuente a copiar
|
116 |
* @param bstrict TRUE si la copia debe ser estrictamente equivalente y FALSE indica que la copia puede
|
117 |
* adaptarse a las necesidades del formato de salida
|
118 |
* @param params Vector de strigs con las opciones de la copia
|
119 |
* @return Gdal Dataset de la im?gen de salida
|
120 |
* @throws GdalException
|
121 |
*/
|
122 |
public Gdal createCopy(String file, Gdal src, boolean bstrict, String[] params) throws GdalException { |
123 |
String path = file.substring(0, file.lastIndexOf(File.separator)); |
124 |
File f = new File(path); |
125 |
if (!f.canWrite())
|
126 |
throw new GdalException("Ruta de archivo incorrecta."); |
127 |
f = null;
|
128 |
|
129 |
// if (cPtr == 0)
|
130 |
// throw new GdalException("No se ha podido acceder al archivo.");
|
131 |
|
132 |
// if (src == null)
|
133 |
// throw new GdalException("El objeto Gdal es null");
|
134 |
|
135 |
Dataset data = gdal.Open(file, bstrict ? 1 : 0); |
136 |
Dataset copyDataset = CreateCopy(file, data, bstrict ? 1 : 0, params); |
137 |
|
138 |
if (copyDataset == null) |
139 |
throw new GdalException("No se ha podido crear la copia"); |
140 |
|
141 |
return new Gdal(copyDataset); |
142 |
} |
143 |
|
144 |
|
145 |
/**
|
146 |
* Crea un nuevo dataset con el driver actual
|
147 |
*
|
148 |
* @param filename Nombre del dataset a crear
|
149 |
* @param nXSize Ancho en pixels
|
150 |
* @param nYSize Alto en pixels
|
151 |
* @param nBands N?mero de bandas
|
152 |
* @param nType Tipo de raster
|
153 |
* @param params lista de par?metros especificos del driver
|
154 |
*/
|
155 |
public Gdal create(String filename, int nXSize, int nYSize, int nBands, int nType, String[] params) throws GdalException { |
156 |
// if (cPtr == 0)
|
157 |
// throw new GdalException("No se ha podido acceder al archivo.");
|
158 |
|
159 |
Driver driver = (Driver) this; |
160 |
byte ptext[] = filename.getBytes(Charset.forName("ISO_8859_1")); |
161 |
String value = new String(ptext, Charset.forName("UTF-8")); |
162 |
Dataset data = driver.Create(value, nXSize, nYSize, nBands, nType, StringArrayToVector(params)); |
163 |
|
164 |
return new Gdal(data); |
165 |
} |
166 |
|
167 |
private static Vector StringArrayToVector(String[] options) |
168 |
{ |
169 |
if (options == null) |
170 |
return null; |
171 |
Vector v = new Vector(); |
172 |
for(int i=0;i<options.length;i++) |
173 |
v.addElement(options[i]); |
174 |
return v;
|
175 |
} |
176 |
|
177 |
} |