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