root / branches / v10 / libraries / libjni-gdal / src / es / gva / cit / jgdal / Gdal.java @ 10988
History | View | Annotate | Download (10.5 KB)
1 |
/**********************************************************************
|
---|---|
2 |
* $Id: Gdal.java 10988 2007-03-30 11:54:34Z nacho $
|
3 |
*
|
4 |
* Name: Gdal.java
|
5 |
* Project: JGDAL. Interface java to gdal (Frank Warmerdam).
|
6 |
* Purpose: Dataset's Basic Funcions.
|
7 |
* Author: Nacho Brodin, brodin_ign@gva.es
|
8 |
*
|
9 |
**********************************************************************/
|
10 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
11 |
*
|
12 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
13 |
*
|
14 |
* This program is free software; you can redistribute it and/or
|
15 |
* modify it under the terms of the GNU General Public License
|
16 |
* as published by the Free Software Foundation; either version 2
|
17 |
* of the License, or (at your option) any later version.
|
18 |
*
|
19 |
* This program is distributed in the hope that it will be useful,
|
20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22 |
* GNU General Public License for more details.
|
23 |
*
|
24 |
* You should have received a copy of the GNU General Public License
|
25 |
* along with this program; if not, write to the Free Software
|
26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
27 |
*
|
28 |
* For more information, contact:
|
29 |
*
|
30 |
* Generalitat Valenciana
|
31 |
* Conselleria d'Infraestructures i Transport
|
32 |
* Av. Blasco Ib??ez, 50
|
33 |
* 46010 VALENCIA
|
34 |
* SPAIN
|
35 |
*
|
36 |
* +34 963862235
|
37 |
* gvsig@gva.es
|
38 |
* www.gvsig.gva.es
|
39 |
*
|
40 |
* or
|
41 |
*
|
42 |
* IVER T.I. S.A
|
43 |
* Salamanca 50
|
44 |
* 46005 Valencia
|
45 |
* Spain
|
46 |
*
|
47 |
* +34 963163400
|
48 |
* dac@iver.es
|
49 |
*
|
50 |
* [01] 01-Oct-2005 nbt New call to JNI function openArrayNat to convert name string to char array.
|
51 |
*/
|
52 |
|
53 |
package es.gva.cit.jgdal; |
54 |
|
55 |
|
56 |
import java.io.*; |
57 |
import java.util.Date; |
58 |
import sun.misc.Signal; |
59 |
import sun.misc.SignalHandler; |
60 |
import java.lang.reflect.*; |
61 |
|
62 |
//import es.gva.cit.jgdal.GdalException;
|
63 |
|
64 |
/**
|
65 |
* Contiene las funcionalidades necesarias para el acceso a los
|
66 |
* elementos de un dataset de gdal correspondiente a una im?gen
|
67 |
*
|
68 |
* @author Nacho Brodin <brodin_ign@gva.es>.<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
|
69 |
* @version 0.0
|
70 |
* @link http://www.gvsig.gva.es
|
71 |
*/
|
72 |
|
73 |
public class Gdal extends JNIBase{ |
74 |
//CONSTANTES
|
75 |
|
76 |
//GDALAccess
|
77 |
public static int GA_ReadOnly=0; |
78 |
public static int GA_Update=1; |
79 |
|
80 |
//GDALDataType
|
81 |
public static int GDT_Unknown = 0; |
82 |
public static int GDT_Byte = 1; //Buffer byte (8) |
83 |
public static int GDT_UInt16 = 2; //Buffer short (16) |
84 |
public static int GDT_Int16 = 3; //Buffer short (16) |
85 |
public static int GDT_UInt32 = 4; //Buffer int (32) |
86 |
public static int GDT_Int32 = 5; //Buffer int (32) |
87 |
public static int GDT_Float32 = 6; //Buffer float (32) |
88 |
public static int GDT_Float64 = 7; //Buffer double (64) |
89 |
public static int GDT_CInt16 = 8; //Buffer short (16) |
90 |
public static int GDT_CInt32 = 9; //Buffer int (32) |
91 |
public static int GDT_CFloat32 = 10; //Buffer float (32) |
92 |
public static int GDT_CFloat64 = 11; //Buffer double (64) |
93 |
public static int GDT_TypeCount = 12; |
94 |
|
95 |
private String pszFilename = ""; |
96 |
|
97 |
private native long openNat(String pszFilename, int access); |
98 |
private native long openArrayNat(byte[] pszFilename, int access); |
99 |
private native int getGeoTransformNat(long cPtr,GeoTransform adfgeotransform); |
100 |
private native int setGeoTransformNat(long cPtr,GeoTransform adfgeotransform); |
101 |
private native String[] getMetadataNat(long cPtr,String pszDomain); |
102 |
private native String getProjectionRefNat(long cPtr); |
103 |
private native void closeNat(long cPtr); |
104 |
private native int getRasterBandNat(long cPtr,int hBand); |
105 |
private native int setProjectionNat(long cPtr, String proj); |
106 |
private native String getDriverShortNameNat(long cPtr); |
107 |
|
108 |
//private static native long getGDALDriverManagerNat();
|
109 |
private static native long getDriverByNameNat(String name); |
110 |
private native String getColorInterpretationNameNat(long cPtr, int colorInterp); |
111 |
|
112 |
/**
|
113 |
*Constructor a partir de la direcci?n de memoria
|
114 |
*/
|
115 |
public Gdal(long cPtr){ |
116 |
this.cPtr=cPtr;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
*Constructor generico
|
121 |
*/
|
122 |
public Gdal(){}
|
123 |
|
124 |
/**
|
125 |
* Devuelve la direcci?n de memoria del objeto dataset en C.
|
126 |
*/
|
127 |
public long getPtro(){return cPtr;} |
128 |
|
129 |
/**
|
130 |
* Abre el fichero de im?gen.
|
131 |
*
|
132 |
* @param pszFilename Nombre del fichero.
|
133 |
* @param access Apertura en solo lectura o escritura.
|
134 |
* @throws GdalException
|
135 |
*/
|
136 |
public void open(String pszFilename, int access)throws GdalException, IOException { |
137 |
File f = new File( pszFilename ); |
138 |
if(!f.exists())
|
139 |
throw new GdalException("El archivo "+pszFilename+" no existe"); |
140 |
|
141 |
if(!f.canRead())
|
142 |
throw new GdalException("El archivo no puede leerse"); |
143 |
|
144 |
/*String os = System.getProperty("os.name");
|
145 |
if(os.toLowerCase().startsWith("windows"))
|
146 |
DiagSignalHandler.install("ABRT");
|
147 |
else
|
148 |
DiagSignalHandler.install("ALRM");*/
|
149 |
//DiagSignalHandler.install("ALRM");
|
150 |
|
151 |
cPtr=openArrayNat(pszFilename.getBytes(), access); |
152 |
//cPtr=openNat(pszFilename, access);
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* Obtiene un array de Strings con los metadatos
|
157 |
*
|
158 |
* @throws GdalException
|
159 |
* @return Array de Strings que corresponden a los metadatos que ofrece la im?gen
|
160 |
*/
|
161 |
public String[] getMetadata()throws GdalException { |
162 |
String[] res = getMetadataNat(cPtr,null); |
163 |
if(res == null) |
164 |
return new String[0]; |
165 |
else return res; |
166 |
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Obtiene el n?mero de bandas de la im?gen
|
171 |
*
|
172 |
* @throws GdalException
|
173 |
* @param hBand Entero que corresponde al n?mero de banda que se quiere recuperar
|
174 |
* @return Objeto GdalRasterBand que representa la banda recuperada
|
175 |
*/
|
176 |
|
177 |
public GdalRasterBand getRasterBand(int hBand)throws GdalException { |
178 |
long cPtr_rb;
|
179 |
|
180 |
cPtr_rb = getRasterBandNat(cPtr,hBand); |
181 |
|
182 |
return new GdalRasterBand(cPtr_rb); |
183 |
} |
184 |
|
185 |
|
186 |
|
187 |
/**
|
188 |
* Obtiene la dimensi?n del raster en el eje X.
|
189 |
*
|
190 |
* @return Devuelve un entero con la longitud de la im?gen en el eje X en pixels.
|
191 |
* @throws GdalException
|
192 |
*/
|
193 |
public int getRasterXSize()throws GdalException { |
194 |
String msg1="Error en GDALGetRasterXSize. La llamada GDALOpen no tuvo ?xito"; |
195 |
String msg2="Error en tama?o X"; |
196 |
return baseSimpleFunctions(5,msg1,msg2); |
197 |
} |
198 |
|
199 |
|
200 |
/**
|
201 |
* Obtiene la dimensi?n del raster en el eje Y.
|
202 |
*
|
203 |
* @return Devuelve un entero con la longitud de la im?gen en el eje Y en pixels.
|
204 |
* @throws GdalException
|
205 |
*/
|
206 |
public int getRasterYSize()throws GdalException { |
207 |
String msg1="Error en GDALGetRasterYSize. La llamada GDALOpen no tuvo ?xito"; |
208 |
String msg2="Error en tama?o Y"; |
209 |
return baseSimpleFunctions(6,msg1,msg2); |
210 |
} |
211 |
|
212 |
|
213 |
/**
|
214 |
* Obtiene el n?mero de bandas de la im?gen
|
215 |
*
|
216 |
* @return Devuelve un entero con el n?mero de bandas que contiene la im?gen.
|
217 |
* @throws GdalException
|
218 |
*/
|
219 |
public int getRasterCount()throws GdalException { |
220 |
String msg1="Error en GDALGetRasterCount. . La llamada GDALOpen no tuvo ?xito"; |
221 |
String msg2="Error en el conteo de n?mero de bandas"; |
222 |
return baseSimpleFunctions(7,msg1,msg2); |
223 |
} |
224 |
|
225 |
|
226 |
/**
|
227 |
* Obtiene el vector geoTransform de la im?gen que contiene los valores Origen y pixelSize
|
228 |
*
|
229 |
* @return Devuelve un vector de doubles que contiene los valores de coordenadas de origen y pixelSize.
|
230 |
* @throws GdalException
|
231 |
*/
|
232 |
public GeoTransform getGeoTransform()throws GdalException { |
233 |
GeoTransform gt=new GeoTransform();
|
234 |
|
235 |
if(getGeoTransformNat(cPtr,gt) < 0) |
236 |
throw new GdalException("Error en getGeoTransform(). No se han obtenido valores para geoTransform."); |
237 |
else{
|
238 |
|
239 |
return gt;
|
240 |
} |
241 |
} |
242 |
|
243 |
/**
|
244 |
* Obtiene el Nombre corto asociado al driver del dataset
|
245 |
*
|
246 |
* @return Cadena con el nombre del driver
|
247 |
* @throws GdalException
|
248 |
*/
|
249 |
public String getDriverShortName()throws GdalException { |
250 |
String shortName = getDriverShortNameNat(cPtr);
|
251 |
|
252 |
if(shortName == null) |
253 |
throw new GdalException("Error en getDriverShortName(). No ha podido obtenerse el driver"); |
254 |
else
|
255 |
return shortName;
|
256 |
} |
257 |
|
258 |
/**
|
259 |
* A?ade el vector geoTransform a la im?gen que contiene los valores Origen y pixelSize
|
260 |
*
|
261 |
* @return Devuelve un vector de doubles que contiene los valores de coordenadas de origen y pixelSize.
|
262 |
* @throws GdalException
|
263 |
*/
|
264 |
public void setGeoTransform(GeoTransform gt)throws GdalException { |
265 |
int res = setGeoTransformNat(cPtr,gt);
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* Obtiene el sistema de coordenadas de referencia de la im?gen.
|
270 |
*
|
271 |
* @return Devuelve un String con los datos del sistema de coordenadas de referencia.
|
272 |
* @throws GdalException
|
273 |
*/
|
274 |
public String getProjectionRef()throws GdalException { |
275 |
String res = getProjectionRefNat(cPtr);
|
276 |
|
277 |
if(res == null)return new String(""); |
278 |
else return res; |
279 |
} |
280 |
|
281 |
/**
|
282 |
* Cierra el fichero de im?gen.
|
283 |
*
|
284 |
* @throws GdalException
|
285 |
*/
|
286 |
public void close()throws GdalException { |
287 |
closeNat(cPtr); |
288 |
} |
289 |
|
290 |
/**
|
291 |
* Obtiene un driver a trav?s de su nombre
|
292 |
*
|
293 |
* @param name Nombre del driver
|
294 |
*/
|
295 |
public static GdalDriver getDriverByName(String name)throws GdalException { |
296 |
long ptrdrv = -1; |
297 |
|
298 |
ptrdrv = getDriverByNameNat(name); |
299 |
//System.out.println("ptrdrv="+ptrdrv);
|
300 |
|
301 |
return (new GdalDriver(ptrdrv)); |
302 |
} |
303 |
|
304 |
|
305 |
/**
|
306 |
* Obtiene el n?mero de bandas de la im?gen
|
307 |
*
|
308 |
* @return Devuelve un entero con el n?mero de bandas que contiene la im?gen.
|
309 |
* @throws GdalException
|
310 |
*/
|
311 |
public int getGCPCount()throws GdalException { |
312 |
String msg1="Error en GDALGetRasterCount. . La llamada GDALOpen no tuvo ?xito"; |
313 |
String msg2="Error en el conteo de n?mero de bandas"; |
314 |
return baseSimpleFunctions(8,msg1,msg2); |
315 |
} |
316 |
|
317 |
/**
|
318 |
*Asigna la proyecci?n especificada en la cadena que se le pasa por par?metro.
|
319 |
*@param proj proyecci?n
|
320 |
*@throws GdalException
|
321 |
*/
|
322 |
public void setProjection(String proj)throws GdalException { |
323 |
int res = setProjectionNat(cPtr, proj);
|
324 |
|
325 |
if(res < 0) |
326 |
throw new GdalException("Error en setProjection(). No se ha podido asignar la proyecci?n."); |
327 |
} |
328 |
|
329 |
/**
|
330 |
* Obtiene la cadena que representa el tipo de banda de color. Los tipos posibles son:
|
331 |
* <UL>
|
332 |
* <LI>0 = "Undefined" </LI>
|
333 |
* <LI>1 = "Gray";</LI>
|
334 |
* <LI>2 = "Palette";</LI>
|
335 |
* <LI>3 = "Red";</LI>
|
336 |
* <LI>4 = "Green";</LI>
|
337 |
* <LI>5 = "Blue";</LI>
|
338 |
* <LI>6 = "Alpha";</LI>
|
339 |
* <LI>7 = "Hue";</LI>
|
340 |
* <LI>8 = "Saturation";</LI>
|
341 |
* <LI>9 = "Lightness";</LI>
|
342 |
* <LI>10 = "Cyan";</LI>
|
343 |
* <LI>11 = "Magenta";</LI>
|
344 |
* <LI>12 = "Yellow";</LI>
|
345 |
* <LI>13 = "Black";</LI>
|
346 |
* <LI>14 = "YCbCr_Y";</LI>
|
347 |
* <LI>15 = "YCbCr_Cb";</LI>
|
348 |
* <LI>16 = "YCbCr_Cr";</LI>
|
349 |
* </UL>
|
350 |
* @return Cadena con el nombre del tipo de banda de color
|
351 |
* @throws GdalException
|
352 |
*/
|
353 |
public String getColorInterpretationName(int colorInterp)throws GdalException { |
354 |
String bandTypeName = getColorInterpretationNameNat(cPtr, colorInterp);
|
355 |
|
356 |
if(bandTypeName == null) |
357 |
throw new GdalException("Error en getColorInterpretationName(). No ha podido obtenerse el tipo de banda de color"); |
358 |
else
|
359 |
return bandTypeName;
|
360 |
} |
361 |
|
362 |
} |