Statistics
| Revision:

svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 19509

History | View | Annotate | Download (8.42 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
 * Implementaci?n de la interfaz MeasureListener como herramienta para medir el
65
 * ?rea.
66
 *
67
 * @author Vicente Caballero Navarro
68
 */
69
public class AreaListenerImpl implements PolylineListener {
70
        private final Image iarea = new ImageIcon(MapControl.class.getResource(
71
                                "images/AreaCursor.gif")).getImage();
72
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea,
73
                        new Point(16, 16), "");
74
        protected MapControl mapCtrl;
75
        protected MeasureEvent event;
76

    
77
        /**
78
         * Crea un nuevo AreaListenerImpl.
79
         *
80
         * @param mc MapControl.
81
         */
82
        public AreaListenerImpl(MapControl mc) {
83
                this.mapCtrl = mc;
84
        }
85

    
86
        /**
87
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
88
         */
89
        public void points(MeasureEvent event) {
90
                this.event = event;
91

    
92
                double dist = 0;
93
                double distAll = 0;
94

    
95
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
96

    
97
                for (int i = 0; i < (event.getXs().length - 1); i++) {
98
                        dist = 0;
99

    
100
                        Point p = new Point(event.getXs()[i].intValue(),
101
                                        event.getXs()[i].intValue());
102
                        Point p2 = new Point(event.getXs()[i + 1].intValue(),
103
                                        event.getXs()[i + 1].intValue());
104

    
105
                        ///dist = vp.toMapDistance((int) p.distance(p2));
106
                        dist = vp.distanceWorld(p, p2);
107
                        distAll += dist;
108
                }
109

    
110
                System.out.println("Per?metro = " + distAll + " ?rea = " +
111
                        (returnArea(vp.toMapPoint(
112
                                        new Point2D.Double(
113
                                                event.getXs()[event.getXs().length - 2].doubleValue(),
114
                                                event.getYs()[event.getYs().length - 2].doubleValue())))));
115
        }
116

    
117
        protected double returnArea(Point2D point) {
118
                Double[] xs=event.getXs();
119
                Double[] ys=event.getYs();
120
                if (mapCtrl.getProjection().isProjected()) {
121
                        return returnCoordsArea(xs,ys,point);
122
                }
123
                return returnGeoCArea(xs,ys,point);
124
        }
125

    
126
        /**
127
         * Calcula el ?rea.
128
         *
129
         * @param aux ?ltimo punto.
130
         *
131
         * @return ?rea.
132
         */
133
        public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) {
134
                Point2D aux=point;
135
                double elArea = 0.0;
136
                Point2D pPixel;
137
                Point2D p = new Point2D.Double();
138
                Point2D.Double pAnt = new Point2D.Double();
139
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
140
                for (int pos = 0; pos < xs.length-1; pos++) {
141
                        pPixel = new Point2D.Double(xs[pos].doubleValue(),
142
                                        ys[pos].doubleValue());
143
                        p = pPixel;//vp.toMapPoint(pPixel);
144
                        if (pos == 0) {
145
                                pAnt.x = aux.getX();
146
                                pAnt.y = aux.getY();
147
                        }
148
                        elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY()));
149
                        pAnt.setLocation(p);
150
                }
151

    
152
                elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY()));
153
                elArea = Math.abs(elArea / 2.0);
154
                return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2)));
155
        }
