svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / fmap / layers / FLyrGeoRaster.java @ 5352
History | View | Annotate | Download (15.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package com.iver.cit.gvsig.fmap.layers; |
20 |
|
21 |
import java.awt.Graphics2D; |
22 |
import java.awt.geom.Point2D; |
23 |
import java.awt.geom.Rectangle2D; |
24 |
import java.awt.image.BufferedImage; |
25 |
|
26 |
import org.cresques.px.Extent; |
27 |
|
28 |
import com.iver.andami.PluginServices; |
29 |
import com.iver.cit.gvsig.fmap.DriverException; |
30 |
import com.iver.cit.gvsig.fmap.FMap; |
31 |
import com.iver.cit.gvsig.fmap.ViewPort; |
32 |
import com.iver.cit.gvsig.gui.View; |
33 |
import com.iver.utiles.XMLEntity; |
34 |
import com.iver.utiles.swing.threads.Cancellable; |
35 |
|
36 |
|
37 |
/**
|
38 |
* Clase de capa raster georeferenciada.
|
39 |
*
|
40 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
41 |
*/
|
42 |
public class FLyrGeoRaster extends FLyrRaster{ |
43 |
|
44 |
//**********************Vars****************************************
|
45 |
private Extent assignExtent = null; |
46 |
private IStackZoom zoom = null; |
47 |
private FLyrPoints lyrPoints = null; |
48 |
private double widthPxImg, heightPxImg; |
49 |
private ViewPort viewPortWithoutTransformation = null; |
50 |
/**
|
51 |
* Dialogo de georreferenciaci?n asociado a la capa.
|
52 |
*/
|
53 |
private IGeoUi geoUI = null; |
54 |
private IPersistence persistence = null; |
55 |
//**********************End Vars************************************
|
56 |
|
57 |
//**********************Methods*************************************
|
58 |
|
59 |
public FLyrGeoRaster(){
|
60 |
|
61 |
} |
62 |
|
63 |
/**
|
64 |
* Contructor
|
65 |
*/
|
66 |
public FLyrGeoRaster(IPersistence persistence, IStackZoom zoom){
|
67 |
this.persistence = persistence;
|
68 |
this.zoom = zoom;
|
69 |
} |
70 |
|
71 |
/**
|
72 |
* Funci?n encargada de realizar la transformaci?n para las coordenadas
|
73 |
* del viewPort que se quiere dibujar. Para esta transformaci?n se tiene
|
74 |
* en cuenta que la imagen tiene un extent propio y uno asignado por el
|
75 |
* usuario por lo que habr? que transformar las coordenadas que solicita
|
76 |
* el usuario en coordenadas de la imagen.
|
77 |
* @param vp ViewPort solicitado por el usuario.
|
78 |
* @return ViewPort transformado a coordenadas de la imagen.
|
79 |
*/
|
80 |
public ViewPort toImageCoord(ViewPort vp) throws DriverException{ |
81 |
this.viewPortWithoutTransformation = vp;
|
82 |
|
83 |
if(this.assignExtent == null) |
84 |
return vp;
|
85 |
|
86 |
//Para la transformaci?n trasladamos virtualmente los extents, el real de la imagen
|
87 |
// y el asignado al centro del eje de coordenadas. Despu?s trasladamos el viewPort
|
88 |
//(vpTrasExtent) y con las proporciones de los 2 extents hallamos en ancho, alto y
|
89 |
//coordenada de inicio del viewport. Todo esto equivale a trasladar y escalar el
|
90 |
//viewPort para hallar el nuevo.
|
91 |
|
92 |
Rectangle2D userExtent = new Rectangle2D.Double(assignExtent.minX(), |
93 |
assignExtent.minY(), |
94 |
assignExtent.width(), |
95 |
assignExtent.height()); |
96 |
Rectangle2D imageExtent = super.getFullExtent(); |
97 |
Rectangle2D vpExtent = vp.getExtent();
|
98 |
|
99 |
//Traslaci?n del viewport estando el extent posicionado en el 0,0
|
100 |
Rectangle2D vpTrasExtent = new Rectangle2D.Double( vpExtent.getMinX() - userExtent.getMinX(), |
101 |
vpExtent.getMinY() - userExtent.getMinY(), |
102 |
vpExtent.getWidth(), |
103 |
vpExtent.getHeight()); |
104 |
double x = 0D, y = 0D, width = 0D, height = 0D; |
105 |
|
106 |
//Escalado de la anchura y altura. No hace falta traslaci?n
|
107 |
width = (vpExtent.getWidth() * imageExtent.getWidth())/assignExtent.width(); |
108 |
height = (vpExtent.getHeight() * imageExtent.getHeight())/assignExtent.height(); |
109 |
|
110 |
//Escalado del min del viewport para hallar el min del nuevo
|
111 |
x = (vpTrasExtent.getMinX() * imageExtent.getWidth())/assignExtent.width(); |
112 |
y = (vpTrasExtent.getMinY() * imageExtent.getHeight())/assignExtent.height(); |
113 |
|
114 |
//Traslaci?n
|
115 |
x += imageExtent.getMinX(); |
116 |
y += imageExtent.getMinY(); |
117 |
|
118 |
Rectangle2D newExtent = new Rectangle2D.Double(x, y, width, height); |
119 |
|
120 |
ViewPort viewPort = new ViewPort(vp.getProjection());
|
121 |
viewPort.setExtent(newExtent); |
122 |
viewPort.setImageSize(vp.getImageSize()); |
123 |
viewPort.setScale(); |
124 |
|
125 |
return viewPort;
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* Dibujado de la capa de raster georeferenciado aplicando la
|
130 |
* transformaci?n del viewPort.
|
131 |
*/
|
132 |
public void draw(BufferedImage image, Graphics2D g, ViewPort vp, |
133 |
Cancellable cancel,double scale) throws DriverException { |
134 |
super.draw(image, g, this.toImageCoord(vp), cancel, scale); |
135 |
} |
136 |
|
137 |
/**
|
138 |
* Impresi?n de la capa de raster georeferenciado aplicando la
|
139 |
* transformaci?n del viewPort.
|
140 |
*/
|
141 |
public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, |
142 |
double scale)throws DriverException { |
143 |
super.print(g, this.toImageCoord(viewPort), cancel, scale); |
144 |
} |
145 |
|
146 |
/**
|
147 |
* Impresi?n de la capa de raster georeferenciado aplicando la
|
148 |
* transformaci?n del viewPort.
|
149 |
*/
|
150 |
public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale) |
151 |
throws DriverException {
|
152 |
_print(g, this.toImageCoord(viewPort), cancel, scale);
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* Transforma un pixel de la imagen cuyas coordenadas son (0..maxWidth, 0..maxHeight)
|
157 |
* en coordenadas del mundo real a partir del extent asignado a la imagen.
|
158 |
* @param pixel Pixel de la imagen
|
159 |
* @return Coordenadas del mundo de ese pixel
|
160 |
*/
|
161 |
public Point2D img2World(Point2D pixel){ |
162 |
Point2D p = new Point2D.Double(); |
163 |
double wcWidth = 0.0, wcHeight = 0.0; |
164 |
double mapImgX = 0, mapImgY = 0; |
165 |
try{
|
166 |
wcWidth = getFullExtent().getWidth(); |
167 |
wcHeight = getFullExtent().getHeight(); |
168 |
mapImgX = ((((pixel.getX() * wcWidth) / widthPxImg)) + this.getFullExtent().getMinX());
|
169 |
mapImgY = ((((pixel.getY() * wcHeight) / heightPxImg)) + this.getFullExtent().getMinY());
|
170 |
}catch(DriverException ex){}
|
171 |
p.setLocation(mapImgX, mapImgY); |
172 |
return p;
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Transforma un pixel de la imagen cuyas coordenadas son (0..maxWidth, 0..maxHeight)
|
177 |
* en coordenadas del mundo real a partir del extent asignado a la imagen.
|
178 |
* @param x Coordenada X del pixel de la imagen
|
179 |
* @param y Coordenada Y del pixel de la imagen
|
180 |
* @return Coordenadas del mundo de ese pixel
|
181 |
*/
|
182 |
public Point2D img2World(double x, double y){ |
183 |
Point2D p = new Point2D.Double(); |
184 |
p.setLocation(x, y); |
185 |
return this.img2World(p); |
186 |
} |
187 |
|
188 |
/**
|
189 |
* Transforma una coordenada del mundo real a un pixel (0..maxWidth, 0..maxHeight)
|
190 |
* en coordenadas de la imagen a partir del extent asignado a esta.
|
191 |
* @param wcPoint coordenada del mundo
|
192 |
* @return Pixel de la imagen
|
193 |
*/
|
194 |
public Point2D world2Img(Point2D wcPoint){ |
195 |
//Si el punto cae fuera de la imagen salimos devolviendo null
|
196 |
|
197 |
while( wcPoint.getX() < this.getMinX() || |
198 |
wcPoint.getX() > this.getMaxX() ||
|
199 |
wcPoint.getY() < this.getMinY() ||
|
200 |
wcPoint.getY() > this.getMaxY())
|
201 |
return null; |
202 |
|
203 |
//Hallamos pixelImg q son las coordenadas en pixeles de la imagen que corresponden
|
204 |
//con el punto wcPoint
|
205 |
double wcWidth = 0.0, wcHeight = 0.0, ptoWCX = 0.0, ptoWCY = 0.0; |
206 |
int pixelImgX = 0, pixelImgY = 0; |
207 |
try{
|
208 |
wcWidth = this.getFullExtent().getWidth();
|
209 |
wcHeight = this.getFullExtent().getHeight();
|
210 |
ptoWCX = wcPoint.getX() - this.getFullExtent().getMinX();
|
211 |
ptoWCY = wcPoint.getY() - this.getFullExtent().getMinY();
|
212 |
pixelImgX = (int)((ptoWCX * this.getImageWidth()) / wcWidth); |
213 |
pixelImgY = (int)((ptoWCY * this.getImageHeight()) / wcHeight); |
214 |
}catch(DriverException ex){}
|
215 |
|
216 |
Point2D result = new Point2D.Double(); |
217 |
result.setLocation(pixelImgX, pixelImgY); |
218 |
return result;
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* Transforma una coordenada del mundo real a un pixel (0..maxWidth, 0..maxHeight)
|
223 |
* en coordenadas de la imagen a partir del extent asignado a esta.
|
224 |
* @param x wcPoint coordenada X del mundo
|
225 |
* @param y wcPoint coordenada Y del mundo
|
226 |
* @return Pixel de la imagen
|
227 |
*/
|
228 |
public Point2D world2Img(double x, double y){ |
229 |
Point2D p = new Point2D.Double(); |
230 |
p.setLocation(x, y); |
231 |
return this.world2Img(p); |
232 |
} |
233 |
|
234 |
|
235 |
/**
|
236 |
* Recupera el extent asignado, si existe y llama al setXMLEntity de la capa de puntos
|
237 |
*/
|
238 |
public void setXMLEntity(XMLEntity xml)throws XMLException { |
239 |
super.setXMLEntity(xml);
|
240 |
|
241 |
if (xml.contains("raster.assignExtent.minX") && |
242 |
xml.contains("raster.assignExtent.minY") &&
|
243 |
xml.contains("raster.assignExtent.maxX") &&
|
244 |
xml.contains("raster.assignExtent.maxY")){
|
245 |
this.assignExtent = new Extent(xml.getDoubleProperty("raster.assignExtent.minX"), |
246 |
xml.getDoubleProperty("raster.assignExtent.minY"),
|
247 |
xml.getDoubleProperty("raster.assignExtent.maxX"),
|
248 |
xml.getDoubleProperty("raster.assignExtent.maxY"));
|
249 |
} |
250 |
|
251 |
if (xml.contains("raster.assignExtent.imageWidth") && |
252 |
xml.contains("raster.assignExtent.imageHeight")){
|
253 |
this.widthPxImg = xml.getDoubleProperty("raster.assignExtent.imageWidth"); |
254 |
this.heightPxImg = xml.getDoubleProperty("raster.assignExtent.imageHeight"); |
255 |
} |
256 |
} |
257 |
|
258 |
/**
|
259 |
* Cambia el nombre a la capa de puntos si esta empieza por * para que no se salve con ese
|
260 |
* nombre y llama a getXMLEntity del padre. Salva el extent asignado, si existe y llama a
|
261 |
* getXMLEntity de la capa de puntos para que se guarden los puntos de control en el proyecto.
|
262 |
* @throws XMLException
|
263 |
*/
|
264 |
public XMLEntity getXMLEntity() throws XMLException { |
265 |
boolean active = false; |
266 |
if(getName().startsWith("*")){ |
267 |
setName(getName().substring(1, getName().length()));
|
268 |
active = true;
|
269 |
} |
270 |
|
271 |
XMLEntity xml = super.getXMLEntity();
|
272 |
|
273 |
if(active)
|
274 |
setName("*"+getName());
|
275 |
|
276 |
if(this.assignExtent != null){ |
277 |
xml.putProperty("raster.assignExtent.minX", assignExtent.getMin().getX());
|
278 |
xml.putProperty("raster.assignExtent.minY", assignExtent.getMin().getY());
|
279 |
xml.putProperty("raster.assignExtent.maxX", assignExtent.getMax().getX());
|
280 |
xml.putProperty("raster.assignExtent.maxY", assignExtent.getMax().getY());
|
281 |
xml.putProperty("raster.assignExtent.imageWidth", this.getImageWidth()); |
282 |
xml.putProperty("raster.assignExtent.imageHeight", this.getImageHeight()); |
283 |
} |
284 |
|
285 |
return xml;
|
286 |
} |
287 |
|
288 |
/**
|
289 |
* A partir de nuevas coordenadas actualiza la vista, minimagen, capa de puntos el
|
290 |
* dialogo y la tabla.
|
291 |
*
|
292 |
*/
|
293 |
public void updateData(int nPunto, Point2D pixel, Point2D map, View view){ |
294 |
this.geoUI.updateData(nPunto, pixel, map, view);
|
295 |
} |
296 |
//**********************End Methods**********************************
|
297 |
|
298 |
//**********************Setters & Getters****************************
|
299 |
/**
|
300 |
* @return Returns the viewPortWithoutTransform.
|
301 |
*/
|
302 |
public ViewPort getViewPortWithoutTransformation() {
|
303 |
return viewPortWithoutTransformation;
|
304 |
} |
305 |
|
306 |
/**
|
307 |
* Asigna la capa de puntos asociada a la capa Georraster
|
308 |
* @param lyr Capa de puntos
|
309 |
*/
|
310 |
public void setFLyrPoints(FLyrPoints lyr){ |
311 |
this.lyrPoints = lyr;
|
312 |
} |
313 |
|
314 |
/**
|
315 |
* Obtiene la capa de puntos asociada a la capa Georraster. Si no existe
|
316 |
* crear? una.
|
317 |
* @return Capa de puntos
|
318 |
*/
|
319 |
public FLyrPoints getFLyrPoints()throws ClassCastException{ |
320 |
//Si no existe una capa de puntos la creamos
|
321 |
if(lyrPoints == null){ |
322 |
View theView = (View) PluginServices.getMDIManager().getActiveView(); |
323 |
FMap fmap = theView.getMapControl().getMapContext(); |
324 |
lyrPoints = new FLyrPoints(this, persistence); |
325 |
fmap.setGraphicsLayer(lyrPoints); |
326 |
} |
327 |
return lyrPoints;
|
328 |
} |
329 |
|
330 |
/**
|
331 |
* Asigna la dimensi?n de la imagen de disco en pixeles
|
332 |
* @param w Ancho
|
333 |
* @param h Alto
|
334 |
*/
|
335 |
public void setImageDimension(double w, double h){ |
336 |
this.widthPxImg = w;
|
337 |
this.heightPxImg = h;
|
338 |
} |
339 |
|
340 |
/**
|
341 |
* Obtiene el ancho de la imagen de disco en pixeles
|
342 |
* @return
|
343 |
*/
|
344 |
public double getImageWidth(){ |
345 |
return this.widthPxImg; |
346 |
} |
347 |
|
348 |
/**
|
349 |
* Obtiene el alto de la imagen de disco en pixeles
|
350 |
* @return
|
351 |
*/
|
352 |
public double getImageHeight(){ |
353 |
return this.heightPxImg; |
354 |
} |
355 |
/**
|
356 |
* @return Returns the geoDialog.
|
357 |
*/
|
358 |
public IGeoUi getGeoDialog() {
|
359 |
return geoUI;
|
360 |
} |
361 |
/**
|
362 |
* @param geoDialog The geoDialog to set.
|
363 |
*/
|
364 |
public void setGeoDialog(IGeoUi geoDialog) { |
365 |
this.geoUI = geoDialog;
|
366 |
} |
367 |
/**
|
368 |
* Obtiene el extent de la capa
|
369 |
* @return extent en Rectangle2D.
|
370 |
*/
|
371 |
public Rectangle2D getFullExtent()throws DriverException { |
372 |
if(assignExtent == null) |
373 |
return super.getFullExtent(); |
374 |
return assignExtent.toRectangle2D();
|
375 |
} |
376 |
|
377 |
/**
|
378 |
* Obtiene el extent de la capa
|
379 |
* @return extent de la capa
|
380 |
*/
|
381 |
public Extent getAssignExtent(){
|
382 |
try{
|
383 |
if(assignExtent == null) |
384 |
return new Extent( super.getFullExtent().getMinX(), |
385 |
super.getFullExtent().getMinY(),
|
386 |
super.getFullExtent().getMaxX(),
|
387 |
super.getFullExtent().getMaxY());
|
388 |
}catch(DriverException ex){}
|
389 |
return assignExtent;
|
390 |
} |
391 |
|
392 |
/**
|
393 |
* Obtiene el valor del pixel del Image en la posici?n x,y
|
394 |
* @param x Posici?n x
|
395 |
* @param y Posici?n y
|
396 |
* @return valor de pixel
|
397 |
*/
|
398 |
/*public int[] getPixel(double wcx, double wcy){
|
399 |
return ((RasterFileAdapter) source).getPixel(wcx, wcy);
|
400 |
}*/
|
401 |
|
402 |
/**
|
403 |
* Obtiene la coordenada X m?xima
|
404 |
*/
|
405 |
public double getMaxX(){ |
406 |
if(assignExtent == null) |
407 |
return super.getMaxX(); |
408 |
return assignExtent.maxX();
|
409 |
} |
410 |
|
411 |
/**
|
412 |
* Obtiene la coordenada Y m?xima
|
413 |
*/
|
414 |
public double getMaxY(){ |
415 |
if(assignExtent == null) |
416 |
return super.getMaxY(); |
417 |
return assignExtent.maxY();
|
418 |
} |
419 |
|
420 |
/**
|
421 |
* Obtiene la coordenada X m?nima
|
422 |
*/
|
423 |
public double getMinX(){ |
424 |
if(assignExtent == null) |
425 |
return super.getMinX(); |
426 |
return assignExtent.minX();
|
427 |
} |
428 |
|
429 |
/**
|
430 |
* Obtiene la coordenada Y m?nima
|
431 |
*/
|
432 |
public double getMinY(){ |
433 |
if(assignExtent == null) |
434 |
return super.getMinY(); |
435 |
return assignExtent.minY();
|
436 |
} |
437 |
|
438 |
/**
|
439 |
* Obtiene el alto
|
440 |
*/
|
441 |
public double getHeight(){ |
442 |
if(assignExtent == null) |
443 |
return super.getHeight(); |
444 |
return assignExtent.height();
|
445 |
} |
446 |
|
447 |
/**
|
448 |
* Obtiene el ancho
|
449 |
*/
|
450 |
public double getWidth(){ |
451 |
if(assignExtent == null) |
452 |
return super.getWidth(); |
453 |
return assignExtent.width();
|
454 |
} |
455 |
|
456 |
/**
|
457 |
* @param assignExtent The assignExtent to set.
|
458 |
*/
|
459 |
public void setAssignExtent(Extent assignExtent) { |
460 |
if(assignExtent.getMax().distance(assignExtent.getMin()) > 0){ |
461 |
this.assignExtent = assignExtent;
|
462 |
zoom.setZoom(assignExtent); |
463 |
} |
464 |
} |
465 |
|
466 |
/**
|
467 |
* Asigna el ?ltimo extent seleccionado si hay alguno
|
468 |
*/
|
469 |
public void setLastExtent(){ |
470 |
Extent e = null;
|
471 |
e = zoom.getLastZoom(); |
472 |
if(e != null) |
473 |
this.assignExtent = e;
|
474 |
} |
475 |
|
476 |
/**
|
477 |
* Obtiene la pila de extents aplicados
|
478 |
* @return pila de extents
|
479 |
*/
|
480 |
public IStackZoom getStackZoom(){
|
481 |
return zoom;
|
482 |
} |
483 |
|
484 |
/**
|
485 |
* Obtiene la capa de puntos
|
486 |
* @return FLyrPoints
|
487 |
*/
|
488 |
public FLyrPoints getLyrPoints() {
|
489 |
return lyrPoints;
|
490 |
} |
491 |
|
492 |
/**
|
493 |
* Obtiene el objeto que gestiona la persistencia
|
494 |
* @return interfaz IPersistence
|
495 |
*/
|
496 |
public IPersistence getPersistence() {
|
497 |
return persistence;
|
498 |
} |
499 |
|
500 |
/**
|
501 |
* Asigna el objeto que gestiona la persistencia
|
502 |
* @param persistence
|
503 |
*/
|
504 |
public void setPersistence(IPersistence persistence) { |
505 |
this.persistence = persistence;
|
506 |
if(this.lyrPoints != null) |
507 |
this.lyrPoints.setPersistence(persistence);
|
508 |
} |
509 |
|
510 |
/**
|
511 |
* Obtiene el objeto que gestiona la pila de zooms
|
512 |
* @return IStackZoom
|
513 |
*/
|
514 |
public IStackZoom getZoom() {
|
515 |
return zoom;
|
516 |
} |
517 |
|
518 |
/**
|
519 |
* Asigna el objeto que gestiona la pila de zooms
|
520 |
* @param IStackZoom
|
521 |
*/
|
522 |
public void setZoom(IStackZoom zoom) { |
523 |
this.zoom = zoom;
|
524 |
} |
525 |
//**********************End Setters & Getters************************
|
526 |
|
527 |
} |