svn-gvsig-desktop / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 20100
History | View | Annotate | Download (7.15 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 com.iver.cit.gvsig.fmap.MapContext; |
44 |
import com.iver.cit.gvsig.fmap.MapControl; |
45 |
import com.iver.cit.gvsig.fmap.ViewPort; |
46 |
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent; |
47 |
import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener; |
48 |
import com.iver.cit.gvsig.fmap.tools.geo.Geo; |
49 |
|
50 |
import java.awt.Cursor; |
51 |
import java.awt.Image; |
52 |
import java.awt.Point; |
53 |
import java.awt.Toolkit; |
54 |
import java.awt.geom.Point2D; |
55 |
|
56 |
import javax.swing.ImageIcon; |
57 |
|
58 |
|
59 |
/**
|
60 |
* <p>Listener for calculating the area of a polygon, defined in the associated {@link MapControl MapControl}
|
61 |
* object.</p>
|
62 |
*
|
63 |
* <p>If the view port of the associated <code>MapControl</code> isn't projected gets the area according the
|
64 |
* geographical coordinates.</p>
|
65 |
*
|
66 |
* @author Vicente Caballero Navarro
|
67 |
*/
|
68 |
public class AreaListenerImpl implements PolylineListener { |
69 |
/**
|
70 |
* The image to display when the cursor is active.
|
71 |
*/
|
72 |
private final Image iarea = new ImageIcon(MapControl.class.getResource( |
73 |
"images/AreaCursor.gif")).getImage();
|
74 |
|
75 |
/**
|
76 |
* The cursor used to work with this tool listener.
|
77 |
*
|
78 |
* @see #getCursor()
|
79 |
*/
|
80 |
private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea, |
81 |
new Point(16, 16), ""); |
82 |
|
83 |
/**
|
84 |
* Reference to the <code>MapControl</code> object that uses.
|
85 |
*/
|
86 |
protected MapControl mapCtrl;
|
87 |
|
88 |
/**
|
89 |
* Information about all vertexes and {@link GeneralPathX GeneralPathX}s of the polyline.
|
90 |
*/
|
91 |
protected MeasureEvent event;
|
92 |
|
93 |
/**
|
94 |
* <p>Creates a new listener for calculating the area of a polygon.</p>
|
95 |
*
|
96 |
* @param mc the <code>MapControl</code> where is calculated the area
|
97 |
*/
|
98 |
public AreaListenerImpl(MapControl mc) {
|
99 |
this.mapCtrl = mc;
|
100 |
} |
101 |
|
102 |
/*
|
103 |
* (non-Javadoc)
|
104 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
105 |
*/
|
106 |
public void points(MeasureEvent event) { |
107 |
this.event = event;
|
108 |
|
109 |
double dist = 0; |
110 |
double distAll = 0; |
111 |
|
112 |
ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
113 |
|
114 |
for (int i = 0; i < (event.getXs().length - 1); i++) { |
115 |
dist = 0;
|
116 |
|
117 |
Point p = new Point(event.getXs()[i].intValue(), |
118 |
event.getXs()[i].intValue()); |
119 |
Point p2 = new Point(event.getXs()[i + 1].intValue(), |
120 |
event.getXs()[i + 1].intValue());
|
121 |
|
122 |
///dist = vp.toMapDistance((int) p.distance(p2));
|
123 |
dist = vp.distanceWorld(p, p2); |
124 |
distAll += dist; |
125 |
} |
126 |
|
127 |
System.out.println("Per?metro = " + distAll + " ?rea = " + |
128 |
(returnArea(vp.toMapPoint( |
129 |
new Point2D.Double( |
130 |
event.getXs()[event.getXs().length - 2].doubleValue(),
|
131 |
event.getYs()[event.getYs().length - 2].doubleValue())))));
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* <p>Returns the area of the polygon, using {@link #returnCoordsArea(Double[], Double[], Point2D) returnCoordsArea}
|
136 |
* if the <code>ViewPort</code> of the associated <code>MapControl</code> is projected, or using
|
137 |
* {@link #returnGeoCArea(Double[], Double[], Point2D) returnGeoCArea} if isn't.</p>
|
138 |
*
|
139 |
* @param point unused parameter
|
140 |
*
|
141 |
* @return area from the vertexes stored at the measure event in real coordinates, or, if the <code>MapControl</code>
|
142 |
* isn't projected, in geographical coordinates
|
143 |
*
|
144 |
* @see #returnCoordsArea(Double[], Double[], Point2D)
|
145 |
* @see #returnGeoCArea(Double[], Double[], Point2D)
|
146 |
*/
|
147 |
protected double returnArea(Point2D point) { |
148 |
Double[] xs=event.getXs(); |
149 |
Double[] ys=event.getYs(); |
150 |
if (mapCtrl.getProjection().isProjected()) {
|
151 |
return returnCoordsArea(xs,ys,point);
|
152 |
} |
153 |
return returnGeoCArea(xs,ys,point);
|
154 |
} |
155 |
|
156 |
/**
|
157 |
* <p>Returns the area in geographical coordinates of the polygon, according the
|
158 |
* <a href="http://en.wikipedia.org/wiki/Haversine_formula">Haversine function</a>.</p>
|
159 |
*
|
160 |
* @see Geo#sphericalPolyArea(double[], double[], int)
|
161 |
*/
|
162 |
public double returnGeoCArea(Double[] xs,Double[] ys,Point2D point) { |
163 |
double[] lat=new double[xs.length]; |
164 |
double[] lon=new double[xs.length]; |
165 |
for (int K= 0; K < xs.length; K++){ |
166 |
lon[K]= xs[K].doubleValue()/Geo.Degree; |
167 |
lat[K]= ys[K].doubleValue()/Geo.Degree; |
168 |
} |
169 |
return (Geo.sphericalPolyArea(lat,lon,xs.length-1)*Geo.SqM); |
170 |
} |
171 |
|
172 |
/**
|
173 |
* <p>Returns the area of the polygon using <i>point</i> as initial, in
|
174 |
* real values with the current measure unit, according the projection in the <code>ViewPort</code>
|
175 |
* of the <code>MapControl</code>.</p>
|
176 |
*
|
177 |
* @param xs abscissa coordinate of all vertexes of the polygon
|
178 |
* @param ys ordinate coordinate of all vertexes of the polygon
|
179 |
* @param point point 2D used as first vertex in the calculation of the area
|
180 |
*
|
181 |
* @return the area of the polygon
|
182 |
*/
|
183 |
public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) { |
184 |
Point2D aux=point;
|
185 |
double elArea = 0.0; |
186 |
Point2D pPixel;
|
187 |
Point2D p = new Point2D.Double(); |
188 |
Point2D.Double pAnt = new Point2D.Double(); |
189 |
ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
190 |
for (int pos = 0; pos < xs.length-1; pos++) { |
191 |
pPixel = new Point2D.Double(xs[pos].doubleValue(), |
192 |
ys[pos].doubleValue()); |
193 |
p = pPixel;//vp.toMapPoint(pPixel);
|
194 |
if (pos == 0) { |
195 |
pAnt.x = aux.getX(); |
196 |
pAnt.y = aux.getY(); |
197 |
} |
198 |
elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
199 |
pAnt.setLocation(p); |
200 |
} |
201 |
|
202 |
elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
203 |
elArea = Math.abs(elArea / 2.0); |
204 |
return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2))); |
205 |
} |
206 |
|
207 |
/*
|
208 |
* (non-Javadoc)
|
209 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
210 |
*/
|
211 |
public Cursor getCursor() { |
212 |
return cur;
|
213 |
} |
214 |
|
215 |
/*
|
216 |
* (non-Javadoc)
|
217 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
218 |
*/
|
219 |
public void pointFixed(MeasureEvent event) { |
220 |
} |
221 |
|
222 |
/*
|
223 |
* (non-Javadoc)
|
224 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
225 |
*/
|
226 |
public void polylineFinished(MeasureEvent event) { |
227 |
} |
228 |
|
229 |
/*
|
230 |
* (non-Javadoc)
|
231 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
232 |
*/
|
233 |
public boolean cancelDrawing() { |
234 |
return false; |
235 |
} |
236 |
} |