156
public static void main(String[] args) {
157
        IProjection projectionUTM = CRSFactory.getCRS("EPSG:23030");
158
        ViewPort vpUTM = new ViewPort(projectionUTM);
159
        MapControl mcUTM=new MapControl();
160
        mcUTM.setMapContext(new MapContext(vpUTM));
161
        AreaListenerImpl areaListenerUTM=new AreaListenerImpl(mcUTM);
162
        IProjection projectionGeo = CRSFactory.getCRS("EPSG:4230");
163
        ViewPort vpGeo = new ViewPort(projectionGeo);
164
        MapControl mcGeo=new MapControl();
165
        mcGeo.setMapContext(new MapContext(vpGeo));
166
        AreaListenerImpl areaListenerGeo=new AreaListenerImpl(mcGeo);
167

    
168
        Double[] xsUTMCaseta=new Double[] {new Double(547508.77),new Double(547517.73),new Double(547512.65)};
169
        Double[] ysUTMCaseta=new Double[] {new Double(4704333.97),new Double(4704331.3),new Double(4704315.2)};
170
        double areaUTMCaseta=areaListenerUTM.returnCoordsArea(xsUTMCaseta,ysUTMCaseta,new Point2D.Double(547512.65,4704315.2));
171
        Double[] xsGeoCaseta=new Double[] {new Double(-2.42192383),new Double(-2.42181545),new Double(-2.42187771)};
172
        Double[] ysGeoCaseta=new Double[] {new Double(42.48914909),new Double(42.48912295),new Double(42.48897922)};
173
        double areaGeoCCaseta=areaListenerGeo.returnGeoCArea(xsGeoCaseta,ysGeoCaseta,new Point2D.Double(-2.42187771,42.48897922));
174

    
175
        System.out.println("AreaUTMCaseta = "+ areaUTMCaseta);
176
        System.out.println("AreaGeoCCaseta = "+ areaGeoCCaseta);
177

    
178

    
179
        Double[] xsUTM=new Double[] {new Double(731292),new Double(731901),new Double(730138)};
180
        Double[] ysUTM=new Double[] {new Double(4351223),new Double(4350768),new Double(4349232)};
181
        double areaUTM=areaListenerUTM.returnCoordsArea(xsUTM,ysUTM,new Point2D.Double(730138,4349232));
182
        Double[] xsGeo=new Double[] {new Double(-0.31888183),new Double(-0.31173131),new Double(-0.33268401)};
183
        Double[] ysGeo=new Double[] {new Double(39.27871741),new Double(39.27464327),new Double(39.26117368)};
184
        double areaGeoC=areaListenerGeo.returnGeoCArea(xsGeo,ysGeo,new Point2D.Double(-0.33268401,39.26117368));
185

    
186
        System.out.println("AreaUTM = "+ areaUTM);
187
        System.out.println("AreaGeoC = "+ areaGeoC);
188

    
189
        Double[] xsUTMspain=new Double[] {new Double(-12806),new Double(1025790),new Double(-31353.14)};
190
        Double[] ysUTMspain=new Double[] {new Double(4793276.43),new Double(4719090.94),new Double(4125607.02)};
191
        double areaUTMspain=areaListenerUTM.returnCoordsArea(xsUTMspain,ysUTMspain,new Point2D.Double(730138,4349232));
192
        Double[] xsGeospain=new Double[] {new Double(-9.22743872),new Double(3.33087936),new Double(-9.01458587),new Double(-9.22743872)};
193
        Double[] ysGeospain=new Double[] {new Double(43.02384666),new Double(42.38528811),new Double(37.06396689),new Double(43.02384666)};
194
        double areaGeospainC=areaListenerGeo.returnGeoCArea(xsGeospain,ysGeospain,new Point2D.Double(-9.01458587,37.06396689));
195

    
196
        System.out.println("AreaUTMSpain = "+ areaUTMspain);
197
        System.out.println("AreaGeoSpainC = "+ areaGeospainC);
198
}
199
        public double returnGeoCArea(Double[] xs,Double[] ys,Point2D point) {
200
                double[] lat=new double[xs.length];
201
                double[] lon=new double[xs.length];
202
                for (int K= 0; K < xs.length; K++){
203
                        lon[K]= xs[K].doubleValue()/Geo.Degree;
204
                        lat[K]= ys[K].doubleValue()/Geo.Degree;
205
                }
206
                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.
207
        }
208
        /**
209
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
210
         */
211
        public Cursor getCursor() {
212
                return cur;
213
        }
214

    
215
        /**
216
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
217
         */
218
        public void pointFixed(MeasureEvent event) {
219
        }
220

    
221
        /**
222
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
223
         */
224
        public void polylineFinished(MeasureEvent event) {
225
        }
226

    
227
        /**
228
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
229
         */
230
        public boolean cancelDrawing() {
231
                return false;
232
        }
233
}