Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / AreaListenerImpl.java @ 36684

History | View | Annotate | Download (10.4 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.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
import org.gvsig.fmap.crs.CRSFactory;
53
import org.gvsig.fmap.mapcontext.MapContext;
54
import org.gvsig.fmap.mapcontext.ViewPort;
55
import org.gvsig.fmap.mapcontrol.MapControl;
56
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
57
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
58
import org.gvsig.fmap.mapcontrol.tools.geo.Geo;
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 = new ImageIcon(MapControl.class.getClassLoader().getResource(
81
                                "org/gvsig/fmap/mapcontrol/images/AreaCursor.gif")).getImage();
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
                logger.debug("Perimetro = {}, Area = {}.", 
128
                        distAll,
129
                        returnArea(vp.toMapPoint(
130
                                        new Point2D.Double(
131
                                                event.getXs()[event.getXs().length - 2].doubleValue(),
132
                                                event.getYs()[event.getYs().length - 2].doubleValue()
133
                                        )
134
                                )
135
                        )
136
                );
137

    
138
        }
139

    
140
        /**
141
         * <p>Returns the area of the polygon, using {@link #returnCoordsArea(Double[], Double[], Point2D) returnCoordsArea}
142
         *  if the <code>ViewPort</code> of the associated <code>MapControl</code> is projected, or using
143
         *  {@link #returnGeoCArea(Double[], Double[], Point2D) returnGeoCArea} if isn't.</p>
144
         *
145
         * @param point unused parameter
146
         *
147
         * @return area from the vertexes stored at the measure event in real coordinates, or, if the <code>MapControl</code>
148
         *  isn't projected, in geographical coordinates
149
         *
150
         * @see #returnCoordsArea(Double[], Double[], Point2D)
151
         * @see #returnGeoCArea(Double[], Double[], Point2D)
152
         */
153
        protected double returnArea(Point2D point) {
154
                Double[] xs=event.getXs();
155
                Double[] ys=event.getYs();
156
                if (mapCtrl.getProjection().isProjected()) {
157
                        return returnCoordsArea(xs,ys,point);
158
                }
159
                return returnGeoCArea(xs,ys,point);
160
        }
161

    
162
        /**
163
         * <p>Returns the area of the polygon using <i>point</i> as initial, in
164
         *  real values with the current measure unit, according the projection in the <code>ViewPort</code>
165
         *  of the <code>MapControl</code>.</p>
166
         *
167
         * @param xs abscissa coordinate of all vertexes of the polygon
168
         * @param ys ordinate coordinate of all vertexes of the polygon
169
         * @param point point 2D used as first vertex in the calculation of the area
170
         *
171
         * @return the area of the polygon
172
         */
173
        public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) {
174
                Point2D aux=point;
175
                double elArea = 0.0;
176
                Point2D pPixel;
177
                Point2D p = new Point2D.Double();
178
                Point2D.Double pAnt = new Point2D.Double();
179
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
180
                for (int pos = 0; pos < xs.length-1; pos++) {
181
                        pPixel = new Point2D.Double(xs[pos].doubleValue(),
182
                                        ys[pos].doubleValue());
183
                        p = pPixel;//vp.toMapPoint(pPixel);
184
                        if (pos == 0) {
185
                                pAnt.x = aux.getX();
186
                                pAnt.y = aux.getY();
187
                        }
188
                        elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY()));
189
                        pAnt.setLocation(p);
190
                }
191

    
192
                elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY()));
193
                elArea = Math.abs(elArea / 2.0);
194
                return (elArea*(Math.pow(MapContext.getDistanceTrans2Meter()[vp.getMapUnits()],2)));
195
        }
