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