root / trunk / libraries / libCq_CMS_praster / src / org / cresques / io / GeoRasterFile.java @ 8026
History | View | Annotate | Download (25.1 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.awt.Component; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.Image; |
29 |
import java.awt.geom.Point2D; |
30 |
import java.awt.image.DataBuffer; |
31 |
import java.io.BufferedReader; |
32 |
import java.io.File; |
33 |
import java.io.FileInputStream; |
34 |
import java.io.FileNotFoundException; |
35 |
import java.io.FileReader; |
36 |
import java.io.FileWriter; |
37 |
import java.io.IOException; |
38 |
import java.lang.reflect.Constructor; |
39 |
import java.lang.reflect.InvocationTargetException; |
40 |
import java.util.TreeMap; |
41 |
|
42 |
import org.cresques.cts.ICoordTrans; |
43 |
import org.cresques.cts.IProjection; |
44 |
import org.cresques.filter.PixelFilter; |
45 |
import org.cresques.filter.SimplePixelFilter; |
46 |
import org.cresques.io.data.BandList; |
47 |
import org.cresques.io.data.RasterBuf; |
48 |
import org.cresques.io.data.RasterMetaFileTags; |
49 |
import org.cresques.io.datastruct.Metadata; |
50 |
import org.cresques.io.datastruct.Palette; |
51 |
import org.cresques.io.exceptions.SupersamplingNotSupportedException; |
52 |
import org.cresques.px.Extent; |
53 |
import org.cresques.px.IObjList; |
54 |
import org.cresques.px.PxContour; |
55 |
import org.cresques.px.PxObjList; |
56 |
import org.gvsig.i18n.Messages; |
57 |
import org.kxml2.io.KXmlParser; |
58 |
import org.xmlpull.v1.XmlPullParserException; |
59 |
|
60 |
/**
|
61 |
* Manejador de ficheros raster georeferenciados.
|
62 |
*
|
63 |
* Esta clase abstracta es el ancestro de todas las clases que proporcionan
|
64 |
* soporte para ficheros raster georeferenciados.<br>
|
65 |
* Actua tambien como una 'Fabrica', ocultando al cliente la manera en que
|
66 |
* se ha implementado ese manejo. Una clase nueva que soportara un nuevo
|
67 |
* tipo de raster tendr?a que registrar su extensi?n o extensiones usando
|
68 |
* el m?todo @see registerExtension.<br>
|
69 |
* @author "Luis W. Sevilla" <sevilla_lui@gva.es>*
|
70 |
*/
|
71 |
|
72 |
public abstract class GeoRasterFile extends GeoFile { |
73 |
|
74 |
/**
|
75 |
* Flag que representa a la banda del Rojo
|
76 |
*/
|
77 |
public static final int RED_BAND = 0x01; |
78 |
|
79 |
/**
|
80 |
* Flag que representa a la banda del Verde
|
81 |
*/
|
82 |
public static final int GREEN_BAND = 0x02; |
83 |
|
84 |
/**
|
85 |
* Flag que representa a la banda del Azul
|
86 |
*/
|
87 |
public static final int BLUE_BAND = 0x04; |
88 |
private static TreeMap supportedExtensions = null; |
89 |
protected Component updatable = null; |
90 |
protected boolean doTransparency = false; |
91 |
private boolean verifySize = false; |
92 |
|
93 |
/**
|
94 |
* Esta variable estar? a true si el driver est? supersampleando en el ?ltimo dibujado.
|
95 |
* Debe tenerse especial cuidado ya que est? consulta no es adecuada desde gvSIG ya que el
|
96 |
* dibujado se realiza asincronamente por lo que el valor puede no coincidir con el ?ltimo
|
97 |
* dibujado.
|
98 |
*/
|
99 |
protected boolean isSupersampling = false; |
100 |
/**
|
101 |
* Paso correspondiente al supersampling o subsampling que se est? aplicando en el ?ltimo dibujado.
|
102 |
*/
|
103 |
protected int[] stepArrayX = null, stepArrayY = null; |
104 |
|
105 |
/**
|
106 |
* Filtro para raster.
|
107 |
* Permite eliminar la franja inutil alrededor de un raster girado o de
|
108 |
* un mosaico de borde irregular.
|
109 |
*
|
110 |
* Funciona bien solo con raster en tonos de gris, porque se basa que
|
111 |
* el valor del pixel no supere un determinado valor 'umbral' que se
|
112 |
* le pasa al constructor.
|
113 |
*
|
114 |
* Desarrollado para 'limpiar' los bordes de los mosaicos del SIG
|
115 |
* Oleicola. Para ese caso los par?metros del constructo son:
|
116 |
* PixelFilter(0x10ffff00, 0xff000000, 0xf0f0f0);
|
117 |
*/
|
118 |
protected PixelFilter tFilter = null; |
119 |
|
120 |
/**
|
121 |
* Asignaci?n de banda del Rojo a una banda de la imagen
|
122 |
*/
|
123 |
protected int rBandNr = 1; |
124 |
|
125 |
/**
|
126 |
* Asignaci?n de banda del Verde a una banda de la imagen
|
127 |
*/
|
128 |
protected int gBandNr = 1; |
129 |
|
130 |
/**
|
131 |
* Asignaci?n de banda del Azul a una banda de la imagen
|
132 |
*/
|
133 |
protected int bBandNr = 1; |
134 |
|
135 |
/**
|
136 |
* N?mero de bandas de la imagen
|
137 |
*/
|
138 |
protected int bandCount = 1; |
139 |
private int dataType = DataBuffer.TYPE_BYTE; |
140 |
protected Palette palette = null; |
141 |
|
142 |
static {
|
143 |
Messages.addResourceFamily("org.cresques.translations.text", "org.cresques.ui"); |
144 |
supportedExtensions = new TreeMap(); |
145 |
supportedExtensions.put("ecw", EcwFile.class);
|
146 |
supportedExtensions.put("jp2", EcwFile.class);
|
147 |
|
148 |
supportedExtensions.put("sid", MrSidFile.class);
|
149 |
|
150 |
supportedExtensions.put("bmp", GdalFile.class);
|
151 |
supportedExtensions.put("gif", GdalFile.class);
|
152 |
supportedExtensions.put("img", GdalFile.class);
|
153 |
supportedExtensions.put("tif", GdalFile.class);
|
154 |
supportedExtensions.put("tiff", GdalFile.class);
|
155 |
supportedExtensions.put("jpg", GdalFile.class);
|
156 |
supportedExtensions.put("png", GdalFile.class);
|
157 |
supportedExtensions.put("vrt", GdalFile.class);
|
158 |
|
159 |
supportedExtensions.put("dat", GdalFile.class); // Envi |
160 |
supportedExtensions.put("lan", GdalFile.class); // Erdas |
161 |
supportedExtensions.put("gis", GdalFile.class); // Erdas |
162 |
supportedExtensions.put("pix", GdalFile.class); // PCI Geomatics |
163 |
supportedExtensions.put("aux", GdalFile.class); // PCI Geomatics |
164 |
supportedExtensions.put("adf", GdalFile.class); // ESRI Grids |
165 |
supportedExtensions.put("mpr", GdalFile.class); // Ilwis |
166 |
supportedExtensions.put("mpl", GdalFile.class); // Ilwis |
167 |
supportedExtensions.put("map", GdalFile.class); // PC Raster |
168 |
} |
169 |
|
170 |
/**
|
171 |
* Factoria para abrir distintos tipos de raster.
|
172 |
*
|
173 |
* @param proj Proyecci?n en la que est? el raster.
|
174 |
* @param fName Nombre del fichero.
|
175 |
* @return GeoRasterFile, o null si hay problemas.
|
176 |
*/
|
177 |
public static GeoRasterFile openFile(IProjection proj, String fName) { |
178 |
String ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1); |
179 |
GeoRasterFile grf = null;
|
180 |
|
181 |
//if (!supportedExtensions.containsKey(ext))
|
182 |
//return grf;
|
183 |
Class clase = null; |
184 |
if (supportedExtensions.containsKey(ext))
|
185 |
clase = (Class) supportedExtensions.get(ext);
|
186 |
else
|
187 |
clase = GdalFile.class; |
188 |
|
189 |
Class [] args = {IProjection.class, String.class}; |
190 |
try {
|
191 |
Constructor hazNuevo = clase.getConstructor(args);
|
192 |
Object [] args2 = {proj, fName}; |
193 |
grf = (GeoRasterFile) hazNuevo.newInstance(args2); |
194 |
grf.setFileSize(new File(fName).length()); |
195 |
} catch (SecurityException e) { |
196 |
e.printStackTrace(); |
197 |
} catch (NoSuchMethodException e) { |
198 |
e.printStackTrace(); |
199 |
} catch (IllegalArgumentException e) { |
200 |
e.printStackTrace(); |
201 |
} catch (InstantiationException e) { |
202 |
e.printStackTrace(); |
203 |
} catch (IllegalAccessException e) { |
204 |
e.printStackTrace(); |
205 |
} catch (InvocationTargetException e) { |
206 |
System.err.println("Extension not supported!!!"); |
207 |
return null; |
208 |
} |
209 |
return grf;
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* Registra una clase que soporta una extensi?n raster.
|
214 |
* @param ext extensi?n soportada.
|
215 |
* @param clase clase que la soporta.
|
216 |
*/
|
217 |
public static void registerExtension(String ext, Class clase) { |
218 |
ext = ext.toLowerCase(); |
219 |
System.out.println("RASTER: extension '"+ext+"' supported."); |
220 |
supportedExtensions.put(ext, clase); |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Tipo de fichero soportado.
|
225 |
* Devuelve true si el tipo de fichero (extension) est? soportado, si no
|
226 |
* devuelve false.
|
227 |
*
|
228 |
* @param fName Fichero raster
|
229 |
* @return true si est? soportado, si no false.
|
230 |
*/
|
231 |
public static boolean fileIsSupported(String fName) { |
232 |
return true; |
233 |
//String ext = fName.toLowerCase().substring(fName.lastIndexOf('.')+1);
|
234 |
//return supportedExtensions.containsKey(ext);
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* Constructor
|
239 |
* @param proj Proyecci?n
|
240 |
* @param name Nombre del fichero de imagen.
|
241 |
*/
|
242 |
public GeoRasterFile(IProjection proj, String name) { |
243 |
super(proj, name);
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* Carga un fichero raster. Puede usarse para calcular el extent e instanciar
|
248 |
* un objeto de este tipo.
|
249 |
*/
|
250 |
abstract public GeoFile load(); |
251 |
|
252 |
/**
|
253 |
* Cierra el fichero y libera los recursos.
|
254 |
*/
|
255 |
abstract public void close(); |
256 |
|
257 |
/**
|
258 |
* Obtiene la codificaci?n del fichero XML
|
259 |
* @param file Nombre del fichero XML
|
260 |
* @return Codificaci?n
|
261 |
*/
|
262 |
private String readFileEncoding(String file){ |
263 |
FileReader fr;
|
264 |
String encoding = null; |
265 |
try
|
266 |
{ |
267 |
fr = new FileReader(file); |
268 |
BufferedReader br = new BufferedReader(fr); |
269 |
char[] buffer = new char[100]; |
270 |
br.read(buffer); |
271 |
StringBuffer st = new StringBuffer(new String(buffer)); |
272 |
String searchText = "encoding=\""; |
273 |
int index = st.indexOf(searchText);
|
274 |
if (index>-1) { |
275 |
st.delete(0, index+searchText.length());
|
276 |
encoding = st.substring(0, st.indexOf("\"")); |
277 |
} |
278 |
fr.close(); |
279 |
} catch(FileNotFoundException ex) { |
280 |
ex.printStackTrace(); |
281 |
} catch (IOException e) { |
282 |
e.printStackTrace(); |
283 |
} |
284 |
return encoding;
|
285 |
} |
286 |
|
287 |
private double[] parserExtent(KXmlParser parser) throws XmlPullParserException, IOException { |
288 |
double originX = 0D, originY = 0D, w = 0D, h = 0D; |
289 |
double pixelSizeX = 0D, pixelSizeY = 0D; |
290 |
|
291 |
boolean end = false; |
292 |
int tag = parser.next();
|
293 |
while (!end) {
|
294 |
switch(tag) {
|
295 |
case KXmlParser.START_TAG:
|
296 |
if(parser.getName() != null){ |
297 |
if (parser.getName().equals(RasterMetaFileTags.POSX)){
|
298 |
originX = Double.parseDouble(parser.nextText());
|
299 |
}else if (parser.getName().equals(RasterMetaFileTags.POSY)){ |
300 |
originY = Double.parseDouble(parser.nextText());
|
301 |
}else if (parser.getName().equals(RasterMetaFileTags.PX_SIZE_X)){ |
302 |
pixelSizeX = Double.parseDouble(parser.nextText());
|
303 |
}else if (parser.getName().equals(RasterMetaFileTags.PX_SIZE_Y)){ |
304 |
pixelSizeY = Double.parseDouble(parser.nextText());
|
305 |
}else if (parser.getName().equals(RasterMetaFileTags.WIDTH)){ |
306 |
w = Double.parseDouble(parser.nextText());
|
307 |
}else if (parser.getName().equals(RasterMetaFileTags.HEIGHT)){ |
308 |
h = Double.parseDouble(parser.nextText());
|
309 |
} |
310 |
} |
311 |
break;
|
312 |
case KXmlParser.END_TAG:
|
313 |
if (parser.getName().equals(RasterMetaFileTags.BBOX))
|
314 |
end = true;
|
315 |
break;
|
316 |
case KXmlParser.TEXT:
|
317 |
break;
|
318 |
} |
319 |
tag = parser.next(); |
320 |
} |
321 |
|
322 |
double[] values = {originX, originY, w, h, pixelSizeX, pixelSizeY}; |
323 |
return values;
|
324 |
} |
325 |
|
326 |
/**
|
327 |
* Obtiene la informaci?n de georreferenciaci?n asociada a la imagen en un fichero .rmf. Esta
|
328 |
* georreferenciaci?n tiene la caracteristica de que tiene prioridad sobre la de la imagen.
|
329 |
* Es almacenada en la clase GeoFile en la variable virtualExtent.
|
330 |
* @param file Fichero de metadatos .rmf
|
331 |
*/
|
332 |
protected void readGeoInfo(String file){ |
333 |
String rmf = file.substring(0, file.lastIndexOf(".") + 1) + "rmf"; |
334 |
File rmfFile = new File(rmf); |
335 |
if(!rmfFile.exists())
|
336 |
return;
|
337 |
|
338 |
boolean georefOk = false; |
339 |
double originX = 0D, originY = 0D, w = 0D, h = 0D; |
340 |
double pixelSizeX = 0D, pixelSizeY = 0D; |
341 |
double imageWidth = 0D, imageHeight = 0D; |
342 |
|
343 |
FileReader fr = null; |
344 |
String v = null; |
345 |
try {
|
346 |
fr = new FileReader(rmf); |
347 |
KXmlParser parser = new KXmlParser();
|
348 |
parser.setInput(new FileInputStream(rmf), readFileEncoding(rmf)); |
349 |
int tag = parser.nextTag();
|
350 |
if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
|
351 |
parser.require(KXmlParser.START_TAG, null, RasterMetaFileTags.MAIN_TAG);
|
352 |
while(tag != KXmlParser.END_DOCUMENT) {
|
353 |
switch(tag) {
|
354 |
case KXmlParser.START_TAG:
|
355 |
if (parser.getName().equals(RasterMetaFileTags.LAYER)) {
|
356 |
int layerListTag = parser.next();
|
357 |
boolean geoRefEnd = false; |
358 |
while (!geoRefEnd){
|
359 |
if(parser.getName() != null){ |
360 |
if (parser.getName().equals(RasterMetaFileTags.PROJ)){
|
361 |
//System.out.println("PROJ:"+parser.nextText());
|
362 |
} else if (parser.getName().equals(RasterMetaFileTags.BBOX)){ |
363 |
double[] values = parserExtent(parser); |
364 |
originX = values[0];
|
365 |
originY = values[1];
|
366 |
w = values[2];
|
367 |
h = values[3];
|
368 |
pixelSizeX = values[4];
|
369 |
pixelSizeY = values[5];
|
370 |
georefOk = true;
|
371 |
} else if (parser.getName().equals(RasterMetaFileTags.DIM)){ |
372 |
boolean DimEnd = false; |
373 |
while (!DimEnd){
|
374 |
layerListTag = parser.next(); |
375 |
if(parser.getName() != null){ |
376 |
if (parser.getName().equals(RasterMetaFileTags.PX_WIDTH)){
|
377 |
imageWidth = Double.parseDouble(parser.nextText());
|
378 |
}else if (parser.getName().equals(RasterMetaFileTags.PX_HEIGHT)){ |
379 |
imageHeight = Double.parseDouble(parser.nextText());
|
380 |
DimEnd = true;
|
381 |
} |
382 |
} |
383 |
} |
384 |
geoRefEnd = true;
|
385 |
} |
386 |
} |
387 |
layerListTag = parser.next(); |
388 |
} |
389 |
} |
390 |
break;
|
391 |
case KXmlParser.END_TAG:
|
392 |
break;
|
393 |
case KXmlParser.TEXT:
|
394 |
break;
|
395 |
} |
396 |
tag = parser.next(); |
397 |
} |
398 |
parser.require(KXmlParser.END_DOCUMENT, null, null); |
399 |
} |
400 |
|
401 |
if(georefOk){
|
402 |
rmfExists = true;
|
403 |
setExtentTransform(originX, originY, w, h, pixelSizeX, pixelSizeY); |
404 |
extent = new Extent(originX, originY, originX + (pixelSizeX * imageWidth), originY + (pixelSizeY * imageHeight));
|
405 |
} |
406 |
|
407 |
} catch (FileNotFoundException fnfEx) { |
408 |
} catch (XmlPullParserException xmlEx) {
|
409 |
xmlEx.printStackTrace(); |
410 |
} catch (IOException e) { |
411 |
} |
412 |
try{
|
413 |
if(fr != null) |
414 |
fr.close(); |
415 |
}catch(IOException ioEx){ |
416 |
//No est? abierto el fichero por lo que no hacemos nada
|
417 |
} |
418 |
} |
419 |
|
420 |
/**
|
421 |
* Calcula la transformaci?n que se produce sobre la vista cuando la imagen tiene un fichero .rmf
|
422 |
* asociado. Esta transformaci?n tiene diferencias entre los distintos formatos por lo que debe calcularla
|
423 |
* el driver correspondiente.
|
424 |
* @param originX Origen de la imagen en la coordenada X
|
425 |
* @param originY Origen de la imagen en la coordenada Y
|
426 |
*/
|
427 |
abstract public void setExtentTransform(double originX, double originY, double w, double h, double psX, double psY); |
428 |
|
429 |
public static PxContour getContour(String fName, String name, IProjection proj) { |
430 |
PxContour contour = null;
|
431 |
return contour;
|
432 |
} |
433 |
|
434 |
/**
|
435 |
* Obtiene el ancho de la imagen
|
436 |
* @return Ancho de la imagen
|
437 |
*/
|
438 |
abstract public int getWidth(); |
439 |
|
440 |
/**
|
441 |
* Obtiene el ancho de la imagen
|
442 |
* @return Ancho de la imagen
|
443 |
*/
|
444 |
abstract public int getHeight(); |
445 |
|
446 |
/**
|
447 |
* Reproyecci?n.
|
448 |
* @param rp Coordenadas de la transformaci?n
|
449 |
*/
|
450 |
abstract public void reProject(ICoordTrans rp); |
451 |
|
452 |
/**
|
453 |
* Asigna un nuevo Extent
|
454 |
* @param e Extent
|
455 |
*/
|
456 |
abstract public void setView(Extent e); |
457 |
|
458 |
/**
|
459 |
* Obtiene el extent asignado
|
460 |
* @return Extent
|
461 |
*/
|
462 |
abstract public Extent getView(); |
463 |
|
464 |
public void setTransparency(boolean t) { |
465 |
doTransparency = t; |
466 |
tFilter = new PixelFilter(255); |
467 |
} |
468 |
|
469 |
/**
|
470 |
* Asigna un valor de transparencia
|
471 |
* @param t Valor de transparencia
|
472 |
*/
|
473 |
public void setTransparency(int t ) { |
474 |
doTransparency = true;
|
475 |
tFilter = new SimplePixelFilter(255 - t); |
476 |
} |
477 |
|
478 |
public boolean getTransparency() { return doTransparency; } |
479 |
|
480 |
public void setAlpha(int alpha) { |
481 |
if (!doTransparency) setTransparency(255 - alpha); |
482 |
else tFilter.setAlpha(alpha);
|
483 |
} |
484 |
public int getAlpha() { |
485 |
if (tFilter == null) |
486 |
return 255; |
487 |
return tFilter.getAlpha();
|
488 |
} |
489 |
|
490 |
public void setUpdatable(Component c) { updatable = c; } |
491 |
|
492 |
/**
|
493 |
* Actualiza la imagen
|
494 |
* @param width ancho
|
495 |
* @param height alto
|
496 |
* @param rp Reproyecci?n
|
497 |
* @return img
|
498 |
*/
|
499 |
abstract public Image updateImage(int width, int height, ICoordTrans rp); |
500 |
|
501 |
/**
|
502 |
* Obtiene el valor del raster en la coordenada que se le pasa.
|
503 |
* El valor ser? Double, Int, Byte, etc. dependiendo del tipo de
|
504 |
* raster.
|
505 |
* @param x coordenada X
|
506 |
* @param y coordenada Y
|
507 |
* @return
|
508 |
*/
|
509 |
abstract public Object getData(int x, int y, int band); |
510 |
|
511 |
/**
|
512 |
* Actualiza la/s banda/s especificadas en la imagen.
|
513 |
* @param width ancho
|
514 |
* @param height alto
|
515 |
* @param rp reproyecci?n
|
516 |
* @param img imagen
|
517 |
* @param flags que bandas [ RED_BAND | GREEN_BAND | BLUE_BAND ]
|
518 |
* @return img
|
519 |
* @throws SupersamplingNotSupportedException
|
520 |
*/
|
521 |
abstract public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBand)throws SupersamplingNotSupportedException; |
522 |
|
523 |
public int getBandCount() { return bandCount; } |
524 |
|
525 |
/**
|
526 |
* Asocia un colorBand al rojo, verde o azul.
|
527 |
* @param flag cual (o cuales) de las bandas.
|
528 |
* @param nBand que colorBand
|
529 |
*/
|
530 |
|
531 |
public void setBand(int flag, int bandNr) { |
532 |
if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) rBandNr = bandNr;
|
533 |
if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) gBandNr = bandNr;
|
534 |
if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) bBandNr = bandNr;
|
535 |
} |
536 |
|
537 |
/**
|
538 |
* Devuelve el colorBand activo en la banda especificada.
|
539 |
* @param flag banda.
|
540 |
*/
|
541 |
|
542 |
public int getBand(int flag) { |
543 |
if (flag == GeoRasterFile.RED_BAND) return rBandNr; |
544 |
if (flag == GeoRasterFile.GREEN_BAND) return gBandNr; |
545 |
if (flag == GeoRasterFile.BLUE_BAND) return bBandNr; |
546 |
return -1; |
547 |
} |
548 |
|
549 |
/**
|
550 |
* @return Returns the dataType.
|
551 |
*/
|
552 |
public int getDataType() { |
553 |
return dataType;
|
554 |
} |
555 |
|
556 |
/**
|
557 |
* @param dataType The dataType to set.
|
558 |
*/
|
559 |
public void setDataType(int dataType) { |
560 |
this.dataType = dataType;
|
561 |
} |
562 |
|
563 |
public IObjList getObjects() {
|
564 |
// TODO hay que a?adir el raster a la lista de objetos
|
565 |
IObjList oList = new PxObjList(proj);
|
566 |
return oList;
|
567 |
} |
568 |
|
569 |
/**
|
570 |
* Calcula los par?metros de un worl file a partir de las esquinas del raster.
|
571 |
* 1. X pixel size A
|
572 |
* 2. X rotation term D
|
573 |
* 3. Y rotation term B
|
574 |
* 4. Y pixel size E
|
575 |
* 5. X coordinate of upper left corner C
|
576 |
* 6. Y coordinate of upper left corner F
|
577 |
* where the real-world coordinates x',y' can be calculated from
|
578 |
* the image coordinates x,y with the equations
|
579 |
* x' = Ax + By + C and y' = Dx + Ey + F.
|
580 |
* The signs of the first 4 parameters depend on the orientation
|
581 |
* of the image. In the usual case where north is more or less
|
582 |
* at the top of the image, the X pixel size will be positive
|
583 |
* and the Y pixel size will be negative. For a south-up image,
|
584 |
* these signs would be reversed.
|
585 |
*
|
586 |
* You can calculate the World file parameters yourself based
|
587 |
* on the corner coordinates. The X and Y pixel sizes can be
|
588 |
* determined simply by dividing the distance between two
|
589 |
* adjacent corners by the number of columns or rows in the image.
|
590 |
* The rotation terms are calculated with these equations:
|
591 |
*
|
592 |
* # B = (A * number_of_columns + C - lower_right_x') / number_of_rows * -1
|
593 |
* # D = (E * number_of_rows + F - lower_right_y') / number_of_columns * -1
|
594 |
*
|
595 |
* @param corner (tl, tr, br, bl)
|
596 |
* @return
|
597 |
*/
|
598 |
public static double [] cornersToWorldFile(Point2D [] esq, Dimension size) { |
599 |
double a=0,b=0,c=0,d=0,e=0,f=0; |
600 |
double x1 = esq[0].getX(), y1 = esq[0].getY(); |
601 |
double x2 = esq[1].getX(), y2 = esq[1].getY(); |
602 |
double x3 = esq[2].getX(), y3 = esq[2].getY(); |
603 |
double x4 = esq[3].getX(), y4 = esq[3].getY(); |
604 |
// A: X-scale
|
605 |
a = Math.abs( Math.sqrt( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) |
606 |
/ size.getWidth()); |
607 |
|
608 |
// E: negative Y-scale
|
609 |
e = - Math.abs(Math.sqrt((x1-x4)*(x1-x4)+ |
610 |
(y1-y4)*(y1-y4))/size.getHeight()); |
611 |
|
612 |
// C, F: upper-left coordinates
|
613 |
c = x1; |
614 |
f = y1; |
615 |
|
616 |
// B & D: rotation parameters
|
617 |
b = (a * size.getWidth() + c - x3 ) / size.getHeight() * -1;
|
618 |
d = (e * size.getHeight() + f - y3 ) / size.getWidth() * -1;
|
619 |
|
620 |
double [] wf = {a,d,b,e,c,f}; |
621 |
return wf;
|
622 |
} |
623 |
public static String printWF(String fName, Point2D [] esq, Dimension sz) { |
624 |
double [] wf = GeoRasterFile.cornersToWorldFile(esq, sz); |
625 |
System.out.println("wf para "+fName); |
626 |
System.out.println(esq+"\n"+sz); |
627 |
String wfData = ""; |
628 |
for (int i=0; i<6; i++) |
629 |
wfData += wf[i]+"\n";
|
630 |
System.out.println(wfData);
|
631 |
return wfData;
|
632 |
} |
633 |
|
634 |
public static void saveWF(String fName, String data) throws IOException { |
635 |
FileWriter fw = new FileWriter(fName); |
636 |
fw.write(data); |
637 |
fw.flush(); |
638 |
fw.close(); |
639 |
} |
640 |
|
641 |
/**
|
642 |
* Cosulta si hay que verificar la relaci?n de aspecto de la imagen, es decir comprueba que el ancho/alto
|
643 |
* pasados a updateImage coinciden con el ancho/alto solicitado en setView a la imagen
|
644 |
* @return true si est? verificando la relaci?n de aspecto.
|
645 |
*/
|
646 |
public boolean mustVerifySize() { |
647 |
return verifySize;
|
648 |
} |
649 |
|
650 |
/**
|
651 |
* Asigna el flag que dice si hay que verificar la relaci?n de aspecto de la imagen, es decir
|
652 |
* comprueba que el ancho/alto pasados a updateImage coinciden con el ancho/alto solicitado
|
653 |
* en setView a la imagen.
|
654 |
* @return true si est? verificando la relaci?n de aspecto.
|
655 |
*/
|
656 |
public void setMustVerifySize(boolean verifySize) { |
657 |
this.verifySize = verifySize;
|
658 |
} |
659 |
|
660 |
/**
|
661 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas reales.
|
662 |
* No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
|
663 |
* pixeles de disco.
|
664 |
* @param x Posici?n X superior izquierda
|
665 |
* @param y Posici?n Y superior izquierda
|
666 |
* @param w Ancho en coordenadas reales
|
667 |
* @param h Alto en coordenadas reales
|
668 |
* @param rasterBuf Buffer de datos
|
669 |
* @param bandList
|
670 |
* @return Buffer de datos
|
671 |
*/
|
672 |
abstract public RasterBuf getWindowRaster(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf); |
673 |
|
674 |
/**
|
675 |
* Obtiene una ventana de datos de la imagen a partir de coordenadas pixel.
|
676 |
* No aplica supersampleo ni subsampleo sino que devuelve una matriz de igual tama?o a los
|
677 |
* pixeles de disco.
|
678 |
* @param x Posici?n X superior izquierda
|
679 |
* @param y Posici?n Y superior izquierda
|
680 |
* @param w Ancho en coordenadas reales
|
681 |
* @param h Alto en coordenadas reales
|
682 |
* @param rasterBuf Buffer de datos
|
683 |
* @param bandList
|
684 |
* @return Buffer de datos
|
685 |
*/
|
686 |
abstract public RasterBuf getWindowRaster(int x, int y, int w, int h, BandList bandList, RasterBuf rasterBuf); |
687 |
|
688 |
abstract public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band); |
689 |
|
690 |
abstract public int getBlockSize(); |
691 |
|
692 |
/**
|
693 |
* Obtiene el objeto que contiene los metadatos. Este m?todo debe ser redefinido por los
|
694 |
* drivers si necesitan devolver metadatos.
|
695 |
* @return
|
696 |
*/
|
697 |
public Metadata getMetadata(){
|
698 |
return null; |
699 |
} |
700 |
|
701 |
/**
|
702 |
* Asigna un extent temporal que puede coincidir con el de la vista. Esto es
|
703 |
* util para cargar imagenes sin georreferenciar ya que podemos asignar el extent
|
704 |
* que queramos para ajustarnos a una vista concreta
|
705 |
* @param tempExtent The tempExtent to set.
|
706 |
*/
|
707 |
public void setExtent(Extent ext) { |
708 |
this.extent = ext;
|
709 |
} |
710 |
|
711 |
/**
|
712 |
* Dice si el fichero tiene georreferenciaci?n o no.
|
713 |
* @return true si tiene georreferenciaci?n y false si no la tiene
|
714 |
*/
|
715 |
public boolean isGeoreferenced(){ |
716 |
return true; |
717 |
} |
718 |
|
719 |
/**
|
720 |
* Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
|
721 |
* el valor de esta variable cada vez que dibuja. Debe tenerse especial cuidado ya que est? consulta no es adecuada desde gvSIG ya que el
|
722 |
* dibujado se realiza asincronamente por lo que el valor puede no coincidir con el ?ltimo
|
723 |
* dibujado.
|
724 |
* @return true si se ha supersampleado y false si no se ha hecho.
|
725 |
*/
|
726 |
public boolean isSupersampling() { |
727 |
return this.isSupersampling; |
728 |
} |
729 |
|
730 |
/**
|
731 |
* Asigna el valor del paso en X e Y aplicado en el ?ltimo dibujado. Es el driver el que colocar?
|
732 |
* el valor de esta variable cada vez que dibuja.
|
733 |
* @param stepx Paso en X
|
734 |
* @param stepy Paso en Y
|
735 |
*/
|
736 |
public void setStep(int[] stepArrayx, int[] stepArrayy){ |
737 |
this.stepArrayX = stepArrayx;
|
738 |
this.stepArrayY = stepArrayy;
|
739 |
} |
740 |
|
741 |
/**
|
742 |
* Obtiene el valor del paso en X aplicado en el ?ltimo dibujado. Es el driver el que colocar?
|
743 |
* el valor de esta variable cada vez que dibuja.
|
744 |
* @return
|
745 |
*/
|
746 |
public int[] getStepX(){ |
747 |
return stepArrayX;
|
748 |
} |
749 |
|
750 |
/**
|
751 |
* Obtiene el valor del paso en Y aplicado en el ?ltimo dibujado. Es el driver el que colocar?
|
752 |
* el valor de esta variable cada vez que dibuja.
|
753 |
* @return
|
754 |
*/
|
755 |
public int[] getStepY(){ |
756 |
return stepArrayY;
|
757 |
} |
758 |
|
759 |
/**
|
760 |
* Obtiene el objeto paleta. Esta paleta es la que tiene adjunta el fichero de disco. Si es
|
761 |
* null este objeto quiere decir que no tiene paleta para su visualizaci?n.
|
762 |
* @return Palette
|
763 |
*/
|
764 |
public Palette getPalette() {
|
765 |
return palette;
|
766 |
} |
767 |
|
768 |
/**
|
769 |
* Asigna el objeto paleta. Esta paleta es la que tiene adjunta el fichero de disco. Si es
|
770 |
* null este objeto quiere decir que no tiene paleta para su visualizaci?n.
|
771 |
* @param Palette
|
772 |
*/
|
773 |
public void setPalette(Palette palette) { |
774 |
this.palette = palette;
|
775 |
} |
776 |
|
777 |
/**
|
778 |
* M?todo que indica si existe un fichero .rmf asociado al GeoRasterFile.
|
779 |
* @return
|
780 |
*/
|
781 |
public boolean getRmfExists(){ |
782 |
return this.rmfExists; |
783 |
} |
784 |
|
785 |
/**
|
786 |
* Obtiene los par?metros de la transformaci?n af?n que corresponde con los elementos de
|
787 |
* un fichero tfw.
|
788 |
* <UL>
|
789 |
* <LI>[1]tama?o de pixel en X</LI>
|
790 |
* <LI>[2]rotaci?n en X</LI>
|
791 |
* <LI>[4]rotaci?n en Y</LI>
|
792 |
* <LI>[5]tama?o de pixel en Y</LI>
|
793 |
* <LI>[0]origen en X</LI>
|
794 |
* <LI>[3]origen en Y</LI>
|
795 |
* </UL>
|
796 |
* Este m?todo debe ser reimplementado por el driver si tiene esta informaci?n. En principio
|
797 |
* Gdal es capaz de proporcionarla de esta forma.
|
798 |
* @return vector de double con los elementos de la transformaci?n af?n.
|
799 |
*/
|
800 |
public double[] getTransform(){return null;} |
801 |
} |