gvsig-raster / org.gvsig.jgdal / trunk / org.gvsig.jgdal / src / main / java / org / gvsig / jgdal / GdalRasterBand.java @ 839
History | View | Annotate | Download (13.9 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 |
|
23 |
package org.gvsig.jgdal; |
24 |
|
25 |
/**
|
26 |
* Representa a una banda simple de la im?gen o canal.
|
27 |
*
|
28 |
* @author Nacho Brodin (nachobrodin@gmail.com).<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
|
29 |
* @version 0.0
|
30 |
* @link http://www.gvsig.gva.es
|
31 |
*/
|
32 |
|
33 |
public class GdalRasterBand extends JNIBase{ |
34 |
|
35 |
|
36 |
private native long getOverviewNat(long cPtr,int i); |
37 |
private native long getRasterColorTableNat(long cPtr); |
38 |
private native GdalBuffer readRasterNat(long cPtr, |
39 |
int nXOff, int nYOff, int nXSize, int nYSize, |
40 |
int BufXSize, int BufYSize, |
41 |
int eBufType);
|
42 |
private native GdalBuffer readRasterWithPaletteNat(long cPtr, |
43 |
int nXOff, int nYOff, int nXSize, int nYSize, |
44 |
int BufXSize, int BufYSize, |
45 |
int eBufType);
|
46 |
private native void writeRasterNat( long cPtr, |
47 |
int nXOff, int nYOff, int nXSize, int nYSize, |
48 |
GdalBuffer buffer, |
49 |
int eBufType);
|
50 |
private native double getRasterNoDataValueNat(long cPtr); |
51 |
private native int existsNoDataValueNat(long cPtr); |
52 |
private native String[] getMetadataNat(long cPtr,String pszDomain); |
53 |
private native int getRasterColorInterpretationNat(long cPtr); |
54 |
private native int setRasterColorInterpretationNat(long cPtr, int bandType); |
55 |
|
56 |
|
57 |
/**
|
58 |
* Asigna el identificador de la banda
|
59 |
*/
|
60 |
|
61 |
public GdalRasterBand(long cPtr) { |
62 |
this.cPtr=cPtr;
|
63 |
} |
64 |
|
65 |
|
66 |
/**
|
67 |
* Lee datos de la banda de la imagen
|
68 |
*
|
69 |
* @return Devuelve un vector de bytes con el trozo de raster le?do.
|
70 |
* @param nXOff El desplazamiento del pixel desde la esquina superior derecha
|
71 |
* de la banda accedida.
|
72 |
* @param nYOff El desplazamiento de l?nea desde la esquina superior derecha
|
73 |
* de la banda accedida.
|
74 |
* @param nXSize Ancho de la regi?n en pixels de la banda que ser? accedida
|
75 |
* @param nYSize Altura de la regi?n en l?neas de la banda que ser? accedida
|
76 |
* @param bufXSize Ancho del buffer donde la regi?n de la im?gen ser? guardada
|
77 |
* @param bufYSize Altura del buffer donde la regi?n de la im?gen ser? guardada
|
78 |
* En caso de que bufXSize o bufYSize sean menores que 1, pasan a tener el mismo valor que
|
79 |
* nXSize y nYSize respectivamente para evitar buffers con tama?o 0.
|
80 |
* @param eBufType
|
81 |
*/
|
82 |
|
83 |
public GdalBuffer readRaster(int nXOff, int nYOff, int nXSize, int nYSize, |
84 |
int bufXSize, int bufYSize, |
85 |
int eBufType)throws GdalException { |
86 |
|
87 |
if (cPtr == 0) |
88 |
throw new GdalException("No se ha podido acceder al archivo."); |
89 |
|
90 |
if ((nXOff<0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize())) |
91 |
throw new GdalException("Desplazamiento de la ventana fuera de rango."); |
92 |
|
93 |
if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize<1) || (nYSize > getRasterBandYSize())) |
94 |
throw new GdalException("Tama?o de ventana incorrecto."); |
95 |
|
96 |
if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
|
97 |
throw new GdalException("Posicion de la ventana incorrecta."); |
98 |
|
99 |
if ((eBufType < 1) || (eBufType > 11)) |
100 |
throw new GdalException("Tipo de datos incorrecto."); |
101 |
|
102 |
if (bufXSize < 1) |
103 |
bufXSize = nXSize; |
104 |
|
105 |
if (bufYSize < 1) |
106 |
bufYSize = nYSize; |
107 |
|
108 |
GdalBuffer buffer = readRasterNat(cPtr, nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType); |
109 |
if(buffer!=null) |
110 |
return buffer;
|
111 |
else
|
112 |
return null; |
113 |
} |
114 |
|
115 |
/**
|
116 |
* Escribe datos en la banda de la imagen
|
117 |
*
|
118 |
* @param nXOff El desplazamiento del pixel desde la esquina superior derecha
|
119 |
* de la banda accedida.
|
120 |
* @param nYOff El desplazamiento de l?nea desde la esquina superior derecha
|
121 |
* de la banda accedida.
|
122 |
* @param nXSize Ancho de la regi?n en pixels de la banda que ser? accedida
|
123 |
* @param nYSize Altura de la regi?n en l?neas de la banda que ser? accedida
|
124 |
* @param BufXSize Ancho del buffer donde la regi?n de la im?gen ser? guardada
|
125 |
* @param BufYSize Altura del buffer donde la regi?n de la im?gen ser? guardada
|
126 |
* @param eBufType
|
127 |
*/
|
128 |
|
129 |
public void writeRaster(int nXOff, int nYOff, int nXSize, int nYSize, GdalBuffer buf, int eBufType)throws GdalException { |
130 |
GdalBuffer buffer = new GdalBuffer();
|
131 |
|
132 |
if ((nXOff < 0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize())) |
133 |
throw new GdalException("Desplazamiento de la ventana fuera de rango."); |
134 |
|
135 |
if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize < 1) || (nYSize > getRasterBandYSize())) |
136 |
throw new GdalException("Tama?o de ventana incorrecto."); |
137 |
|
138 |
if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
|
139 |
throw new GdalException("Posicion de la ventana incorrecta."); |
140 |
|
141 |
if ((eBufType < 1) || (eBufType > 11)) |
142 |
throw new GdalException("Tipo de datos incorrecto."); |
143 |
|
144 |
if (buf == null) |
145 |
throw new GdalException("Buffer incorrecto"); |
146 |
|
147 |
switch(eBufType) {
|
148 |
case 0: |
149 |
return;
|
150 |
case 1: |
151 |
buffer.buffByte = buf.buffByte; |
152 |
break;
|
153 |
case 2: |
154 |
case 3: |
155 |
case 8: |
156 |
buffer.buffShort = buf.buffShort; |
157 |
break;
|
158 |
case 4: |
159 |
case 5: |
160 |
case 9: |
161 |
buffer.buffInt = buf.buffInt; |
162 |
break;
|
163 |
case 6: |
164 |
case 10: |
165 |
buffer.buffFloat = buf.buffFloat; |
166 |
break;
|
167 |
case 7: |
168 |
case 11: |
169 |
buffer.buffDouble = buf.buffDouble; |
170 |
break;
|
171 |
case 12: |
172 |
return;
|
173 |
} |
174 |
|
175 |
writeRasterNat(cPtr, nXOff, nYOff, nXSize, nYSize, buffer, eBufType); |
176 |
} |
177 |
|
178 |
/**
|
179 |
*Obtiene el tama?o en pixeles de la im?gen en el eje de las X
|
180 |
*@return Tama?o en pixeles del eje X
|
181 |
*@throws GdalException
|
182 |
*/
|
183 |
|
184 |
public int getRasterBandXSize()throws GdalException { |
185 |
String msg1="Error en getRasterBandXSize(). La llamada getRasterBand no tuvo exito"; |
186 |
String msg2="Tama?o de banda erroneo devuelto por GetRasterBandXSize"; |
187 |
|
188 |
return baseSimpleFunctions(0,msg1,msg2); |
189 |
} |
190 |
|
191 |
/**
|
192 |
*Obtiene el tama?o en pixeles de la im?gen en el eje de las Y
|
193 |
*@return Tama?o en pixeles del eje Y
|
194 |
*@throws GdalException
|
195 |
*/
|
196 |
|
197 |
public int getRasterBandYSize()throws GdalException { |
198 |
String msg1="Error en getRasterBandYSize(). La llamada getRasterBand no tuvo exito"; |
199 |
String msg2="Tama?o de banda erroneo devuelto por GetRasterBandYSize"; |
200 |
|
201 |
return baseSimpleFunctions(1,msg1,msg2); |
202 |
} |
203 |
|
204 |
|
205 |
/**
|
206 |
* Devuelve el n?mero de overviews que contiene la banda.
|
207 |
* @return N?mero de overviews
|
208 |
* @throws GdalException
|
209 |
*/
|
210 |
|
211 |
public int getOverviewCount()throws GdalException { |
212 |
String msg1="Error en getOverviewCount(). La llamada getRasterBand no tuvo exito"; |
213 |
String msg2="Error al obtener el n?mero de overviews"; |
214 |
|
215 |
return baseSimpleFunctions(2,msg1,msg2); |
216 |
} |
217 |
|
218 |
|
219 |
/**
|
220 |
* Obtiene el overview indicado por el ?ndice "i".
|
221 |
*
|
222 |
* @param i indice del overview que se quiere recuperar.
|
223 |
* @return GdalRasterBand Banda correspondiente al overview selecccionado
|
224 |
* @throws GdalException
|
225 |
*/
|
226 |
|
227 |
public GdalRasterBand getOverview(int i)throws GdalException { |
228 |
long cPtr_ov;
|
229 |
|
230 |
if((i < 0) || (i >= getOverviewCount())) |
231 |
throw new GdalException("El overview seleccionado no existe"); |
232 |
|
233 |
cPtr_ov = getOverviewNat(cPtr,i); |
234 |
|
235 |
if (cPtr_ov == 0) |
236 |
throw new GdalException("No se ha podido obtener el overview"); |
237 |
|
238 |
return new GdalRasterBand(cPtr_ov); |
239 |
} |
240 |
|
241 |
|
242 |
/**
|
243 |
* Devuelve el tama?o en X del bloque para esa banda
|
244 |
* @return Tama?o en pixeles del bloque en el eje X
|
245 |
* @throws GdalException
|
246 |
*/
|
247 |
|
248 |
public int getBlockXSize()throws GdalException { |
249 |
String msg1="Error en getBlockXSize(). La llamada getRasterBand no tuvo exito"; |
250 |
String msg2="Tama?o de bloque erroneo devuelto por GetBlockXSize"; |
251 |
|
252 |
return baseSimpleFunctions(3,msg1,msg2); |
253 |
} |
254 |
|
255 |
|
256 |
/**
|
257 |
* Devuelve el tama?o en Y del bloque para esa banda
|
258 |
* @return Tama?o en pixeles del bloque en el eje Y
|
259 |
* @throws GdalException
|
260 |
*/
|
261 |
|
262 |
public int getBlockYSize()throws GdalException { |
263 |
String msg1="Error en getBlockXSize(). La llamada getRasterBand no tuvo exito"; |
264 |
String msg2="Tama?o de bloque erroneo devuelto por GetBlockYSize"; |
265 |
|
266 |
return baseSimpleFunctions(4,msg1,msg2); |
267 |
} |
268 |
|
269 |
/**
|
270 |
* Devuelve el tipo de datos de la banda
|
271 |
* @return Tama?o en pixeles del bloque en el eje Y
|
272 |
* @throws GdalException
|
273 |
*/
|
274 |
|
275 |
public int getRasterDataType()throws GdalException { |
276 |
String msg1="Error en getRasterDataType(). La llamada getRasterBand no tuvo exito"; |
277 |
String msg2="Tipo de dato devuelto por GetRasterDataType erroneo"; |
278 |
|
279 |
return baseSimpleFunctions(9,msg1,msg2); |
280 |
} |
281 |
|
282 |
/**
|
283 |
* Obtiene la tabla de color asociada a la imagen
|
284 |
*/
|
285 |
public GdalColorTable getRasterColorTable()throws GdalException { |
286 |
GdalColorTable gct = null;
|
287 |
|
288 |
if (cPtr == 0) |
289 |
throw new GdalException("No se ha podido acceder al archivo."); |
290 |
|
291 |
long cPtr_ct = getRasterColorTableNat(cPtr);
|
292 |
|
293 |
if ((cPtr_ct == 0) || (cPtr_ct == -1)) |
294 |
return null; |
295 |
|
296 |
gct = new GdalColorTable(cPtr_ct);
|
297 |
|
298 |
return gct;
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* Lee datos de la banda de la im?gen con una paleta asociada
|
303 |
*
|
304 |
* @return Devuelve un vector de bytes con el trozo de raster le?do.
|
305 |
* @param nXOff El desplazamiento del pixel desde la esquina superior derecha
|
306 |
* de la banda accedida.
|
307 |
* @param nYOff El desplazamiento de l?nea desde la esquina superior derecha
|
308 |
* de la banda accedida.
|
309 |
* @param nXSize Ancho de la regi?n en pixels de la banda que ser? accedida
|
310 |
* @param nYSize Altura de la regi?n en l?neas de la banda que ser? accedida
|
311 |
* @param BufXSize Ancho del buffer donde la regi?n de la im?gen ser? guardada
|
312 |
* @param BufYSize Altura del buffer donde la regi?n de la im?gen ser? guardada
|
313 |
* En caso de que bufXSize o bufYSize sean menores que 1, pasan a tener el mismo valor que
|
314 |
* nXSize y nYSize respectivamente para evitar buffers con tama?o 0.
|
315 |
* @param eBufType
|
316 |
*/
|
317 |
|
318 |
public GdalBuffer readRasterWithPalette(int nXOff, int nYOff, int nXSize, int nYSize, |
319 |
int bufXSize, int bufYSize, |
320 |
int eBufType)throws GdalException { |
321 |
|
322 |
if (cPtr == 0) |
323 |
throw new GdalException("No se ha podido acceder al archivo."); |
324 |
|
325 |
if ((nXOff<0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize())) |
326 |
throw new GdalException("Desplazamiento de la ventana fuera de rango."); |
327 |
|
328 |
if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize < 1) || (nYSize > getRasterBandYSize())) |
329 |
throw new GdalException("Tama?o de ventana incorrecto."); |
330 |
|
331 |
if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
|
332 |
throw new GdalException("Posicion de la ventana incorrecta."); |
333 |
|
334 |
if ((eBufType < 1) || (eBufType > 11)) |
335 |
throw new GdalException("Tipo de datos incorrecto."); |
336 |
|
337 |
if (bufXSize < 1) |
338 |
bufXSize = nXSize; |
339 |
|
340 |
if (bufYSize < 1) |
341 |
bufYSize = nYSize; |
342 |
|
343 |
|
344 |
GdalBuffer buffer = readRasterWithPaletteNat(cPtr, nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType); |
345 |
|
346 |
|
347 |
if(buffer!=null) |
348 |
return buffer;
|
349 |
else
|
350 |
return null; |
351 |
} |
352 |
|
353 |
/**
|
354 |
*Devuelve el valor de NoData
|
355 |
*/
|
356 |
public double getRasterNoDataValue()throws GdalException { |
357 |
if (cPtr == 0) |
358 |
throw new GdalException("No se ha podido acceder al archivo."); |
359 |
|
360 |
return getRasterNoDataValueNat(cPtr);
|
361 |
} |
362 |
|
363 |
/**
|
364 |
* Obtiene el valorDevuelve el valor de NoData
|
365 |
*/
|
366 |
public boolean existsNoDataValue()throws GdalException { |
367 |
if (cPtr == 0) |
368 |
throw new GdalException("No se ha podido acceder al archivo."); |
369 |
|
370 |
int result = existsNoDataValueNat(cPtr);
|
371 |
|
372 |
if(result == 1) |
373 |
return true; |
374 |
else
|
375 |
return false; |
376 |
} |
377 |
|
378 |
/**
|
379 |
* Obtiene un array de Strings con los metadatos
|
380 |
*
|
381 |
* @throws GdalException
|
382 |
* @return Array de Strings que corresponden a los metadatos que ofrece la im?gen
|
383 |
*/
|
384 |
|
385 |
public String[] getMetadata()throws GdalException { |
386 |
if (cPtr == 0) |
387 |
throw new GdalException("No se ha podido acceder al archivo."); |
388 |
|
389 |
String[] res = getMetadataNat(cPtr,null); |
390 |
if(res == null) |
391 |
return new String[0]; |
392 |
else return res; |
393 |
} |
394 |
|
395 |
/**
|
396 |
* Obtiene identificador que representa el tipo de banda de color.
|
397 |
* @return identificador del tipo de banda de color
|
398 |
* @throws GdalException
|
399 |
*/
|
400 |
|
401 |
public int getRasterColorInterpretation()throws GdalException { |
402 |
if (cPtr == 0) |
403 |
throw new GdalException("No se ha podido acceder al archivo."); |
404 |
|
405 |
int bandType = getRasterColorInterpretationNat(cPtr);
|
406 |
return bandType;
|
407 |
} |
408 |
|
409 |
|
410 |
/**
|
411 |
* Asigna la interpretaci?n de color de la banda.
|
412 |
* Con algunos formatos no es posible modificar la interpretaci?n de color,
|
413 |
* tales como tiff y jpg. En el caso de tif, no hay error pero tampoco se
|
414 |
* produce el cambio en la interpretaci?n. En el caso de jpg, gdal lanza un error.
|
415 |
* 0 = "Undefined"
|
416 |
* 1 = "Gray";
|
417 |
* 2 = "Palette";
|
418 |
* 3 = "Red";
|
419 |
* 4 = "Green";
|
420 |
* 5 = "Blue";
|
421 |
* 6 = "Alpha";
|
422 |
* 7 = "Hue";
|
423 |
* 8 = "Saturation";
|
424 |
* 9 = "Lightness";
|
425 |
* 10 = "Cyan";
|
426 |
* 11 = "Magenta";
|
427 |
* 12 = "Yellow";
|
428 |
* 13 = "Black";
|
429 |
* 14 = "YCbCr_Y";
|
430 |
* 15 = "YCbCr_Cb";
|
431 |
* 16 = "YCbCr_Cr";
|
432 |
* @param bandType
|
433 |
* @throws GdalException
|
434 |
*/
|
435 |
public void setRasterColorInterpretation(int bandType) throws GdalException{ |
436 |
if (cPtr == 0) |
437 |
throw new GdalException("No se ha podido acceder al archivo."); |
438 |
|
439 |
if ((bandType < 0) || (bandType > 16)){ |
440 |
throw new GdalException("Tipo de banda incorrecto"); |
441 |
} |
442 |
|
443 |
int err = setRasterColorInterpretationNat(cPtr, bandType);
|
444 |
|
445 |
} |
446 |
|
447 |
} |