svn-gvsig-desktop / tags / gvSIGv0_6_1RELEASE / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 5222
History | View | Annotate | Download (6.05 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 |
* 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 |
package com.iver.cit.gvsig.fmap.tools; |
42 |
|
43 |
import java.awt.Cursor; |
44 |
import java.awt.Image; |
45 |
import java.awt.Point; |
46 |
import java.awt.Toolkit; |
47 |
import java.awt.geom.Point2D; |
48 |
|
49 |
import javax.swing.ImageIcon; |
50 |
|
51 |
import com.iver.cit.gvsig.fmap.MapControl; |
52 |
import com.iver.cit.gvsig.fmap.ViewPort; |
53 |
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent; |
54 |
import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener; |
55 |
|
56 |
|
57 |
/**
|
58 |
* Implementaci?n de la interfaz MeasureListener como herramienta para medir el
|
59 |
* ?rea.
|
60 |
*
|
61 |
* @author Vicente Caballero Navarro
|
62 |
*/
|
63 |
public class AreaListenerImpl implements PolylineListener { |
64 |
private final Image iarea = new ImageIcon(MapControl.class.getResource( |
65 |
"images/AreaCursor.gif")).getImage();
|
66 |
private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea, |
67 |
new Point(16, 16), ""); |
68 |
protected MapControl mapCtrl;
|
69 |
protected MeasureEvent event;
|
70 |
|
71 |
/**
|
72 |
* Crea un nuevo AreaListenerImpl.
|
73 |
*
|
74 |
* @param mc MapControl.
|
75 |
*/
|
76 |
public AreaListenerImpl(MapControl mc) {
|
77 |
this.mapCtrl = mc;
|
78 |
} |
79 |
|
80 |
/**
|
81 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
82 |
*/
|
83 |
public void points(MeasureEvent event) { |
84 |
this.event = event;
|
85 |
|
86 |
double dist = 0; |
87 |
double distAll = 0; |
88 |
|
89 |
ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
90 |
|
91 |
for (int i = 0; i < (event.getXs().length - 1); i++) { |
92 |
dist = 0;
|
93 |
|
94 |
Point p = new Point(event.getXs()[i].intValue(), |
95 |
event.getXs()[i].intValue()); |
96 |
Point p2 = new Point(event.getXs()[i + 1].intValue(), |
97 |
event.getXs()[i + 1].intValue());
|
98 |
|
99 |
///dist = vp.toMapDistance((int) p.distance(p2));
|
100 |
dist = vp.distanceWorld(p, p2); |
101 |
distAll += dist; |
102 |
} |
103 |
|
104 |
System.out.println("Per?metro = " + distAll + " ?rea = " + |
105 |
(returnArea(vp.toMapPoint( |
106 |
new Point2D.Double( |
107 |
event.getXs()[event.getXs().length - 2].doubleValue(),
|
108 |
event.getYs()[event.getYs().length - 2].doubleValue())))));
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* <p>
|
113 |
* Computes the area using the trapezoid method. This is, we decompose a polygon in a set of
|
114 |
* trapezoids defined each two points of the polygon starting and ending in the first user's
|
115 |
* entered point. For each trapezoid by these each two points and the points over the y-axis (when y equals 0)
|
116 |
* that have the same x value than the points defined by the user and then we obtain
|
117 |
* the area of this trapezoid with this formula.
|
118 |
* </p>
|
119 |
* <p>
|
120 |
* The four points that define the trapezoid we are considering are
|
121 |
* p1 = (x1, y1) -> the first user point
|
122 |
* p2 = (x2, y2) -> the second user point
|
123 |
* p3 = (x3, y3) -> the image over the y-axis of p1
|
124 |
* p4 = (x4, y4) -> the image over the y-axis
|
125 |
* </p>
|
126 |
* <pre>
|
127 |
* (y1 - y2) * (x2 - x1)
|
128 |
* A = | ----------------------------| + |((x2 - x1) * (y2 - y1)| (of course, simplify this)
|
129 |
* 2
|
130 |
* <pre>
|
131 |
*
|
132 |
* Calculated the area of every trapezoid generated at the descomposition, only rests
|
133 |
* to acumulate it to the total amout when (x2 - x1)>0 and substract it when (x2 - x1)< 0
|
134 |
*
|
135 |
* @param aux, the last point.
|
136 |
*
|
137 |
* @return area.
|
138 |
*/
|
139 |
protected double returnArea(Point2D aux) { |
140 |
|
141 |
double elArea;
|
142 |
int pos;
|
143 |
Point2D p = new Point2D.Double(); |
144 |
Point2D.Double pAnt = new Point2D.Double(); |
145 |
Double[] x = event.getXs(); |
146 |
Double[] y = event.getYs(); |
147 |
|
148 |
System.out.println("[equis]"); |
149 |
for (int j = 0; j < x.length; j++) { |
150 |
System.out.print(x[j].doubleValue()+", "); |
151 |
} |
152 |
// double baseMayor, baseMenor, altura; // De cada trapezoide
|
153 |
// El area de cada pareja de puntos ser?a (x2-x1) * baseMenor + (baseMayor-baseMenor)*(x2-x1)
|
154 |
// Simplificando la ecuaci?n queda: (x2-x1)*((baseMayor+baseMenor)/2). Es decir, que da igual
|
155 |
// quien es la base mayor y quien la manor.
|
156 |
// si la coordenada X del punto i+1 es mayor que la del punto i => Area negativa
|
157 |
// si no => Area positiva
|
158 |
if (x.length < 2) { |
159 |
return 0.0; |
160 |
} |
161 |
|
162 |
elArea = 0.0;
|
163 |
|
164 |
for (pos = 0; pos < x.length; pos++) { |
165 |
p = new Point2D.Double(x[pos].doubleValue(), y[pos].doubleValue()); |
166 |
|
167 |
if (pos == 0) { |
168 |
pAnt.x = aux.getX(); |
169 |
pAnt.y = aux.getY(); |
170 |
} |
171 |
|
172 |
elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
173 |
pAnt.setLocation(p); |
174 |
} |
175 |
|
176 |
elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
177 |
elArea = Math.abs(elArea / 2.0); |
178 |
|
179 |
return elArea;
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
184 |
*/
|
185 |
public Cursor getCursor() { |
186 |
return cur;
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
191 |
*/
|
192 |
public void pointFixed(MeasureEvent event) { |
193 |
} |
194 |
|
195 |
/**
|
196 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
197 |
*/
|
198 |
public void polylineFinished(MeasureEvent event) { |
199 |
} |
200 |
|
201 |
/**
|
202 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
203 |
*/
|
204 |
public boolean cancelDrawing() { |
205 |
return false; |
206 |
} |
207 |
} |