svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / ViewPort.java @ 850
History | View | Annotate | Download (11.5 KB)
1 |
/* Generated by Together */
|
---|---|
2 |
|
3 |
package com.iver.cit.gvsig.fmap; |
4 |
|
5 |
import java.awt.Color; |
6 |
import java.awt.Dimension; |
7 |
import java.awt.Point; |
8 |
import java.awt.geom.AffineTransform; |
9 |
import java.awt.geom.NoninvertibleTransformException; |
10 |
import java.awt.geom.Point2D; |
11 |
import java.awt.geom.Rectangle2D; |
12 |
import java.util.ArrayList; |
13 |
|
14 |
import org.cresques.cts.IProjection; |
15 |
import org.cresques.cts.ProjectionPool; |
16 |
|
17 |
import com.iver.utiles.StringUtilities; |
18 |
import com.iver.utiles.XMLEntity; |
19 |
|
20 |
public class ViewPort { |
21 |
public static int KILOMETROS = 1; |
22 |
public static int METROS = 2; |
23 |
public static int MILLAS = 3; |
24 |
public static int YARDAS = 4; |
25 |
public static int PIES = 5; |
26 |
public static int PULGADAS = 6; |
27 |
|
28 |
/**
|
29 |
* Resoluci?n (Puntos por pulgada) de la vista actual.
|
30 |
* Se necesita para los c?lculos de escala geogr?fica.
|
31 |
*/
|
32 |
private static int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); |
33 |
|
34 |
private Rectangle2D extent; |
35 |
private Rectangle2D adjustedExtent; |
36 |
private ExtentHistory extents = new ExtentHistory(); |
37 |
private Dimension imageSize; |
38 |
private AffineTransform trans = new AffineTransform(); |
39 |
private int distanceUnits = METROS; |
40 |
private int mapUnits = METROS; |
41 |
|
42 |
private ArrayList extentListeners=new ArrayList(); |
43 |
private Point2D offset = new Point2D.Double(0, 0); |
44 |
private Rectangle2D clip; |
45 |
private Color backColor = Color.WHITE; |
46 |
|
47 |
private IProjection proj;
|
48 |
|
49 |
private double dist1pixel; |
50 |
private double dist3pixel; |
51 |
|
52 |
|
53 |
private double scale; |
54 |
|
55 |
public boolean addExtentListener(ExtentListener arg0) { |
56 |
return extentListeners.add(arg0);
|
57 |
} |
58 |
|
59 |
public boolean removeExtentListener(ExtentListener arg0) { |
60 |
return extentListeners.remove(arg0);
|
61 |
} |
62 |
|
63 |
public int fromMapDistance(double d) { |
64 |
Point2D.Double pWorld = new Point2D.Double(1, 1); |
65 |
Point2D.Double pScreen = new Point2D.Double(); |
66 |
|
67 |
double nuevoX;
|
68 |
double nuevoY;
|
69 |
double cX;
|
70 |
double cY;
|
71 |
|
72 |
try {
|
73 |
trans.deltaTransform(pWorld, pScreen); |
74 |
} catch (Exception e) { |
75 |
System.err.print(e.getMessage());
|
76 |
} |
77 |
|
78 |
return (int) (d * pScreen.x); |
79 |
} |
80 |
|
81 |
public Point2D fromMapPoint(double x, double y) { |
82 |
Point2D.Double pWorld = new Point2D.Double(x, y); |
83 |
Point2D.Double pScreen = new Point2D.Double(); |
84 |
|
85 |
double nuevoX;
|
86 |
double nuevoY;
|
87 |
double cX;
|
88 |
double cY;
|
89 |
|
90 |
try {
|
91 |
trans.transform(pWorld, pScreen); |
92 |
} catch (Exception e) { |
93 |
System.err.print(e.getMessage());
|
94 |
} |
95 |
|
96 |
return pScreen;
|
97 |
} |
98 |
|
99 |
public Point2D toMapPoint(int x, int y) { |
100 |
Point pScreen = new Point(x, y); |
101 |
return toMapPoint(pScreen);
|
102 |
} |
103 |
|
104 |
public double toMapDistance(int d) { |
105 |
double dist = d / trans.getScaleX();
|
106 |
return dist;
|
107 |
} |
108 |
|
109 |
public Point2D toMapPoint(Point2D pScreen) { |
110 |
Point2D.Double pWorld = new Point2D.Double(); |
111 |
AffineTransform at;
|
112 |
|
113 |
try {
|
114 |
at = trans.createInverse(); |
115 |
at.transform(pScreen, pWorld); |
116 |
} catch (NoninvertibleTransformException e) { |
117 |
throw new RuntimeException(e); |
118 |
} |
119 |
|
120 |
return pWorld;
|
121 |
} |
122 |
|
123 |
public void setPreviousExtent() { |
124 |
} |
125 |
|
126 |
public Rectangle2D getExtent() { |
127 |
return extent;
|
128 |
} |
129 |
|
130 |
public void setExtent(Rectangle2D r) { |
131 |
if (extent!=null){ |
132 |
extents.put(extent); |
133 |
} |
134 |
//Esto comprueba que el extent no es de anchura o altura = "0"
|
135 |
//y si es as? lo redimensiona.
|
136 |
if (r.getWidth()==0 || r.getHeight()==0){ |
137 |
extent=new Rectangle2D.Double(r.getMinX()-0.1,r.getMinY()-0.1,r.getWidth()+0.2,r.getHeight()+0.2); |
138 |
}else{
|
139 |
extent = r; |
140 |
} |
141 |
//TODO calcular la escala sin usar setScale
|
142 |
|
143 |
//Calcula la transformaci?n af?n
|
144 |
calculateAffineTransform(); |
145 |
|
146 |
// Lanzamos los eventos de extent cambiado
|
147 |
callExtentListeners(adjustedExtent); |
148 |
} |
149 |
|
150 |
public void setScale(double scale) { |
151 |
this.scale = scale;
|
152 |
//TODO calcular el extent sin usar setExtent
|
153 |
//Calcula la transformaci?n af?n
|
154 |
calculateAffineTransform(); |
155 |
|
156 |
// Lanzamos los eventos de extent cambiado
|
157 |
callExtentListeners(adjustedExtent); |
158 |
} |
159 |
|
160 |
/**
|
161 |
* Devuelve la escala. Debe estar siempre actualizada y
|
162 |
* no calcularse nunca aqu? pues se utiliza en el dibujado
|
163 |
* para cada geometr?a
|
164 |
* @return
|
165 |
*/
|
166 |
public double getScale() { |
167 |
return proj.getScale(extent.getMinX(), extent.getMaxX(), imageSize.getWidth(), dpi);
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* @return
|
172 |
*/
|
173 |
public AffineTransform getAffineTransform() { |
174 |
return trans;
|
175 |
} |
176 |
/**
|
177 |
* @return Returns the imageSize.
|
178 |
*/
|
179 |
public Dimension getImageSize() { |
180 |
return imageSize;
|
181 |
} |
182 |
/**
|
183 |
* @param imageSize The imageSize to set.
|
184 |
*/
|
185 |
public void setImageSize(Dimension imageSize) { |
186 |
this.imageSize = imageSize;
|
187 |
calculateAffineTransform(); |
188 |
} |
189 |
|
190 |
private void callExtentListeners(Rectangle2D newRect) { |
191 |
ExtentEvent ev = new ExtentEvent(newRect);
|
192 |
for (int i = 0; i < extentListeners.size(); i++) { |
193 |
ExtentListener listener = (ExtentListener) extentListeners.get(i); |
194 |
listener.extentChanged(ev); |
195 |
} |
196 |
} |
197 |
|
198 |
|
199 |
/**
|
200 |
*
|
201 |
*/
|
202 |
private void calculateAffineTransform() { |
203 |
if ((imageSize == null) || (extent == null)) return; |
204 |
|
205 |
AffineTransform escalado = new AffineTransform(); |
206 |
AffineTransform translacion = new AffineTransform(); |
207 |
|
208 |
double escalaX;
|
209 |
double escalaY;
|
210 |
|
211 |
escalaX = imageSize.getWidth() / extent.getWidth(); |
212 |
escalaY = imageSize.getHeight() / extent.getHeight(); |
213 |
|
214 |
adjustedExtent = new Rectangle2D.Double(); |
215 |
if (escalaX < escalaY) {
|
216 |
scale = escalaX; |
217 |
adjustedExtent.setRect(extent.getX(), extent.getY(), extent.getWidth(), imageSize.getHeight() / scale); |
218 |
} else {
|
219 |
scale = escalaY; |
220 |
adjustedExtent.setRect(extent.getX(), extent.getY(), imageSize.getWidth() / scale, extent.getHeight()); |
221 |
} |
222 |
|
223 |
translacion.setToTranslation(-adjustedExtent.getX(), -adjustedExtent.getY() - |
224 |
adjustedExtent.getHeight()); |
225 |
escalado.setToScale(scale, -scale); |
226 |
|
227 |
AffineTransform offsetTrans = new AffineTransform(); |
228 |
offsetTrans.setToTranslation(offset.getX(), offset.getY()); |
229 |
|
230 |
trans.setToIdentity(); |
231 |
trans.concatenate(offsetTrans); |
232 |
trans.concatenate(escalado); |
233 |
|
234 |
trans.concatenate(translacion); |
235 |
|
236 |
extent = adjustedExtent; |
237 |
|
238 |
// Calculamos las distancias de 1 pixel y 3 pixel con esa transformaci?n
|
239 |
// de coordenadas, de forma que est?n precalculadas para cuando las necesitemos
|
240 |
AffineTransform at;
|
241 |
try {
|
242 |
at = trans.createInverse(); |
243 |
java.awt.Point pPixel = new java.awt.Point(1, 1); |
244 |
Point2D.Float pProv = new Point2D.Float(); |
245 |
at.deltaTransform(pPixel, pProv); |
246 |
|
247 |
dist1pixel = pProv.x; |
248 |
dist3pixel = 3 * pProv.x;
|
249 |
|
250 |
} catch (NoninvertibleTransformException e) { |
251 |
e.printStackTrace(); |
252 |
} |
253 |
|
254 |
} |
255 |
public void setOffset(Point2D p){ |
256 |
offset=p; |
257 |
} |
258 |
public void setBackColor(Color c) { |
259 |
backColor = c; |
260 |
} |
261 |
|
262 |
public Color getBackColor() { |
263 |
return backColor;
|
264 |
} |
265 |
/**
|
266 |
* @return Returns the adjustedExtent.
|
267 |
*/
|
268 |
public Rectangle2D getAdjustedExtent() { |
269 |
return adjustedExtent;
|
270 |
} |
271 |
/**
|
272 |
* @return Returns the distanceUnits.
|
273 |
*/
|
274 |
public int getDistanceUnits() { |
275 |
return distanceUnits;
|
276 |
} |
277 |
/**
|
278 |
* @param distanceUnits The distanceUnits to set.
|
279 |
*/
|
280 |
public void setDistanceUnits(int distanceUnits) { |
281 |
this.distanceUnits = distanceUnits;
|
282 |
} |
283 |
/**
|
284 |
* @return Returns the mapUnits.
|
285 |
*/
|
286 |
public int getMapUnits() { |
287 |
return mapUnits;
|
288 |
} |
289 |
/**
|
290 |
* @param mapUnits The mapUnits to set.
|
291 |
*/
|
292 |
public void setMapUnits(int mapUnits) { |
293 |
this.mapUnits = mapUnits;
|
294 |
} |
295 |
|
296 |
public int getImageWidth(){ |
297 |
return imageSize.width;
|
298 |
} |
299 |
|
300 |
public int getImageHeight(){ |
301 |
return imageSize.height;
|
302 |
} |
303 |
public double getDist1pixel() { |
304 |
return dist1pixel;
|
305 |
} |
306 |
public void setDist1pixel(double dist1pixel) { |
307 |
this.dist1pixel = dist1pixel;
|
308 |
} |
309 |
public double getDist3pixel() { |
310 |
return dist3pixel;
|
311 |
} |
312 |
public void setDist3pixel(double dist3pixel) { |
313 |
this.dist3pixel = dist3pixel;
|
314 |
} |
315 |
/**
|
316 |
* @return Returns the extents.
|
317 |
*/
|
318 |
public ExtentHistory getExtents() {
|
319 |
return extents;
|
320 |
} |
321 |
|
322 |
/**
|
323 |
*
|
324 |
*/
|
325 |
public void setExtentPrev() { |
326 |
extent = extents.removePrev(); |
327 |
//TODO calcular la escala sin usar setScale
|
328 |
//Calcula la transformaci?n af?n
|
329 |
calculateAffineTransform(); |
330 |
// Lanzamos los eventos de extent cambiado
|
331 |
callExtentListeners(adjustedExtent); |
332 |
} |
333 |
/**
|
334 |
* @return Returns the proj.
|
335 |
*/
|
336 |
public IProjection getProjection() {
|
337 |
return proj;
|
338 |
} |
339 |
/**
|
340 |
* @param proj The proj to set.
|
341 |
*/
|
342 |
public void setProjection(IProjection proj) { |
343 |
this.proj = proj;
|
344 |
} |
345 |
public XMLEntity getXMLEntity(){
|
346 |
XMLEntity xml=new XMLEntity();
|
347 |
if (adjustedExtent != null){ |
348 |
xml.putProperty("adjustedExtentX",adjustedExtent.getX());
|
349 |
xml.putProperty("adjustedExtentY",adjustedExtent.getY());
|
350 |
xml.putProperty("adjustedExtentW",adjustedExtent.getWidth());
|
351 |
xml.putProperty("adjustedExtentH",adjustedExtent.getHeight());
|
352 |
} |
353 |
xml.putProperty("backColor",StringUtilities.color2String(backColor));
|
354 |
if (clip!=null){ |
355 |
xml.putProperty("clipX",clip.getX());
|
356 |
xml.putProperty("clipY",clip.getY());
|
357 |
xml.putProperty("clipW",clip.getWidth());
|
358 |
xml.putProperty("clipH",clip.getHeight());
|
359 |
} |
360 |
xml.putProperty("dist1pixel",dist1pixel);
|
361 |
xml.putProperty("dist3pixel",dist3pixel);
|
362 |
xml.putProperty("distanceUnits",distanceUnits);
|
363 |
if (extent!=null){ |
364 |
xml.putProperty("extentX",extent.getX());
|
365 |
xml.putProperty("extentY",extent.getY());
|
366 |
xml.putProperty("extentW",extent.getWidth());
|
367 |
xml.putProperty("extentH",extent.getHeight());
|
368 |
} |
369 |
xml.addChild(extents.getXMLEntity()); |
370 |
xml.putProperty("mapUnits",mapUnits);
|
371 |
xml.putProperty("offsetX",offset.getX());
|
372 |
xml.putProperty("offsetY",offset.getY());
|
373 |
if (proj!=null){ |
374 |
xml.putProperty("proj",proj.getAbrev());
|
375 |
} |
376 |
xml.putProperty("scale",scale);
|
377 |
|
378 |
return xml;
|
379 |
} |
380 |
public static ViewPort createFromXML(XMLEntity xml){ |
381 |
ViewPort vp=new ViewPort();
|
382 |
if (xml.contains("adjustedExtentX")){ |
383 |
vp.adjustedExtent=new Rectangle2D.Double(xml.getDoubleProperty("adjustedExtentX"),xml.getDoubleProperty("adjustedExtentY"),xml.getDoubleProperty("adjustedExtentW"),xml.getDoubleProperty("adjustedExtentH")); |
384 |
} |
385 |
if (xml.contains("backColor")){ |
386 |
vp.setBackColor(StringUtilities.string2Color(xml.getStringProperty("backColor")));
|
387 |
} |
388 |
if (xml.contains("clipX")){ |
389 |
vp.clip=new Rectangle2D.Double(xml.getDoubleProperty("clipX"),xml.getDoubleProperty("clipY"),xml.getDoubleProperty("clipW"),xml.getDoubleProperty("clipH")); |
390 |
} |
391 |
vp.setDist1pixel(xml.getDoubleProperty("dist1pixel"));
|
392 |
vp.setDist3pixel(xml.getDoubleProperty("dist3pixel"));
|
393 |
vp.setDistanceUnits(xml.getIntProperty("distanceUnits"));
|
394 |
vp.extents=ExtentHistory.createFromXML(xml.getChild(0));
|
395 |
if (xml.contains("extentX")){ |
396 |
vp.setExtent(new Rectangle2D.Double(xml.getDoubleProperty("extentX"),xml.getDoubleProperty("extentY"),xml.getDoubleProperty("extentW"),xml.getDoubleProperty("extentH"))); |
397 |
// TODO calcular la escala sin usar setScale
|
398 |
//Calcula la transformaci?n af?n
|
399 |
vp.calculateAffineTransform(); |
400 |
// Lanzamos los eventos de extent cambiado
|
401 |
vp.callExtentListeners(vp.adjustedExtent); |
402 |
} |
403 |
|
404 |
vp.setMapUnits(xml.getIntProperty("mapUnits"));
|
405 |
vp.setOffset(new Point2D.Double(xml.getDoubleProperty("offsetX"),xml.getDoubleProperty("offsetY"))); |
406 |
if (xml.contains("proj")){ |
407 |
vp.proj=ProjectionPool.get(xml.getStringProperty("proj"));
|
408 |
} |
409 |
vp.setScale(xml.getDoubleProperty("scale"));
|
410 |
return vp;
|
411 |
} |
412 |
public ViewPort cloneViewPort(){
|
413 |
return createFromXML(getXMLEntity());
|
414 |
} |
415 |
} |