svn-gvsig-desktop / tags / v1_0_2_Build_903 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 10704
History | View | Annotate | Download (5.24 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 | 288 | vcaballero | package com.iver.cit.gvsig.fmap.tools; |
42 | |||
43 | 9590 | caballero | import com.iver.cit.gvsig.fmap.MapContext; |
44 | 1223 | fernando | import com.iver.cit.gvsig.fmap.MapControl; |
45 | 1050 | vcaballero | import com.iver.cit.gvsig.fmap.ViewPort; |
46 | import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent; |
||
47 | 1221 | fernando | import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener; |
48 | 10004 | caballero | import com.iver.cit.gvsig.fmap.tools.geo.Geo; |
49 | 1050 | vcaballero | |
50 | 291 | vcaballero | import java.awt.Cursor; |
51 | import java.awt.Image; |
||
52 | 288 | vcaballero | import java.awt.Point; |
53 | 291 | vcaballero | import java.awt.Toolkit; |
54 | 288 | vcaballero | import java.awt.geom.Point2D; |
55 | |||
56 | 291 | vcaballero | import javax.swing.ImageIcon; |
57 | 288 | vcaballero | |
58 | 291 | vcaballero | |
59 | 288 | vcaballero | /**
|
60 | 1050 | vcaballero | * Implementaci?n de la interfaz MeasureListener como herramienta para medir el
|
61 | * ?rea.
|
||
62 | 288 | vcaballero | *
|
63 | * @author Vicente Caballero Navarro
|
||
64 | */
|
||
65 | 1221 | fernando | public class AreaListenerImpl implements PolylineListener { |
66 | 1223 | fernando | private final Image iarea = new ImageIcon(MapControl.class.getResource( |
67 | 1050 | vcaballero | "images/AreaCursor.gif")).getImage();
|
68 | 291 | vcaballero | private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea, |
69 | 1050 | vcaballero | new Point(16, 16), ""); |
70 | 1223 | fernando | protected MapControl mapCtrl;
|
71 | 1050 | vcaballero | protected MeasureEvent event;
|
72 | 288 | vcaballero | |
73 | 1050 | vcaballero | /**
|
74 | * Crea un nuevo AreaListenerImpl.
|
||
75 | *
|
||
76 | * @param mc MapControl.
|
||
77 | */
|
||
78 | 1223 | fernando | public AreaListenerImpl(MapControl mc) {
|
79 | 1050 | vcaballero | this.mapCtrl = mc;
|
80 | } |
||
81 | 288 | vcaballero | |
82 | 1050 | vcaballero | /**
|
83 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
84 | 1050 | vcaballero | */
|
85 | public void points(MeasureEvent event) { |
||
86 | this.event = event;
|
||
87 | 288 | vcaballero | |
88 | 1050 | vcaballero | double dist = 0; |
89 | double distAll = 0; |
||
90 | 288 | vcaballero | |
91 | 1050 | vcaballero | ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
92 | 288 | vcaballero | |
93 | 1050 | vcaballero | for (int i = 0; i < (event.getXs().length - 1); i++) { |
94 | dist = 0;
|
||
95 | 288 | vcaballero | |
96 | 1050 | vcaballero | Point p = new Point(event.getXs()[i].intValue(), |
97 | event.getXs()[i].intValue()); |
||
98 | Point p2 = new Point(event.getXs()[i + 1].intValue(), |
||
99 | event.getXs()[i + 1].intValue());
|
||
100 | 288 | vcaballero | |
101 | 1050 | vcaballero | ///dist = vp.toMapDistance((int) p.distance(p2));
|
102 | dist = vp.distanceWorld(p, p2); |
||
103 | distAll += dist; |
||
104 | } |
||
105 | 288 | vcaballero | |
106 | 1050 | vcaballero | System.out.println("Per?metro = " + distAll + " ?rea = " + |
107 | (returnArea(vp.toMapPoint( |
||
108 | new Point2D.Double( |
||
109 | event.getXs()[event.getXs().length - 2].doubleValue(),
|
||
110 | event.getYs()[event.getYs().length - 2].doubleValue())))));
|
||
111 | } |
||
112 | 288 | vcaballero | |
113 | 10004 | caballero | protected double returnArea(Point2D point) { |
114 | Double[] xs=event.getXs(); |
||
115 | Double[] ys=event.getYs(); |
||
116 | if (mapCtrl.getProjection().isProjected()) {
|
||
117 | return returnCoordsArea(xs,ys,point);
|
||
118 | } |
||
119 | return returnGeoCArea(xs,ys,point);
|
||
120 | } |
||
121 | public double returnGeoCArea(Double[] xs,Double[] ys,Point2D point) { |
||
122 | double[] lat=new double[xs.length]; |
||
123 | double[] lon=new double[xs.length]; |
||
124 | for (int K= 0; K < xs.length; K++){ |
||
125 | lon[K]= xs[K].doubleValue()/Geo.Degree; |
||
126 | lat[K]= ys[K].doubleValue()/Geo.Degree; |
||
127 | } |
||
128 | 10378 | caballero | return (Geo.sphericalPolyArea(lat,lon,xs.length-1)*Geo.SqM); |
129 | 10004 | caballero | } |
130 | 1050 | vcaballero | /**
|
131 | * Calcula el ?rea.
|
||
132 | *
|
||
133 | * @param aux ?ltimo punto.
|
||
134 | *
|
||
135 | * @return ?rea.
|
||
136 | */
|
||
137 | 10004 | caballero | public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) { |
138 | Point2D aux=point;
|
||
139 | double elArea = 0.0; |
||
140 | 1050 | vcaballero | Point2D pPixel;
|
141 | Point2D p = new Point2D.Double(); |
||
142 | Point2D.Double pAnt = new Point2D.Double(); |
||
143 | ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
||
144 | 10004 | caballero | for (int pos = 0; pos < xs.length-1; pos++) { |
145 | pPixel = new Point2D.Double(xs[pos].doubleValue(), |
||
146 | ys[pos].doubleValue()); |
||
147 | 4226 | caballero | p = pPixel;//vp.toMapPoint(pPixel);
|
148 | if (pos == 0) { |
||
149 | 1050 | vcaballero | pAnt.x = aux.getX(); |
150 | pAnt.y = aux.getY(); |
||
151 | } |
||
152 | elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
||
153 | pAnt.setLocation(p); |
||
154 | } |
||
155 | 291 | vcaballero | |
156 | 1050 | vcaballero | elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
157 | elArea = Math.abs(elArea / 2.0); |
||
158 | 10004 | caballero | return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2))); |
159 | 1050 | vcaballero | } |
160 | |||
161 | 291 | vcaballero | /**
|
162 | * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
||
163 | */
|
||
164 | public Cursor getCursor() { |
||
165 | return cur;
|
||
166 | } |
||
167 | 363 | fernando | |
168 | /**
|
||
169 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
170 | 363 | fernando | */
|
171 | public void pointFixed(MeasureEvent event) { |
||
172 | } |
||
173 | |||
174 | /**
|
||
175 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
176 | 363 | fernando | */
|
177 | public void polylineFinished(MeasureEvent event) { |
||
178 | } |
||
179 | |||
180 | /**
|
||
181 | * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
||
182 | */
|
||
183 | public boolean cancelDrawing() { |
||
184 | return false; |
||
185 | } |
||
186 | 291 | vcaballero | } |