svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ViewPort.java @ 5369
History | View | Annotate | Download (21.3 KB)
1 | 1100 | fjp | /* 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 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 213 | fernando | package com.iver.cit.gvsig.fmap; |
42 | |||
43 | import java.awt.Color; |
||
44 | import java.awt.Dimension; |
||
45 | 234 | fjp | import java.awt.Point; |
46 | 4213 | caballero | import java.awt.Rectangle; |
47 | 213 | fernando | import java.awt.geom.AffineTransform; |
48 | 234 | fjp | import java.awt.geom.NoninvertibleTransformException; |
49 | 213 | fernando | import java.awt.geom.Point2D; |
50 | import java.awt.geom.Rectangle2D; |
||
51 | import java.util.ArrayList; |
||
52 | |||
53 | 3884 | caballero | import org.cresques.cts.GeoCalc; |
54 | import org.cresques.cts.IProjection; |
||
55 | import org.cresques.cts.ProjectionPool; |
||
56 | import org.cresques.cts.gt2.CSUTM; |
||
57 | 346 | fernando | |
58 | 3884 | caballero | import com.iver.utiles.StringUtilities; |
59 | import com.iver.utiles.XMLEntity; |
||
60 | |||
61 | |||
62 | 956 | vcaballero | /**
|
63 | 1036 | vcaballero | * Clase con atributos de la vista.
|
64 | 1443 | jmorell | * 050211, jmorell: A?ado los Grados como unidad de mapa.
|
65 | 956 | vcaballero | *
|
66 | * @author Vicente Caballero Navarro
|
||
67 | */
|
||
68 | public class ViewPort { |
||
69 | public static int KILOMETROS = 0; |
||
70 | public static int METROS = 1; |
||
71 | public static int CENTIMETRO = 2; |
||
72 | public static int MILIMETRO = 3; |
||
73 | public static int MILLAS = 4; |
||
74 | public static int YARDAS = 5; |
||
75 | public static int PIES = 6; |
||
76 | public static int PULGADAS = 7; |
||
77 | 1443 | jmorell | public static int GRADOS = 8; |
78 | 435 | vcaballero | |
79 | 346 | fernando | /**
|
80 | 956 | vcaballero | * Resoluci?n (Puntos por pulgada) de la vista actual. Se necesita para los
|
81 | * c?lculos de escala geogr?fica.
|
||
82 | 346 | fernando | */
|
83 | 956 | vcaballero | private static int dpi = java.awt.Toolkit.getDefaultToolkit() |
84 | .getScreenResolution(); |
||
85 | private Rectangle2D extent; |
||
86 | private Rectangle2D adjustedExtent; |
||
87 | private ExtentHistory extents = new ExtentHistory(); |
||
88 | private Dimension imageSize; |
||
89 | private AffineTransform trans = new AffineTransform(); |
||
90 | 213 | fernando | private int distanceUnits = METROS; |
91 | private int mapUnits = METROS; |
||
92 | 1197 | fernando | private ArrayList listeners = new ArrayList(); |
93 | 956 | vcaballero | private Point2D offset = new Point2D.Double(0, 0); |
94 | private Rectangle2D clip; |
||
95 | 1282 | fjp | private Color backColor = null; //Color.WHITE; |
96 | 346 | fernando | private IProjection proj;
|
97 | 956 | vcaballero | private double dist1pixel; |
98 | private double dist3pixel; |
||
99 | 213 | fernando | private double scale; |
100 | 3683 | caballero | private Rectangle2D cliprect; |
101 | 956 | vcaballero | |
102 | /**
|
||
103 | * Crea un nuevo ViewPort.
|
||
104 | *
|
||
105 | 1036 | vcaballero | * @param proj Proyecci?n.
|
106 | 956 | vcaballero | */
|
107 | public ViewPort(IProjection proj) {
|
||
108 | 867 | fjp | // Por defecto
|
109 | 956 | vcaballero | this.proj = proj;
|
110 | 867 | fjp | } |
111 | 213 | fernando | |
112 | 956 | vcaballero | /**
|
113 | 1036 | vcaballero | * A?ade un ViewPortListener al extentListener.
|
114 | 956 | vcaballero | *
|
115 | 1036 | vcaballero | * @param arg0 ViewPortListener.
|
116 | 956 | vcaballero | *
|
117 | 1036 | vcaballero | * @return True si ha sido a?adida correctamente.
|
118 | 956 | vcaballero | */
|
119 | 1197 | fernando | public boolean addViewPortListener(ViewPortListener arg0) { |
120 | return listeners.add(arg0);
|
||
121 | 213 | fernando | } |
122 | |||
123 | 956 | vcaballero | /**
|
124 | 1036 | vcaballero | * Borra el ViewPortListener que se pasa como par?metro delos
|
125 | * extentListener.
|
||
126 | 956 | vcaballero | *
|
127 | 1036 | vcaballero | * @param arg0 ViewPortListener.
|
128 | 956 | vcaballero | *
|
129 | 1036 | vcaballero | * @return True si ha sido borrado correctamente.
|
130 | 956 | vcaballero | */
|
131 | 1197 | fernando | public boolean removeViewPortListener(ViewPortListener arg0) { |
132 | return listeners.remove(arg0);
|
||
133 | 213 | fernando | } |
134 | |||
135 | 956 | vcaballero | /**
|
136 | 1036 | vcaballero | * Devuelve la distancia en pixels a partir de una distancia real.
|
137 | 956 | vcaballero | *
|
138 | 1036 | vcaballero | * @param d Distancia real.
|
139 | 956 | vcaballero | *
|
140 | 1036 | vcaballero | * @return Distancia en pixels.
|
141 | 956 | vcaballero | */
|
142 | public int fromMapDistance(double d) { |
||
143 | Point2D.Double pWorld = new Point2D.Double(1, 1); |
||
144 | Point2D.Double pScreen = new Point2D.Double(); |
||
145 | 234 | fjp | |
146 | 956 | vcaballero | try {
|
147 | trans.deltaTransform(pWorld, pScreen); |
||
148 | } catch (Exception e) { |
||
149 | System.err.print(e.getMessage());
|
||
150 | } |
||
151 | 213 | fernando | |
152 | 956 | vcaballero | return (int) (d * pScreen.x); |
153 | } |
||
154 | 234 | fjp | |
155 | 956 | vcaballero | /**
|
156 | 1036 | vcaballero | * Devuelve un punto en pixels a partir de una coordenada X e Y real.
|
157 | 956 | vcaballero | *
|
158 | 1036 | vcaballero | * @param x Coordenada X real.
|
159 | * @param y Coordenada Y real.
|
||
160 | 956 | vcaballero | *
|
161 | 1036 | vcaballero | * @return Punto en pixels.
|
162 | 956 | vcaballero | */
|
163 | public Point2D fromMapPoint(double x, double y) { |
||
164 | Point2D.Double pWorld = new Point2D.Double(x, y); |
||
165 | Point2D.Double pScreen = new Point2D.Double(); |
||
166 | 234 | fjp | |
167 | 956 | vcaballero | try {
|
168 | trans.transform(pWorld, pScreen); |
||
169 | } catch (Exception e) { |
||
170 | System.err.print(e.getMessage());
|
||
171 | } |
||
172 | 213 | fernando | |
173 | 956 | vcaballero | return pScreen;
|
174 | } |
||
175 | 1036 | vcaballero | |
176 | /**
|
||
177 | * Devuelve el punto en pixels a partir de un punto real.
|
||
178 | *
|
||
179 | * @param point Punto real.
|
||
180 | *
|
||
181 | * @return Punto en pixels.
|
||
182 | */
|
||
183 | 1004 | vcaballero | public Point2D fromMapPoint(Point2D point) { |
184 | 1036 | vcaballero | return fromMapPoint(point.getX(), point.getY());
|
185 | 1004 | vcaballero | } |
186 | 213 | fernando | |
187 | 956 | vcaballero | /**
|
188 | 1036 | vcaballero | * Devuelve un punto real a partir de una coordenada X e Y en pixels.
|
189 | 956 | vcaballero | *
|
190 | 1036 | vcaballero | * @param x Coordenada X en pixels.
|
191 | * @param y Coordenada Y en pixels.
|
||
192 | 956 | vcaballero | *
|
193 | 1036 | vcaballero | * @return Punto real.
|
194 | 956 | vcaballero | */
|
195 | public Point2D toMapPoint(int x, int y) { |
||
196 | Point pScreen = new Point(x, y); |
||
197 | 213 | fernando | |
198 | 956 | vcaballero | return toMapPoint(pScreen);
|
199 | } |
||
200 | 234 | fjp | |
201 | 4213 | caballero | public Rectangle2D toMapRectangle(Rectangle2D r){ |
202 | double w=toMapDistance((int)r.getWidth()); |
||
203 | double h=toMapDistance((int)r.getHeight()); |
||
204 | Point2D p1=toMapPoint((int)r.getX(),(int)r.getY()); |
||
205 | return new Rectangle2D.Double(p1.getX(),p1.getY(),w,h); |
||
206 | } |
||
207 | 956 | vcaballero | /**
|
208 | 1036 | vcaballero | * Devuelve la distancia real a partir de la distancia en pixels.
|
209 | 956 | vcaballero | *
|
210 | 1036 | vcaballero | * @param d Distancia en pixels.
|
211 | 956 | vcaballero | *
|
212 | 1036 | vcaballero | * @return Distancia real.
|
213 | 956 | vcaballero | */
|
214 | public double toMapDistance(int d) { |
||
215 | double dist = d / trans.getScaleX();
|
||
216 | 234 | fjp | |
217 | 956 | vcaballero | return dist;
|
218 | } |
||
219 | 213 | fernando | |
220 | 956 | vcaballero | /**
|
221 | 1036 | vcaballero | * Devuelve un punto real a partir de un punto en pixels.
|
222 | 956 | vcaballero | *
|
223 | 1036 | vcaballero | * @param pScreen Punto en pixels.
|
224 | 956 | vcaballero | *
|
225 | 1036 | vcaballero | * @return Punto real.
|
226 | 956 | vcaballero | *
|
227 | 1036 | vcaballero | * @throws RuntimeException
|
228 | 956 | vcaballero | */
|
229 | public Point2D toMapPoint(Point2D pScreen) { |
||
230 | Point2D.Double pWorld = new Point2D.Double(); |
||
231 | AffineTransform at;
|
||
232 | 213 | fernando | |
233 | 956 | vcaballero | try {
|
234 | at = trans.createInverse(); |
||
235 | at.transform(pScreen, pWorld); |
||
236 | } catch (NoninvertibleTransformException e) { |
||
237 | throw new RuntimeException(e); |
||
238 | } |
||
239 | 213 | fernando | |
240 | 956 | vcaballero | return pWorld;
|
241 | } |
||
242 | 1036 | vcaballero | |
243 | 999 | luisw | /**
|
244 | 1036 | vcaballero | * Calcula la distancia entre dos puntos en unidades de usuario. Los puntos
|
245 | * est?n en unidades de usuario. Se tiene en cuenta la proyecci?n, con lo
|
||
246 | * que es INDISPENSABLE que la variable proj contenga el valor correcto de
|
||
247 | * la proyecci?n.
|
||
248 | *
|
||
249 | 999 | luisw | * @param pt1
|
250 | * @param pt2
|
||
251 | 1036 | vcaballero | *
|
252 | * @return distancia real.
|
||
253 | 999 | luisw | */
|
254 | public double distanceWorld(Point2D pt1, Point2D pt2) { |
||
255 | double dist = -1; |
||
256 | dist = pt1.distance(pt2); |
||
257 | 1036 | vcaballero | |
258 | if ((proj != null) && !(proj instanceof CSUTM)) { |
||
259 | dist = new GeoCalc(proj).distanceVincenty(proj.toGeo(pt1),
|
||
260 | proj.toGeo(pt2)); |
||
261 | } |
||
262 | |||
263 | 999 | luisw | return dist;
|
264 | } |
||
265 | 213 | fernando | |
266 | 956 | vcaballero | /**
|
267 | 1036 | vcaballero | * Rellena el extent anterior como actual.
|
268 | 956 | vcaballero | */
|
269 | public void setPreviousExtent() { |
||
270 | extent = extents.removePrev(); |
||
271 | 918 | fernando | |
272 | 956 | vcaballero | //Calcula la transformaci?n af?n
|
273 | calculateAffineTransform(); |
||
274 | 680 | fernando | |
275 | 956 | vcaballero | // Lanzamos los eventos de extent cambiado
|
276 | 3683 | caballero | callExtentChanged(getAdjustedExtent()); |
277 | 956 | vcaballero | } |
278 | 213 | fernando | |
279 | 956 | vcaballero | /**
|
280 | 4910 | jaume | * <p>
|
281 | * When the zoom changes (for instance when using the zoom in or zoom out tools,
|
||
282 | * but also zooming to a selected feature or shape) the extent that covers that
|
||
283 | * area is the value returned by this method. It is not the actual area shown
|
||
284 | * in the view because it does not care about the aspect ratio. However, any
|
||
285 | * part of the real world contained in this extent is shown in the view.<br>
|
||
286 | * </p>
|
||
287 | * <p>
|
||
288 | * Probably <b>this is not what you are looking for</b>. If you are looking for
|
||
289 | * the complete extent currently shown, you must use getAdjustedExtent() method
|
||
290 | * which returns the extent that contains this one but regarding the current
|
||
291 | * view's aspect ratio.
|
||
292 | * </p>
|
||
293 | 1036 | vcaballero | * @return Extent.
|
294 | 956 | vcaballero | */
|
295 | public Rectangle2D getExtent() { |
||
296 | return extent;
|
||
297 | } |
||
298 | 213 | fernando | |
299 | /**
|
||
300 | 1036 | vcaballero | * Inserta el extent.
|
301 | 956 | vcaballero | *
|
302 | 1036 | vcaballero | * @param r Extent.
|
303 | 956 | vcaballero | */
|
304 | public void setExtent(Rectangle2D r) { |
||
305 | if (extent != null) { |
||
306 | extents.put(extent); |
||
307 | } |
||
308 | |||
309 | 3884 | caballero | //Esto comprueba que el extent no es de anchura o altura = "0"
|
310 | 956 | vcaballero | //y si es as? lo redimensiona.
|
311 | 2409 | caballero | if (r!=null &&((r.getWidth() == 0) || (r.getHeight() == 0))) { |
312 | 956 | vcaballero | extent = new Rectangle2D.Double(r.getMinX() - 0.1, |
313 | r.getMinY() - 0.1, r.getWidth() + 0.2, r.getHeight() + 0.2); |
||
314 | } else {
|
||
315 | extent = r; |
||
316 | } |
||
317 | |||
318 | //Calcula la transformaci?n af?n
|
||
319 | calculateAffineTransform(); |
||
320 | |||
321 | // Lanzamos los eventos de extent cambiado
|
||
322 | 3683 | caballero | callExtentChanged(getAdjustedExtent()); |
323 | 956 | vcaballero | } |
324 | |||
325 | /**
|
||
326 | 1036 | vcaballero | * Inserta la escala.
|
327 | 2970 | fjp | * TODO: (FJP) Que alguien me explique qu? sentido tiene esto
|
328 | 956 | vcaballero | *
|
329 | 1036 | vcaballero | * @param scale escala.
|
330 | 956 | vcaballero | */
|
331 | 1135 | vcaballero | public void setScale() { |
332 | //this.scale = scale;
|
||
333 | 956 | vcaballero | |
334 | //Calcula la transformaci?n af?n
|
||
335 | calculateAffineTransform(); |
||
336 | |||
337 | // Lanzamos los eventos de extent cambiado
|
||
338 | 3683 | caballero | callExtentChanged(getAdjustedExtent()); |
339 | 956 | vcaballero | } |
340 | |||
341 | /**
|
||
342 | * Devuelve la escala. Debe estar siempre actualizada y no calcularse nunca
|
||
343 | * aqu? pues se utiliza en el dibujado para cada geometr?a
|
||
344 | *
|
||
345 | 1036 | vcaballero | * @return Escala.
|
346 | 213 | fernando | */
|
347 | 956 | vcaballero | public double getScale() { |
348 | return proj.getScale(extent.getMinX(), extent.getMaxX(),
|
||
349 | imageSize.getWidth(), dpi); |
||
350 | } |
||
351 | |||
352 | /**
|
||
353 | 1036 | vcaballero | * Devuelve la matriz de transformaci?n.
|
354 | 956 | vcaballero | *
|
355 | 1036 | vcaballero | * @return Matriz de transformaci?n.
|
356 | 956 | vcaballero | */
|
357 | 213 | fernando | public AffineTransform getAffineTransform() { |
358 | return trans;
|
||
359 | } |
||
360 | 956 | vcaballero | |
361 | 213 | fernando | /**
|
362 | 1036 | vcaballero | * Devuelve las dimensiones de la imagen.
|
363 | 956 | vcaballero | *
|
364 | 213 | fernando | * @return Returns the imageSize.
|
365 | */
|
||
366 | public Dimension getImageSize() { |
||
367 | return imageSize;
|
||
368 | } |
||
369 | 956 | vcaballero | |
370 | 213 | fernando | /**
|
371 | 1036 | vcaballero | * Inserta las dimensiones de la imagen.
|
372 | 956 | vcaballero | *
|
373 | 213 | fernando | * @param imageSize The imageSize to set.
|
374 | */
|
||
375 | public void setImageSize(Dimension imageSize) { |
||
376 | this.imageSize = imageSize;
|
||
377 | calculateAffineTransform(); |
||
378 | } |
||
379 | |||
380 | 956 | vcaballero | /**
|
381 | 1036 | vcaballero | * Llamada a los listeners tras el cambio de extent.
|
382 | 956 | vcaballero | *
|
383 | 1036 | vcaballero | * @param newRect Extent.
|
384 | 956 | vcaballero | */
|
385 | 1197 | fernando | private void callExtentChanged(Rectangle2D newRect) { |
386 | 1082 | fernando | ExtentEvent ev = ExtentEvent.createExtentEvent(newRect); |
387 | 655 | fjp | |
388 | 1197 | fernando | for (int i = 0; i < listeners.size(); i++) { |
389 | ViewPortListener listener = (ViewPortListener) listeners.get(i); |
||
390 | 956 | vcaballero | listener.extentChanged(ev); |
391 | } |
||
392 | } |
||
393 | |||
394 | 213 | fernando | /**
|
395 | 1036 | vcaballero | * Llamada a los listeners tras el cambio de color.
|
396 | 956 | vcaballero | *
|
397 | 1036 | vcaballero | * @param c Color.
|
398 | 213 | fernando | */
|
399 | 1197 | fernando | private void callColorChanged(Color c) { |
400 | 1082 | fernando | ColorEvent ce = ColorEvent.createColorEvent(c); |
401 | 956 | vcaballero | |
402 | 1197 | fernando | for (int i = 0; i < listeners.size(); i++) { |
403 | ViewPortListener listener = (ViewPortListener) listeners.get(i); |
||
404 | 956 | vcaballero | listener.backColorChanged(ce); |
405 | } |
||
406 | } |
||
407 | |||
408 | /**
|
||
409 | 1036 | vcaballero | * C?lculo de la matriz de transformaci?n.
|
410 | 956 | vcaballero | *
|
411 | 3884 | caballero | * @throws RuntimeException
|
412 | 956 | vcaballero | */
|
413 | 213 | fernando | private void calculateAffineTransform() { |
414 | 956 | vcaballero | if ((imageSize == null) || (extent == null) || |
415 | (imageSize.getWidth() <= 0) || (imageSize.getHeight() <= 0)) { |
||
416 | return;
|
||
417 | } |
||
418 | 213 | fernando | |
419 | 956 | vcaballero | AffineTransform escalado = new AffineTransform(); |
420 | AffineTransform translacion = new AffineTransform(); |
||
421 | 213 | fernando | |
422 | 956 | vcaballero | double escalaX;
|
423 | double escalaY;
|
||
424 | 213 | fernando | |
425 | 956 | vcaballero | escalaX = imageSize.getWidth() / extent.getWidth(); |
426 | escalaY = imageSize.getHeight() / extent.getHeight(); |
||
427 | 213 | fernando | |
428 | 956 | vcaballero | double xCenter = extent.getCenterX();
|
429 | double yCenter = extent.getCenterY();
|
||
430 | double newHeight;
|
||
431 | double newWidth;
|
||
432 | 213 | fernando | |
433 | 956 | vcaballero | adjustedExtent = new Rectangle2D.Double(); |
434 | 213 | fernando | |
435 | 956 | vcaballero | if (escalaX < escalaY) {
|
436 | scale = escalaX; |
||
437 | newHeight = imageSize.getHeight() / scale; |
||
438 | adjustedExtent.setRect(xCenter - (extent.getWidth() / 2.0),
|
||
439 | yCenter - (newHeight / 2.0), extent.getWidth(), newHeight);
|
||
440 | } else {
|
||
441 | scale = escalaY; |
||
442 | newWidth = imageSize.getWidth() / scale; |
||
443 | adjustedExtent.setRect(xCenter - (newWidth / 2.0),
|
||
444 | yCenter - (extent.getHeight() / 2.0), newWidth,
|
||
445 | extent.getHeight()); |
||
446 | } |
||
447 | 213 | fernando | |
448 | 3683 | caballero | translacion.setToTranslation(-getAdjustedExtent().getX(), |
449 | -getAdjustedExtent().getY() - getAdjustedExtent().getHeight()); |
||
450 | 956 | vcaballero | escalado.setToScale(scale, -scale); |
451 | |||
452 | AffineTransform offsetTrans = new AffineTransform(); |
||
453 | offsetTrans.setToTranslation(offset.getX(), offset.getY()); |
||
454 | |||
455 | trans.setToIdentity(); |
||
456 | trans.concatenate(offsetTrans); |
||
457 | trans.concatenate(escalado); |
||
458 | |||
459 | trans.concatenate(translacion); |
||
460 | |||
461 | 3884 | caballero | // Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
|
462 | 956 | vcaballero | // de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
|
463 | AffineTransform at;
|
||
464 | |||
465 | 237 | fjp | try {
|
466 | at = trans.createInverse(); |
||
467 | 956 | vcaballero | |
468 | 237 | fjp | java.awt.Point pPixel = new java.awt.Point(1, 1); |
469 | 956 | vcaballero | Point2D.Float pProv = new Point2D.Float(); |
470 | at.deltaTransform(pPixel, pProv); |
||
471 | 237 | fjp | |
472 | 956 | vcaballero | dist1pixel = pProv.x; |
473 | dist3pixel = 3 * pProv.x;
|
||
474 | 237 | fjp | } catch (NoninvertibleTransformException e) { |
475 | 867 | fjp | System.err.println("transformada afin = " + trans.toString()); |
476 | 956 | vcaballero | System.err.println("extent = " + extent.toString() + |
477 | " imageSize= " + imageSize.toString());
|
||
478 | 918 | fernando | throw new RuntimeException(e); |
479 | 237 | fjp | } |
480 | 213 | fernando | } |
481 | 956 | vcaballero | |
482 | /**
|
||
483 | 1036 | vcaballero | * Inserta la desviaci?n.
|
484 | 956 | vcaballero | *
|
485 | 1036 | vcaballero | * @param p Punto.
|
486 | 956 | vcaballero | */
|
487 | public void setOffset(Point2D p) { |
||
488 | offset = p; |
||
489 | 351 | vcaballero | } |
490 | 956 | vcaballero | /**
|
491 | 5369 | jaume | * The offset is the position where to start drawing. The offset of a View is
|
492 | * always (0, 0) because the drawing area fits with the full window area. But in
|
||
493 | * a Layout it is up to the place where the FFrameView is located.
|
||
494 | 1317 | vcaballero | *
|
495 | 5369 | jaume | * @param p Point, in pixels, where the map starts.
|
496 | 1317 | vcaballero | */
|
497 | public Point2D getOffset() { |
||
498 | return offset;
|
||
499 | } |
||
500 | /**
|
||
501 | 1036 | vcaballero | * Inserta el color de fondo.
|
502 | 956 | vcaballero | *
|
503 | 1036 | vcaballero | * @param c Color de fondo.
|
504 | 956 | vcaballero | */
|
505 | 213 | fernando | public void setBackColor(Color c) { |
506 | backColor = c; |
||
507 | 1197 | fernando | callColorChanged(backColor); |
508 | 213 | fernando | } |
509 | |||
510 | 956 | vcaballero | /**
|
511 | 1036 | vcaballero | * Devuelve el color de fondo.
|
512 | 956 | vcaballero | *
|
513 | 1036 | vcaballero | * @return Color de fondo.
|
514 | 956 | vcaballero | */
|
515 | 213 | fernando | public Color getBackColor() { |
516 | return backColor;
|
||
517 | } |
||
518 | 956 | vcaballero | |
519 | 213 | fernando | /**
|
520 | 4910 | jaume | * Returns the extent currently covered by the view.
|
521 | 956 | vcaballero | *
|
522 | 213 | fernando | * @return Returns the adjustedExtent.
|
523 | */
|
||
524 | public Rectangle2D getAdjustedExtent() { |
||
525 | 3683 | caballero | if (cliprect!=null){ |
526 | return adjustedExtent.createIntersection(cliprect);
|
||
527 | } |
||
528 | 213 | fernando | return adjustedExtent;
|
529 | } |
||
530 | 956 | vcaballero | |
531 | 213 | fernando | /**
|
532 | 1036 | vcaballero | * Devuelve la unidad de medida.
|
533 | 956 | vcaballero | *
|
534 | 213 | fernando | * @return Returns the distanceUnits.
|
535 | */
|
||
536 | public int getDistanceUnits() { |
||
537 | return distanceUnits;
|
||
538 | } |
||
539 | 956 | vcaballero | |
540 | 213 | fernando | /**
|
541 | 1036 | vcaballero | * Inserta la unidad de medida.
|
542 | 956 | vcaballero | *
|
543 | 213 | fernando | * @param distanceUnits The distanceUnits to set.
|
544 | */
|
||
545 | public void setDistanceUnits(int distanceUnits) { |
||
546 | this.distanceUnits = distanceUnits;
|
||
547 | } |
||
548 | 956 | vcaballero | |
549 | 213 | fernando | /**
|
550 | 1036 | vcaballero | * Devuelve la unidad de medida del mapa.
|
551 | 956 | vcaballero | *
|
552 | 213 | fernando | * @return Returns the mapUnits.
|
553 | */
|
||
554 | public int getMapUnits() { |
||
555 | return mapUnits;
|
||
556 | } |
||
557 | 956 | vcaballero | |
558 | 213 | fernando | /**
|
559 | 1036 | vcaballero | * Inserta la unidad de medida del mapa.
|
560 | 956 | vcaballero | *
|
561 | 213 | fernando | * @param mapUnits The mapUnits to set.
|
562 | */
|
||
563 | public void setMapUnits(int mapUnits) { |
||
564 | this.mapUnits = mapUnits;
|
||
565 | 956 | vcaballero | } |
566 | |||
567 | /**
|
||
568 | 1036 | vcaballero | * Devuelve la anchura de la imagen.
|
569 | 956 | vcaballero | *
|
570 | 1036 | vcaballero | * @return anchura en pixels de la imagen.
|
571 | 956 | vcaballero | */
|
572 | public int getImageWidth() { |
||
573 | 214 | fernando | return imageSize.width;
|
574 | 213 | fernando | } |
575 | 956 | vcaballero | |
576 | /**
|
||
577 | 1036 | vcaballero | * Devuelve la altura de la imagen.
|
578 | 956 | vcaballero | *
|
579 | 1036 | vcaballero | * @return altura de la imagen.
|
580 | 956 | vcaballero | */
|
581 | public int getImageHeight() { |
||
582 | 214 | fernando | return imageSize.height;
|
583 | } |
||
584 | 956 | vcaballero | |
585 | /**
|
||
586 | 1036 | vcaballero | * Devuelve la distancia real de un pixel.
|
587 | 956 | vcaballero | *
|
588 | 1036 | vcaballero | * @return Distancia real de un pixel.
|
589 | 956 | vcaballero | */
|
590 | 237 | fjp | public double getDist1pixel() { |
591 | return dist1pixel;
|
||
592 | } |
||
593 | 956 | vcaballero | |
594 | /**
|
||
595 | 1036 | vcaballero | * Inserta la distancia real de un pixel.
|
596 | 956 | vcaballero | *
|
597 | 1036 | vcaballero | * @param dist1pixel Distancia real de un pixel.
|
598 | 956 | vcaballero | */
|
599 | 237 | fjp | public void setDist1pixel(double dist1pixel) { |
600 | this.dist1pixel = dist1pixel;
|
||
601 | } |
||
602 | 956 | vcaballero | |
603 | /**
|
||
604 | 1036 | vcaballero | * Devuelve la distancia real de tres pixel.
|
605 | 956 | vcaballero | *
|
606 | 1036 | vcaballero | * @return Distancia real de tres pixel.
|
607 | 956 | vcaballero | */
|
608 | 237 | fjp | public double getDist3pixel() { |
609 | return dist3pixel;
|
||
610 | } |
||
611 | 956 | vcaballero | |
612 | /**
|
||
613 | 1036 | vcaballero | * Inserta la distancia real de tres pixels.
|
614 | 956 | vcaballero | *
|
615 | 1036 | vcaballero | * @param dist3pixel Distancia real de tres pixels.
|
616 | 956 | vcaballero | */
|
617 | 237 | fjp | public void setDist3pixel(double dist3pixel) { |
618 | this.dist3pixel = dist3pixel;
|
||
619 | } |
||
620 | 956 | vcaballero | |
621 | 277 | fernando | /**
|
622 | 1036 | vcaballero | * Devuelve los Extents anteriores almacenados.
|
623 | 956 | vcaballero | *
|
624 | 277 | fernando | * @return Returns the extents.
|
625 | */
|
||
626 | public ExtentHistory getExtents() {
|
||
627 | return extents;
|
||
628 | } |
||
629 | |||
630 | /**
|
||
631 | 1036 | vcaballero | * Devuelve la proyecci?n.
|
632 | 956 | vcaballero | *
|
633 | 346 | fernando | * @return Returns the proj.
|
634 | */
|
||
635 | public IProjection getProjection() {
|
||
636 | return proj;
|
||
637 | } |
||
638 | 956 | vcaballero | |
639 | 346 | fernando | /**
|
640 | 1036 | vcaballero | * Inserta la proyecci?n.
|
641 | 956 | vcaballero | *
|
642 | 346 | fernando | * @param proj The proj to set.
|
643 | */
|
||
644 | public void setProjection(IProjection proj) { |
||
645 | this.proj = proj;
|
||
646 | } |
||
647 | 3884 | caballero | |
648 | 1152 | fjp | /**
|
649 | * M?todo que solo lo utilizamos a la hora de imprimir. NO lanza
|
||
650 | * un calculateAffineTransform, ni recalcula el adjustedExtent.
|
||
651 | * TODO: Para evitar este m?todo, habr?a que redefinir el interfaz
|
||
652 | 3884 | caballero | * RasterAdapter, y que recibiera un ViewPortData.
|
653 | 1152 | fjp | * @param at
|
654 | */
|
||
655 | 3884 | caballero | public void setAffineTransform(AffineTransform at) |
656 | 1152 | fjp | { |
657 | this.trans = at;
|
||
658 | } |
||
659 | 956 | vcaballero | |
660 | /**
|
||
661 | 1036 | vcaballero | * Devuelve el XMLEntity.
|
662 | 956 | vcaballero | *
|
663 | 1036 | vcaballero | * @return XMLEntity.
|
664 | 956 | vcaballero | */
|
665 | public XMLEntity getXMLEntity() {
|
||
666 | XMLEntity xml = new XMLEntity();
|
||
667 | 1094 | vcaballero | xml.putProperty("className",this.getClass().getName()); |
668 | 956 | vcaballero | |
669 | if (adjustedExtent != null) { |
||
670 | xml.putProperty("adjustedExtentX", adjustedExtent.getX());
|
||
671 | xml.putProperty("adjustedExtentY", adjustedExtent.getY());
|
||
672 | xml.putProperty("adjustedExtentW", adjustedExtent.getWidth());
|
||
673 | xml.putProperty("adjustedExtentH", adjustedExtent.getHeight());
|
||
674 | 741 | vcaballero | } |
675 | 956 | vcaballero | |
676 | 1282 | fjp | if (backColor != null) |
677 | xml.putProperty("backColor", StringUtilities.color2String(backColor));
|
||
678 | 956 | vcaballero | |
679 | if (clip != null) { |
||
680 | xml.putProperty("clipX", clip.getX());
|
||
681 | xml.putProperty("clipY", clip.getY());
|
||
682 | xml.putProperty("clipW", clip.getWidth());
|
||
683 | xml.putProperty("clipH", clip.getHeight());
|
||
684 | 435 | vcaballero | } |
685 | 956 | vcaballero | |
686 | xml.putProperty("dist1pixel", dist1pixel);
|
||
687 | xml.putProperty("dist3pixel", dist3pixel);
|
||
688 | xml.putProperty("distanceUnits", distanceUnits);
|
||
689 | |||
690 | if (extent != null) { |
||
691 | xml.putProperty("extentX", extent.getX());
|
||
692 | xml.putProperty("extentY", extent.getY());
|
||
693 | xml.putProperty("extentW", extent.getWidth());
|
||
694 | xml.putProperty("extentH", extent.getHeight());
|
||
695 | 741 | vcaballero | } |
696 | 956 | vcaballero | |
697 | 449 | vcaballero | xml.addChild(extents.getXMLEntity()); |
698 | 956 | vcaballero | xml.putProperty("mapUnits", mapUnits);
|
699 | xml.putProperty("offsetX", offset.getX());
|
||
700 | xml.putProperty("offsetY", offset.getY());
|
||
701 | |||
702 | if (proj != null) { |
||
703 | xml.putProperty("proj", proj.getAbrev());
|
||
704 | 442 | vcaballero | } |
705 | 956 | vcaballero | |
706 | xml.putProperty("scale", scale);
|
||
707 | |||
708 | 435 | vcaballero | return xml;
|
709 | } |
||
710 | 956 | vcaballero | |
711 | /**
|
||
712 | 1036 | vcaballero | * Crea un nuevo ViewPort a partir del XMLEntity.
|
713 | 956 | vcaballero | *
|
714 | 1036 | vcaballero | * @param xml XMLEntity.
|
715 | 956 | vcaballero | *
|
716 | 1036 | vcaballero | * @return Nuevo ViewPort.
|
717 | 956 | vcaballero | */
|
718 | 2183 | fernando | public static ViewPort createFromXML03(XMLEntity xml) { |
719 | ViewPort vp = new ViewPort(null); |
||
720 | |||
721 | if (xml.contains("adjustedExtentX")) { |
||
722 | vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty( |
||
723 | "adjustedExtentX"),
|
||
724 | xml.getDoubleProperty("adjustedExtentY"),
|
||
725 | xml.getDoubleProperty("adjustedExtentW"),
|
||
726 | xml.getDoubleProperty("adjustedExtentH"));
|
||
727 | } |
||
728 | |||
729 | if (xml.contains("backColor")) { |
||
730 | vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty( |
||
731 | "backColor")));
|
||
732 | } |
||
733 | |||
734 | if (xml.contains("clipX")) { |
||
735 | vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"), |
||
736 | xml.getDoubleProperty("clipY"),
|
||
737 | xml.getDoubleProperty("clipW"),
|
||
738 | xml.getDoubleProperty("clipH"));
|
||
739 | } |
||
740 | |||
741 | vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
|
||
742 | vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
|
||
743 | vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
|
||
744 | vp.extents = ExtentHistory.createFromXML03(xml.getChild(0));
|
||
745 | |||
746 | if (xml.contains("extentX")) { |
||
747 | vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"), |
||
748 | xml.getDoubleProperty("extentY"),
|
||
749 | xml.getDoubleProperty("extentW"),
|
||
750 | xml.getDoubleProperty("extentH")));
|
||
751 | |||
752 | //Calcula la transformaci?n af?n
|
||
753 | vp.calculateAffineTransform(); |
||
754 | |||
755 | // Lanzamos los eventos de extent cambiado
|
||
756 | // vp.callExtentListeners(vp.adjustedExtent);
|
||
757 | } |
||
758 | |||
759 | vp.setMapUnits(xml.getIntProperty("mapUnits"));
|
||
760 | vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"), |
||
761 | xml.getDoubleProperty("offsetY")));
|
||
762 | |||
763 | if (xml.contains("proj")) { |
||
764 | vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
|
||
765 | } |
||
766 | |||
767 | //vp.setScale(xml.getDoubleProperty("scale"));
|
||
768 | vp.setScale(); |
||
769 | return vp;
|
||
770 | } |
||
771 | |||
772 | /**
|
||
773 | * Crea un nuevo ViewPort a partir del XMLEntity.
|
||
774 | *
|
||
775 | * @param xml XMLEntity.
|
||
776 | *
|
||
777 | * @return Nuevo ViewPort.
|
||
778 | */
|
||
779 | 956 | vcaballero | public static ViewPort createFromXML(XMLEntity xml) { |
780 | ViewPort vp = new ViewPort(null); |
||
781 | |||
782 | if (xml.contains("adjustedExtentX")) { |
||
783 | vp.adjustedExtent = new Rectangle2D.Double(xml.getDoubleProperty( |
||
784 | "adjustedExtentX"),
|
||
785 | xml.getDoubleProperty("adjustedExtentY"),
|
||
786 | xml.getDoubleProperty("adjustedExtentW"),
|
||
787 | xml.getDoubleProperty("adjustedExtentH"));
|
||
788 | 741 | vcaballero | } |
789 | 956 | vcaballero | |
790 | if (xml.contains("backColor")) { |
||
791 | vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty( |
||
792 | "backColor")));
|
||
793 | 435 | vcaballero | } |
794 | 956 | vcaballero | |
795 | if (xml.contains("clipX")) { |
||
796 | vp.clip = new Rectangle2D.Double(xml.getDoubleProperty("clipX"), |
||
797 | xml.getDoubleProperty("clipY"),
|
||
798 | xml.getDoubleProperty("clipW"),
|
||
799 | xml.getDoubleProperty("clipH"));
|
||
800 | 741 | vcaballero | } |
801 | 956 | vcaballero | |
802 | 442 | vcaballero | vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
|
803 | vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
|
||
804 | vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
|
||
805 | 956 | vcaballero | vp.extents = ExtentHistory.createFromXML(xml.getChild(0));
|
806 | 918 | fernando | |
807 | 956 | vcaballero | if (xml.contains("extentX")) { |
808 | vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"), |
||
809 | xml.getDoubleProperty("extentY"),
|
||
810 | xml.getDoubleProperty("extentW"),
|
||
811 | xml.getDoubleProperty("extentH")));
|
||
812 | |||
813 | //Calcula la transformaci?n af?n
|
||
814 | vp.calculateAffineTransform(); |
||
815 | |||
816 | // Lanzamos los eventos de extent cambiado
|
||
817 | 1040 | fjp | // vp.callExtentListeners(vp.adjustedExtent);
|
818 | 741 | vcaballero | } |
819 | 956 | vcaballero | |
820 | 442 | vcaballero | vp.setMapUnits(xml.getIntProperty("mapUnits"));
|
821 | 956 | vcaballero | vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"), |
822 | xml.getDoubleProperty("offsetY")));
|
||
823 | |||
824 | if (xml.contains("proj")) { |
||
825 | vp.proj = ProjectionPool.get(xml.getStringProperty("proj"));
|
||
826 | 442 | vcaballero | } |
827 | 956 | vcaballero | |
828 | 1135 | vcaballero | //vp.setScale(xml.getDoubleProperty("scale"));
|
829 | vp.setScale(); |
||
830 | 435 | vcaballero | return vp;
|
831 | } |
||
832 | 956 | vcaballero | |
833 | /**
|
||
834 | 1036 | vcaballero | * Clona el ViewPort.
|
835 | 956 | vcaballero | *
|
836 | 1036 | vcaballero | * @return ViewPort clonado.
|
837 | 956 | vcaballero | */
|
838 | public ViewPort cloneViewPort() {
|
||
839 | return createFromXML(getXMLEntity());
|
||
840 | 798 | vcaballero | } |
841 | 956 | vcaballero | |
842 | /**
|
||
843 | 1036 | vcaballero | * Devuelve el String con datos del ViewPort.
|
844 | 956 | vcaballero | *
|
845 | 1036 | vcaballero | * @return Cadena con datos del ViewPort.
|
846 | 956 | vcaballero | */
|
847 | public String toString() { |
||
848 | 875 | fjp | String str;
|
849 | 956 | vcaballero | str = "Datos del viewPort:\nExtent=" + extent + "\nadjustedExtent=" + |
850 | adjustedExtent + "\nimageSize=" + imageSize + "\nescale=" + scale + |
||
851 | 875 | fjp | "\ntrans=" + trans;
|
852 | 956 | vcaballero | |
853 | 875 | fjp | return str;
|
854 | } |
||
855 | 3683 | caballero | |
856 | public void setClipRect(Rectangle2D rectView) { |
||
857 | cliprect=rectView; |
||
858 | 3884 | caballero | |
859 | 3683 | caballero | } |
860 | |||
861 | 4213 | caballero | public Rectangle2D fromMapRectangle(Rectangle2D r) { |
862 | double w=fromMapDistance((int)r.getWidth()); |
||
863 | double h=fromMapDistance((int)r.getHeight()); |
||
864 | Point2D p1=fromMapPoint((int)r.getX(),(int)r.getY()); |
||
865 | return new Rectangle2D.Double(p1.getX(),p1.getY(),w,h); |
||
866 | } |
||
867 | 3884 | caballero | |
868 | 4213 | caballero | |
869 | 213 | fernando | } |