196
        public static void main(String[] args) {
197
                IProjection projectionUTM = CRSFactory.getCRS("EPSG:23030");
198
                ViewPort vpUTM = new ViewPort(projectionUTM);
199
                MapControl mcUTM=new MapControl();
200
                mcUTM.setMapContext(new MapContext(vpUTM));
201
                AreaListenerImpl areaListenerUTM=new AreaListenerImpl(mcUTM);
202
                IProjection projectionGeo = CRSFactory.getCRS("EPSG:4230");
203
                ViewPort vpGeo = new ViewPort(projectionGeo);
204
                MapControl mcGeo=new MapControl();
205
                mcGeo.setMapContext(new MapContext(vpGeo));
206
                AreaListenerImpl areaListenerGeo=new AreaListenerImpl(mcGeo);
207

    
208
                Double[] xsUTMCaseta=new Double[] {new Double(547508.77),new Double(547517.73),new Double(547512.65)};
209
                Double[] ysUTMCaseta=new Double[] {new Double(4704333.97),new Double(4704331.3),new Double(4704315.2)};
210
                double areaUTMCaseta=areaListenerUTM.returnCoordsArea(xsUTMCaseta,ysUTMCaseta,new Point2D.Double(547512.65,4704315.2));
211
                Double[] xsGeoCaseta=new Double[] {new Double(-2.42192383),new Double(-2.42181545),new Double(-2.42187771)};
212
                Double[] ysGeoCaseta=new Double[] {new Double(42.48914909),new Double(42.48912295),new Double(42.48897922)};
213
                double areaGeoCCaseta=areaListenerGeo.returnGeoCArea(xsGeoCaseta,ysGeoCaseta,new Point2D.Double(-2.42187771,42.48897922));
214

    
215
                System.out.println("AreaUTMCaseta = "+ areaUTMCaseta);
216
                System.out.println("AreaGeoCCaseta = "+ areaGeoCCaseta);
217

    
218

    
219
                Double[] xsUTM=new Double[] {new Double(731292),new Double(731901),new Double(730138)};
220
                Double[] ysUTM=new Double[] {new Double(4351223),new Double(4350768),new Double(4349232)};
221
                double areaUTM=areaListenerUTM.returnCoordsArea(xsUTM,ysUTM,new Point2D.Double(730138,4349232));
222
                Double[] xsGeo=new Double[] {new Double(-0.31888183),new Double(-0.31173131),new Double(-0.33268401)};
223
                Double[] ysGeo=new Double[] {new Double(39.27871741),new Double(39.27464327),new Double(39.26117368)};
224
                double areaGeoC=areaListenerGeo.returnGeoCArea(xsGeo,ysGeo,new Point2D.Double(-0.33268401,39.26117368));
225

    
226
                System.out.println("AreaUTM = "+ areaUTM);
227
                System.out.println("AreaGeoC = "+ areaGeoC);
228

    
229
                Double[] xsUTMspain=new Double[] {new Double(-12806),new Double(1025790),new Double(-31353.14)};
230
                Double[] ysUTMspain=new Double[] {new Double(4793276.43),new Double(4719090.94),new Double(4125607.02)};
231
                double areaUTMspain=areaListenerUTM.returnCoordsArea(xsUTMspain,ysUTMspain,new Point2D.Double(730138,4349232));
232
                Double[] xsGeospain=new Double[] {new Double(-9.22743872),new Double(3.33087936),new Double(-9.01458587),new Double(-9.22743872)};
233
                Double[] ysGeospain=new Double[] {new Double(43.02384666),new Double(42.38528811),new Double(37.06396689),new Double(43.02384666)};
234
                double areaGeospainC=areaListenerGeo.returnGeoCArea(xsGeospain,ysGeospain,new Point2D.Double(-9.01458587,37.06396689));
235

    
236
                System.out.println("AreaUTMSpain = "+ areaUTMspain);
237
                System.out.println("AreaGeoSpainC = "+ areaGeospainC);
238
        }
239

    
240
        /**
241
         * <p>Returns the area in geographical coordinates of the polygon, according the
242
         *  <a href="http://en.wikipedia.org/wiki/Haversine_formula">Haversine function</a>.</p>
243
         *
244
         * @see Geo#sphericalPolyArea(double[], double[], int)
245
         */
246
        public double returnGeoCArea(Double[] xs, Double[] ys, Point2D point) {
247
                double[] lat = new double[xs.length];
248
                double[] lon = new double[xs.length];
249
                for (int K = 0; K < xs.length; K++) {
250
                        lon[K] = xs[K].doubleValue() / Geo.Degree;
251
                        lat[K] = ys[K].doubleValue() / Geo.Degree;
252
                }
253
                return (Geo.sphericalPolyArea(lat, lon, xs.length - 1) * Geo.SqM);// /1.29132441;//Esto
254
        }
255

    
256
        /*
257
         * (non-Javadoc)
258
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
259
         */
260
        public Image getImageCursor() {
261
                return iarea;
262
        }
263

    
264
        /*
265
         * (non-Javadoc)
266
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
267
         */
268
        public void pointFixed(MeasureEvent event) {
269
        }
270

    
271
        /*
272
         * (non-Javadoc)
273
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
274
         */
275
        public void polylineFinished(MeasureEvent event) {
276
        }
277

    
278
        /*
279
         * (non-Javadoc)
280
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
281
         */
282
        public boolean cancelDrawing() {
283
                return false;
284
        }
285
}