root / branches / simbologia / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 10450
History | View | Annotate | Download (8.19 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 | 291 | vcaballero | import java.awt.Cursor; |
44 | import java.awt.Image; |
||
45 | 288 | vcaballero | import java.awt.Point; |
46 | 291 | vcaballero | import java.awt.Toolkit; |
47 | 288 | vcaballero | import java.awt.geom.Point2D; |
48 | |||
49 | 291 | vcaballero | import javax.swing.ImageIcon; |
50 | 288 | vcaballero | |
51 | 9820 | caballero | import org.cresques.cts.IProjection; |
52 | 291 | vcaballero | |
53 | 9820 | caballero | 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 | 9904 | caballero | |
63 | 288 | vcaballero | /**
|
64 | 10351 | jaume | * Implementaci?n de la interfaz MeasureListener como herramienta para medir el ?rea.
|
65 | * @author Vicente Caballero Navarro
|
||
66 | 288 | vcaballero | */
|
67 | 1221 | fernando | public class AreaListenerImpl implements PolylineListener { |
68 | 1223 | fernando | private final Image iarea = new ImageIcon(MapControl.class.getResource( |
69 | 1050 | vcaballero | "images/AreaCursor.gif")).getImage();
|
70 | 291 | vcaballero | private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea, |
71 | 1050 | vcaballero | new Point(16, 16), ""); |
72 | 1223 | fernando | protected MapControl mapCtrl;
|
73 | 1050 | vcaballero | protected MeasureEvent event;
|
74 | 288 | vcaballero | |
75 | 1050 | vcaballero | /**
|
76 | * Crea un nuevo AreaListenerImpl.
|
||
77 | *
|
||
78 | * @param mc MapControl.
|
||
79 | */
|
||
80 | 1223 | fernando | public AreaListenerImpl(MapControl mc) {
|
81 | 1050 | vcaballero | this.mapCtrl = mc;
|
82 | } |
||
83 | 288 | vcaballero | |
84 | 1050 | vcaballero | /**
|
85 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
86 | 1050 | vcaballero | */
|
87 | public void points(MeasureEvent event) { |
||
88 | this.event = event;
|
||
89 | 288 | vcaballero | |
90 | 1050 | vcaballero | double dist = 0; |
91 | double distAll = 0; |
||
92 | 288 | vcaballero | |
93 | 1050 | vcaballero | ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
94 | 288 | vcaballero | |
95 | 1050 | vcaballero | for (int i = 0; i < (event.getXs().length - 1); i++) { |
96 | dist = 0;
|
||
97 | 288 | vcaballero | |
98 | 1050 | vcaballero | Point p = new Point(event.getXs()[i].intValue(), |
99 | event.getXs()[i].intValue()); |
||
100 | Point p2 = new Point(event.getXs()[i + 1].intValue(), |
||
101 | event.getXs()[i + 1].intValue());
|
||
102 | 288 | vcaballero | |
103 | 1050 | vcaballero | ///dist = vp.toMapDistance((int) p.distance(p2));
|
104 | dist = vp.distanceWorld(p, p2); |
||
105 | distAll += dist; |
||
106 | } |
||
107 | 288 | vcaballero | |
108 | 1050 | vcaballero | System.out.println("Per?metro = " + distAll + " ?rea = " + |
109 | (returnArea(vp.toMapPoint( |
||
110 | new Point2D.Double( |
||
111 | event.getXs()[event.getXs().length - 2].doubleValue(),
|
||
112 | event.getYs()[event.getYs().length - 2].doubleValue())))));
|
||
113 | } |
||
114 | 288 | vcaballero | |
115 | 9820 | caballero | protected double returnArea(Point2D point) { |
116 | Double[] xs=event.getXs(); |
||
117 | Double[] ys=event.getYs(); |
||
118 | if (mapCtrl.getProjection().isProjected()) {
|
||
119 | return returnCoordsArea(xs,ys,point);
|
||
120 | } |
||
121 | return returnGeoCArea(xs,ys,point);
|
||
122 | } |
||
123 | |||
124 | 1050 | vcaballero | /**
|
125 | * Calcula el ?rea.
|
||
126 | *
|
||
127 | * @param aux ?ltimo punto.
|
||
128 | *
|
||
129 | * @return ?rea.
|
||
130 | */
|
||
131 | 9820 | caballero | public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) { |
132 | Point2D aux=point;
|
||
133 | double elArea = 0.0; |
||
134 | 1050 | vcaballero | Point2D pPixel;
|
135 | Point2D p = new Point2D.Double(); |
||
136 | Point2D.Double pAnt = new Point2D.Double(); |
||
137 | ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
||
138 | 9820 | caballero | for (int pos = 0; pos < xs.length-1; pos++) { |
139 | pPixel = new Point2D.Double(xs[pos].doubleValue(), |
||
140 | ys[pos].doubleValue()); |
||
141 | 4226 | caballero | p = pPixel;//vp.toMapPoint(pPixel);
|
142 | if (pos == 0) { |
||
143 | 1050 | vcaballero | pAnt.x = aux.getX(); |
144 | pAnt.y = aux.getY(); |
||
145 | } |
||
146 | elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
||
147 | pAnt.setLocation(p); |
||
148 | } |
||
149 | 291 | vcaballero | |
150 | 1050 | vcaballero | elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
151 | elArea = Math.abs(elArea / 2.0); |
||
152 | 9820 | caballero | return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2))); |
153 | } |
||
154 | public static void main(String[] args) { |
||
155 | IProjection projectionUTM = CRSFactory.getCRS("EPSG:23030");
|
||
156 | ViewPort vpUTM = new ViewPort(projectionUTM);
|
||
157 | MapControl mcUTM=new MapControl();
|
||
158 | mcUTM.setMapContext(new MapContext(vpUTM));
|
||
159 | AreaListenerImpl areaListenerUTM=new AreaListenerImpl(mcUTM);
|
||
160 | IProjection projectionGeo = CRSFactory.getCRS("EPSG:4230");
|
||
161 | ViewPort vpGeo = new ViewPort(projectionGeo);
|
||
162 | MapControl mcGeo=new MapControl();
|
||
163 | mcGeo.setMapContext(new MapContext(vpGeo));
|
||
164 | AreaListenerImpl areaListenerGeo=new AreaListenerImpl(mcGeo);
|
||
165 | 1050 | vcaballero | |
166 | 9820 | caballero | Double[] xsUTMCaseta=new Double[] {new Double(547508.77),new Double(547517.73),new Double(547512.65)}; |
167 | Double[] ysUTMCaseta=new Double[] {new Double(4704333.97),new Double(4704331.3),new Double(4704315.2)}; |
||
168 | double areaUTMCaseta=areaListenerUTM.returnCoordsArea(xsUTMCaseta,ysUTMCaseta,new Point2D.Double(547512.65,4704315.2)); |
||
169 | Double[] xsGeoCaseta=new Double[] {new Double(-2.42192383),new Double(-2.42181545),new Double(-2.42187771)}; |
||
170 | Double[] ysGeoCaseta=new Double[] {new Double(42.48914909),new Double(42.48912295),new Double(42.48897922)}; |
||
171 | double areaGeoCCaseta=areaListenerGeo.returnGeoCArea(xsGeoCaseta,ysGeoCaseta,new Point2D.Double(-2.42187771,42.48897922)); |
||
172 | |||
173 | System.out.println("AreaUTMCaseta = "+ areaUTMCaseta); |
||
174 | System.out.println("AreaGeoCCaseta = "+ areaGeoCCaseta); |
||
175 | |||
176 | |||
177 | Double[] xsUTM=new Double[] {new Double(731292),new Double(731901),new Double(730138)}; |
||
178 | Double[] ysUTM=new Double[] {new Double(4351223),new Double(4350768),new Double(4349232)}; |
||
179 | double areaUTM=areaListenerUTM.returnCoordsArea(xsUTM,ysUTM,new Point2D.Double(730138,4349232)); |
||
180 | Double[] xsGeo=new Double[] {new Double(-0.31888183),new Double(-0.31173131),new Double(-0.33268401)}; |
||
181 | Double[] ysGeo=new Double[] {new Double(39.27871741),new Double(39.27464327),new Double(39.26117368)}; |
||
182 | double areaGeoC=areaListenerGeo.returnGeoCArea(xsGeo,ysGeo,new Point2D.Double(-0.33268401,39.26117368)); |
||
183 | |||
184 | System.out.println("AreaUTM = "+ areaUTM); |
||
185 | System.out.println("AreaGeoC = "+ areaGeoC); |
||
186 | |||
187 | Double[] xsUTMspain=new Double[] {new Double(-12806),new Double(1025790),new Double(-31353.14)}; |
||
188 | Double[] ysUTMspain=new Double[] {new Double(4793276.43),new Double(4719090.94),new Double(4125607.02)}; |
||
189 | double areaUTMspain=areaListenerUTM.returnCoordsArea(xsUTMspain,ysUTMspain,new Point2D.Double(730138,4349232)); |
||
190 | 9904 | caballero | Double[] xsGeospain=new Double[] {new Double(-9.22743872),new Double(3.33087936),new Double(-9.01458587),new Double(-9.22743872)}; |
191 | Double[] ysGeospain=new Double[] {new Double(43.02384666),new Double(42.38528811),new Double(37.06396689),new Double(43.02384666)}; |
||
192 | 9820 | caballero | double areaGeospainC=areaListenerGeo.returnGeoCArea(xsGeospain,ysGeospain,new Point2D.Double(-9.01458587,37.06396689)); |
193 | |||
194 | System.out.println("AreaUTMSpain = "+ areaUTMspain); |
||
195 | System.out.println("AreaGeoSpainC = "+ areaGeospainC); |
||
196 | } |
||
197 | public double returnGeoCArea(Double[] xs,Double[] ys,Point2D point) { |
||
198 | double[] lat=new double[xs.length]; |
||
199 | double[] lon=new double[xs.length]; |
||
200 | for (int K= 0; K < xs.length; K++){ |
||
201 | 9904 | caballero | lon[K]= xs[K].doubleValue()/Geo.Degree; |
202 | lat[K]= ys[K].doubleValue()/Geo.Degree; |
||
203 | 9820 | caballero | } |
204 | 10450 | jaume | return (Geo.sphericalPolyArea(lat,lon,xs.length-1)*Geo.SqM);///1.29132441;//Esto es una constante para ajustar el resultado, que por alg?n motivo no es correcto. |
205 | 9820 | caballero | } |
206 | 291 | vcaballero | /**
|
207 | * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
||
208 | */
|
||
209 | public Cursor getCursor() { |
||
210 | return cur;
|
||
211 | } |
||
212 | 363 | fernando | |
213 | /**
|
||
214 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
215 | 363 | fernando | */
|
216 | public void pointFixed(MeasureEvent event) { |
||
217 | } |
||
218 | |||
219 | /**
|
||
220 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
221 | 363 | fernando | */
|
222 | public void polylineFinished(MeasureEvent event) { |
||
223 | } |
||
224 | |||
225 | /**
|
||
226 | * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
||
227 | */
|
||
228 | public boolean cancelDrawing() { |
||
229 | return false; |
||
230 | } |
||
231 | 291 | vcaballero | } |