Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 1100

History | View | Annotate | Download (5.36 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.NewMapControl;
44
import com.iver.cit.gvsig.fmap.ViewPort;
45
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent;
46
import com.iver.cit.gvsig.fmap.tools.Listeners.MeasureListener;
47

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

    
54
import javax.swing.ImageIcon;
55

    
56

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

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

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

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

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

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

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

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

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

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

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

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

    
140
                i = 0;
141
                elArea = 0.0;
142

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

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

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

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

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

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

    
168
                return elArea;
169
        }
170

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

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

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

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