root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 20098
History | View | Annotate | Download (10.1 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 org.cresques.cts.IProjection; |
52 |
|
53 |
import com.iver.cit.gvsig.fmap.MapContext; |
54 |
import com.iver.cit.gvsig.fmap.MapControl; |
55 |
import com.iver.cit.gvsig.fmap.ViewPort; |
56 |
import com.iver.cit.gvsig.fmap.crs.CRSFactory; |
57 |
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent; |
58 |
import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener; |
59 |
import com.iver.cit.gvsig.fmap.tools.geo.Geo; |
60 |
|
61 |
|
62 |
|
63 |
/**
|
64 |
* <p>Listener for calculating the area of a polygon, defined in the associated {@link MapControl MapControl}
|
65 |
* object.</p>
|
66 |
*
|
67 |
* <p>If the view port of the associated <code>MapControl</code> isn't projected gets the area according the
|
68 |
* geographical coordinates.</p>
|
69 |
*
|
70 |
* @author Vicente Caballero Navarro
|
71 |
*/
|
72 |
public class AreaListenerImpl implements PolylineListener { |
73 |
/**
|
74 |
* The image to display when the cursor is active.
|
75 |
*/
|
76 |
private final Image iarea = new ImageIcon(MapControl.class.getResource( |
77 |
"images/AreaCursor.gif")).getImage();
|
78 |
|
79 |
/**
|
80 |
* The cursor used to work with this tool listener.
|
81 |
*
|
82 |
* @see #getCursor()
|
83 |
*/
|
84 |
private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea, |
85 |
new Point(16, 16), ""); |
86 |
|
87 |
/**
|
88 |
* Reference to the <code>MapControl</code> object that uses.
|
89 |
*/
|
90 |
protected MapControl mapCtrl;
|
91 |
|
92 |
/**
|
93 |
* Information about all vertexes and {@link GeneralPathX GeneralPathX}s of the polyline.
|
94 |
*/
|
95 |
protected MeasureEvent event;
|
96 |
|
97 |
/**
|
98 |
* <p>Creates a new listener for calculating the area of a polygon.</p>
|
99 |
*
|
100 |
* @param mc the <code>MapControl</code> where is calculated the area
|
101 |
*/
|
102 |
public AreaListenerImpl(MapControl mc) {
|
103 |
this.mapCtrl = mc;
|
104 |
} |
105 |
|
106 |
/*
|
107 |
* (non-Javadoc)
|
108 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
109 |
*/
|
110 |
public void points(MeasureEvent event) { |
111 |
this.event = event;
|
112 |
|
113 |
double dist = 0; |
114 |
double distAll = 0; |
115 |
|
116 |
ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
117 |
|
118 |
for (int i = 0; i < (event.getXs().length - 1); i++) { |
119 |
dist = 0;
|
120 |
|
121 |
Point p = new Point(event.getXs()[i].intValue(), |
122 |
event.getXs()[i].intValue()); |
123 |
Point p2 = new Point(event.getXs()[i + 1].intValue(), |
124 |
event.getXs()[i + 1].intValue());
|
125 |
|
126 |
///dist = vp.toMapDistance((int) p.distance(p2));
|
127 |
dist = vp.distanceWorld(p, p2); |
128 |
distAll += dist; |
129 |
} |
130 |
|
131 |
System.out.println("Per?metro = " + distAll + " ?rea = " + |
132 |
(returnArea(vp.toMapPoint( |
133 |
new Point2D.Double( |
134 |
event.getXs()[event.getXs().length - 2].doubleValue(),
|
135 |
event.getYs()[event.getYs().length - 2].doubleValue())))));
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* <p>Returns the area of the polygon, using {@link #returnCoordsArea(Double[], Double[], Point2D) returnCoordsArea}
|
140 |
* if the <code>ViewPort</code> of the associated <code>MapControl</code> is projected, or using
|
141 |
* {@link #returnGeoCArea(Double[], Double[], Point2D) returnGeoCArea} if isn't.</p>
|
142 |
*
|
143 |
* @param point unused parameter
|
144 |
*
|
145 |
* @return area from the vertexes stored at the measure event in real coordinates, or, if the <code>MapControl</code>
|
146 |
* isn't projected, in geographical coordinates
|
147 |
*
|
148 |
* @see #returnCoordsArea(Double[], Double[], Point2D)
|
149 |
* @see #returnGeoCArea(Double[], Double[], Point2D)
|
150 |
*/
|
151 |
protected double returnArea(Point2D point) { |
152 |
Double[] xs=event.getXs(); |
153 |
Double[] ys=event.getYs(); |
154 |
if (mapCtrl.getProjection().isProjected()) {
|
155 |
return returnCoordsArea(xs,ys,point);
|
156 |
} |
157 |
return returnGeoCArea(xs,ys,point);
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* <p>Returns the area of the polygon using <i>point</i> as initial, in
|
162 |
* real values with the current measure unit, according the projection in the <code>ViewPort</code>
|
163 |
* of the <code>MapControl</code>.</p>
|
164 |
*
|
165 |
* @param xs abscissa coordinate of all vertexes of the polygon
|
166 |
* @param ys ordinate coordinate of all vertexes of the polygon
|
167 |
* @param point point 2D used as first vertex in the calculation of the area
|
168 |
*
|
169 |
* @return the area of the polygon
|
170 |
*/
|
171 |
public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) { |
172 |
Point2D aux=point;
|
173 |
double elArea = 0.0; |
174 |
Point2D pPixel;
|
175 |
Point2D p = new Point2D.Double(); |
176 |
Point2D.Double pAnt = new Point2D.Double(); |
177 |
ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
178 |
for (int pos = 0; pos < xs.length-1; pos++) { |
179 |
pPixel = new Point2D.Double(xs[pos].doubleValue(), |
180 |
ys[pos].doubleValue()); |
181 |
p = pPixel;//vp.toMapPoint(pPixel);
|
182 |
if (pos == 0) { |
183 |
pAnt.x = aux.getX(); |
184 |
pAnt.y = aux.getY(); |
185 |
} |
186 |
elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
187 |
pAnt.setLocation(p); |
188 |
} |
189 |
|
190 |
elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
191 |
elArea = Math.abs(elArea / 2.0); |
192 |
return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2))); |
193 |
} |
194 |
public static void main(String[] args) { |
195 |
IProjection projectionUTM = CRSFactory.getCRS("EPSG:23030");
|
196 |
ViewPort vpUTM = new ViewPort(projectionUTM);
|
197 |
MapControl mcUTM=new MapControl();
|
198 |
mcUTM.setMapContext(new MapContext(vpUTM));
|
199 |
AreaListenerImpl areaListenerUTM=new AreaListenerImpl(mcUTM);
|
200 |
IProjection projectionGeo = CRSFactory.getCRS("EPSG:4230");
|
201 |
ViewPort vpGeo = new ViewPort(projectionGeo);
|
202 |
MapControl mcGeo=new MapControl();
|
203 |
mcGeo.setMapContext(new MapContext(vpGeo));
|
204 |
AreaListenerImpl areaListenerGeo=new AreaListenerImpl(mcGeo);
|
205 |
|
206 |
Double[] xsUTMCaseta=new Double[] {new Double(547508.77),new Double(547517.73),new Double(547512.65)}; |
207 |
Double[] ysUTMCaseta=new Double[] {new Double(4704333.97),new Double(4704331.3),new Double(4704315.2)}; |
208 |
double areaUTMCaseta=areaListenerUTM.returnCoordsArea(xsUTMCaseta,ysUTMCaseta,new Point2D.Double(547512.65,4704315.2)); |
209 |
Double[] xsGeoCaseta=new Double[] {new Double(-2.42192383),new Double(-2.42181545),new Double(-2.42187771)}; |
210 |
Double[] ysGeoCaseta=new Double[] {new Double(42.48914909),new Double(42.48912295),new Double(42.48897922)}; |
211 |
double areaGeoCCaseta=areaListenerGeo.returnGeoCArea(xsGeoCaseta,ysGeoCaseta,new Point2D.Double(-2.42187771,42.48897922)); |
212 |
|
213 |
System.out.println("AreaUTMCaseta = "+ areaUTMCaseta); |
214 |
System.out.println("AreaGeoCCaseta = "+ areaGeoCCaseta); |
215 |
|
216 |
|
217 |
Double[] xsUTM=new Double[] {new Double(731292),new Double(731901),new Double(730138)}; |
218 |
Double[] ysUTM=new Double[] {new Double(4351223),new Double(4350768),new Double(4349232)}; |
219 |
double areaUTM=areaListenerUTM.returnCoordsArea(xsUTM,ysUTM,new Point2D.Double(730138,4349232)); |
220 |
Double[] xsGeo=new Double[] {new Double(-0.31888183),new Double(-0.31173131),new Double(-0.33268401)}; |
221 |
Double[] ysGeo=new Double[] {new Double(39.27871741),new Double(39.27464327),new Double(39.26117368)}; |
222 |
double areaGeoC=areaListenerGeo.returnGeoCArea(xsGeo,ysGeo,new Point2D.Double(-0.33268401,39.26117368)); |
223 |
|
224 |
System.out.println("AreaUTM = "+ areaUTM); |
225 |
System.out.println("AreaGeoC = "+ areaGeoC); |
226 |
|
227 |
Double[] xsUTMspain=new Double[] {new Double(-12806),new Double(1025790),new Double(-31353.14)}; |
228 |
Double[] ysUTMspain=new Double[] {new Double(4793276.43),new Double(4719090.94),new Double(4125607.02)}; |
229 |
double areaUTMspain=areaListenerUTM.returnCoordsArea(xsUTMspain,ysUTMspain,new Point2D.Double(730138,4349232)); |
230 |
Double[] xsGeospain=new Double[] {new Double(-9.22743872),new Double(3.33087936),new Double(-9.01458587),new Double(-9.22743872)}; |
231 |
Double[] ysGeospain=new Double[] {new Double(43.02384666),new Double(42.38528811),new Double(37.06396689),new Double(43.02384666)}; |
232 |
double areaGeospainC=areaListenerGeo.returnGeoCArea(xsGeospain,ysGeospain,new Point2D.Double(-9.01458587,37.06396689)); |
233 |
|
234 |
System.out.println("AreaUTMSpain = "+ areaUTMspain); |
235 |
System.out.println("AreaGeoSpainC = "+ areaGeospainC); |
236 |
} |
237 |
|
238 |
/**
|
239 |
* <p>Returns the area in geographical coordinates of the polygon, according the
|
240 |
* <a href="http://en.wikipedia.org/wiki/Haversine_formula">Haversine function</a>.</p>
|
241 |
*
|
242 |
* @see Geo#sphericalPolyArea(double[], double[], int)
|
243 |
*/
|
244 |
public double returnGeoCArea(Double[] xs, Double[] ys, Point2D point) { |
245 |
double[] lat = new double[xs.length]; |
246 |
double[] lon = new double[xs.length]; |
247 |
for (int K = 0; K < xs.length; K++) { |
248 |
lon[K] = xs[K].doubleValue() / Geo.Degree; |
249 |
lat[K] = ys[K].doubleValue() / Geo.Degree; |
250 |
} |
251 |
return (Geo.sphericalPolyArea(lat, lon, xs.length - 1) * Geo.SqM);// /1.29132441;//Esto |
252 |
} |
253 |
|
254 |
/*
|
255 |
* (non-Javadoc)
|
256 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
257 |
*/
|
258 |
public Cursor getCursor() { |
259 |
return cur;
|
260 |
} |
261 |
|
262 |
/*
|
263 |
* (non-Javadoc)
|
264 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
265 |
*/
|
266 |
public void pointFixed(MeasureEvent event) { |
267 |
} |
268 |
|
269 |
/*
|
270 |
* (non-Javadoc)
|
271 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
272 |
*/
|
273 |
public void polylineFinished(MeasureEvent event) { |
274 |
} |
275 |
|
276 |
/*
|
277 |
* (non-Javadoc)
|
278 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
279 |
*/
|
280 |
public boolean cancelDrawing() { |
281 |
return false; |
282 |
} |
283 |
} |