Statistics
| Revision:

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

History | View | Annotate | Download (5.24 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 com.iver.cit.gvsig.fmap.MapContext;
44
import com.iver.cit.gvsig.fmap.MapControl;
45
import com.iver.cit.gvsig.fmap.ViewPort;
46
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent;
47
import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener;
48
import com.iver.cit.gvsig.fmap.tools.geo.Geo;
49

    
50
import java.awt.Cursor;
51
import java.awt.Image;
52
import java.awt.Point;
53
import java.awt.Toolkit;
54
import java.awt.geom.Point2D;
55

    
56
import javax.swing.ImageIcon;
57

    
58

    
59
/**
60
 * Implementaci?n de la interfaz MeasureListener como herramienta para medir el
61
 * ?rea.
62
 *
63
 * @author Vicente Caballero Navarro
64
 */
65
public class AreaListenerImpl implements PolylineListener {
66
        private final Image iarea = new ImageIcon(MapControl.class.getResource(
67
                                "images/AreaCursor.gif")).getImage();
68
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea,
69
                        new Point(16, 16), "");
70
        protected MapControl mapCtrl;
71
        protected MeasureEvent event;
72

    
73
        /**
74
         * Crea un nuevo AreaListenerImpl.
75
         *
76
         * @param mc MapControl.
77
         */
78
        public AreaListenerImpl(MapControl mc) {
79
                this.mapCtrl = mc;
80
        }
81

    
82
        /**
83
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
84
         */
85
        public void points(MeasureEvent event) {
86
                this.event = event;
87

    
88
                double dist = 0;
89
                double distAll = 0;
90

    
91
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
92

    
93
                for (int i = 0; i < (event.getXs().length - 1); i++) {
94
                        dist = 0;
95

    
96
                        Point p = new Point(event.getXs()[i].intValue(),
97
                                        event.getXs()[i].intValue());
98
                        Point p2 = new Point(event.getXs()[i + 1].intValue(),
99
                                        event.getXs()[i + 1].intValue());
100

    
101
                        ///dist = vp.toMapDistance((int) p.distance(p2));
102
                        dist = vp.distanceWorld(p, p2);
103
                        distAll += dist;
104
                }
105

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

    
113
        protected double returnArea(Point2D point) {
114
                Double[] xs=event.getXs();
115
                Double[] ys=event.getYs();
116
                if (mapCtrl.getProjection().isProjected()) {
117
                        return returnCoordsArea(xs,ys,point);
118
                }
119
                return returnGeoCArea(xs,ys,point);
120
        }
121
        public double returnGeoCArea(Double[] xs,Double[] ys,Point2D point) {
122
                double[] lat=new double[xs.length];
123
                double[] lon=new double[xs.length];
124
                for (int K= 0; K < xs.length; K++){
125
                        lon[K]= xs[K].doubleValue()/Geo.Degree;
126
                        lat[K]= ys[K].doubleValue()/Geo.Degree;
127
                }
128
                return (Geo.sphericalPolyArea(lat,lon,xs.length-1)*Geo.SqM);
129
        }
130
        /**
131
         * Calcula el ?rea.
132
         *
133
         * @param aux ?ltimo punto.
134
         *
135
         * @return ?rea.
136
         */
137
        public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) {
138
                Point2D aux=point;
139
                double elArea = 0.0;
140
                Point2D pPixel;
141
                Point2D p = new Point2D.Double();
142
                Point2D.Double pAnt = new Point2D.Double();
143
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
144
                for (int pos = 0; pos < xs.length-1; pos++) {
145
                        pPixel = new Point2D.Double(xs[pos].doubleValue(),
146
                                        ys[pos].doubleValue());
147
                        p = pPixel;//vp.toMapPoint(pPixel);
148
                        if (pos == 0) {
149
                                pAnt.x = aux.getX();
150
                                pAnt.y = aux.getY();
151
                        }
152
                        elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY()));
153
                        pAnt.setLocation(p);
154
                }
155

    
156
                elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY()));
157
                elArea = Math.abs(elArea / 2.0);
158
                return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2)));
159
        }
160

    
161
        /**
162
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
163
         */
164
        public Cursor getCursor() {
165
                return cur;
166
        }
167

    
168
        /**
169
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
170
         */
171
        public void pointFixed(MeasureEvent event) {
172
        }
173

    
174
        /**
175
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
176
         */
177
        public void polylineFinished(MeasureEvent event) {
178
        }
179

    
180
        /**
181
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
182
         */
183
        public boolean cancelDrawing() {
184
                return false;
185
        }
186
}