Statistics
| Revision:

root / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 9590

History | View | Annotate | Download (5.45 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

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

    
55
import javax.swing.ImageIcon;
56

    
57

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

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

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

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

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

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

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

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

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

    
112
        /**
113
         * Calcula el ?rea.
114
         *
115
         * @param aux ?ltimo punto.
116
         *
117
         * @return ?rea.
118
         */
119
        protected double returnArea(Point2D aux) {
120
                System.out.println("aux = " + aux);
121

    
122
                double elArea;
123
                //int pos=0;
124
                Point2D pPixel;
125
                Point2D p = new Point2D.Double();
126
                Point2D.Double pAnt = new Point2D.Double();
127
                //long i;
128
                Double[] x = event.getXs();
129
                Double[] y = event.getYs();
130

    
131
                // double baseMayor, baseMenor, altura; // De cada trapezoide
132
                // El area de cada pareja de puntos ser? (x2-x1) * baseMenor + (baseMayor-baseMenor)*(x2-x1)
133
                // Simplificando la ecuaci?n queda: (x2-x1)*((baseMayor+baseMenor)/2). Es decir, que da igual
134
                // qui?n es la base mayor y quien la manor.
135
                // si la coordenada X del punto i+1 es mayor que la del punto i => Area negativa
136
                // si no => Area positiva
137
                /*if (x.length < 2) {
138
                        return 0.0;
139
                }*/
140

    
141
                //i = 0;
142
                elArea = 0.0;
143

    
144
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
145

    
146
                for (int pos = 0; pos < x.length; pos++) {
147
                        pPixel = new Point2D.Double(((Double) x[pos]).doubleValue(),
148
                                        ((Double) y[pos]).doubleValue());
149
                        p = pPixel;//vp.toMapPoint(pPixel);
150

    
151
                        // p.x = getMapContext().toMapDistance((long)((Float) poliLinesX.get(pos)).floatValue());
152
                        ///System.out.println("p.x = " + p.getX());
153
                        // p.y = getMapContext().toMapDistance((long)((Float) poliLinesY.get(pos)).floatValue());
154
                        ///System.out.println("p.y = " + p.getY());
155

    
156

    
157
                        if (pos == 0) {
158
                                pAnt.x = aux.getX();
159
                                pAnt.y = aux.getY();
160
                        }
161

    
162
                        elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY()));
163
                        pAnt.setLocation(p);
164
                }
165

    
166
                elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY()));
167
                elArea = Math.abs(elArea / 2.0);
168

    
169
                return elArea*MapContext.CHANGEM[vp.getMapUnits()];
170
        }
171

    
172
        /**
173
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
174
         */
175
        public Cursor getCursor() {
176
                return cur;
177
        }
178

    
179
        /**
180
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
181
         */
182
        public void pointFixed(MeasureEvent event) {
183
        }
184

    
185
        /**
186
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
187
         */
188
        public void polylineFinished(MeasureEvent event) {
189
        }
190

    
191
        /**
192
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
193
         */
194
        public boolean cancelDrawing() {
195
                return false;
196
        }
197
}