svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / AreaListenerImpl.java @ 9558
History | View | Annotate | Download (5.37 KB)
1 | 1100 | fjp | /* 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 | 288 | vcaballero | package com.iver.cit.gvsig.fmap.tools; |
42 | |||
43 | 1223 | fernando | import com.iver.cit.gvsig.fmap.MapControl; |
44 | 1050 | vcaballero | import com.iver.cit.gvsig.fmap.ViewPort; |
45 | import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent; |
||
46 | 1221 | fernando | import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener; |
47 | 1050 | vcaballero | |
48 | 291 | vcaballero | import java.awt.Cursor; |
49 | import java.awt.Image; |
||
50 | 288 | vcaballero | import java.awt.Point; |
51 | 291 | vcaballero | import java.awt.Toolkit; |
52 | 288 | vcaballero | import java.awt.geom.Point2D; |
53 | |||
54 | 291 | vcaballero | import javax.swing.ImageIcon; |
55 | 288 | vcaballero | |
56 | 291 | vcaballero | |
57 | 288 | vcaballero | /**
|
58 | 1050 | vcaballero | * Implementaci?n de la interfaz MeasureListener como herramienta para medir el
|
59 | * ?rea.
|
||
60 | 288 | vcaballero | *
|
61 | * @author Vicente Caballero Navarro
|
||
62 | */
|
||
63 | 1221 | fernando | public class AreaListenerImpl implements PolylineListener { |
64 | 1223 | fernando | private final Image iarea = new ImageIcon(MapControl.class.getResource( |
65 | 1050 | vcaballero | "images/AreaCursor.gif")).getImage();
|
66 | 291 | vcaballero | private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea, |
67 | 1050 | vcaballero | new Point(16, 16), ""); |
68 | 1223 | fernando | protected MapControl mapCtrl;
|
69 | 1050 | vcaballero | protected MeasureEvent event;
|
70 | 288 | vcaballero | |
71 | 1050 | vcaballero | /**
|
72 | * Crea un nuevo AreaListenerImpl.
|
||
73 | *
|
||
74 | * @param mc MapControl.
|
||
75 | */
|
||
76 | 1223 | fernando | public AreaListenerImpl(MapControl mc) {
|
77 | 1050 | vcaballero | this.mapCtrl = mc;
|
78 | } |
||
79 | 288 | vcaballero | |
80 | 1050 | vcaballero | /**
|
81 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
82 | 1050 | vcaballero | */
|
83 | public void points(MeasureEvent event) { |
||
84 | this.event = event;
|
||
85 | 288 | vcaballero | |
86 | 1050 | vcaballero | double dist = 0; |
87 | double distAll = 0; |
||
88 | 288 | vcaballero | |
89 | 1050 | vcaballero | ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
90 | 288 | vcaballero | |
91 | 1050 | vcaballero | for (int i = 0; i < (event.getXs().length - 1); i++) { |
92 | dist = 0;
|
||
93 | 288 | vcaballero | |
94 | 1050 | vcaballero | 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 | 288 | vcaballero | |
99 | 1050 | vcaballero | ///dist = vp.toMapDistance((int) p.distance(p2));
|
100 | dist = vp.distanceWorld(p, p2); |
||
101 | distAll += dist; |
||
102 | } |
||
103 | 288 | vcaballero | |
104 | 1050 | vcaballero | 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 | 288 | vcaballero | |
111 | 1050 | vcaballero | /**
|
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 | 288 | vcaballero | |
121 | 1050 | vcaballero | double elArea;
|
122 | 4226 | caballero | //int pos=0;
|
123 | 1050 | vcaballero | Point2D pPixel;
|
124 | Point2D p = new Point2D.Double(); |
||
125 | Point2D.Double pAnt = new Point2D.Double(); |
||
126 | 4226 | caballero | //long i;
|
127 | 1050 | vcaballero | Double[] x = event.getXs(); |
128 | Double[] y = event.getYs(); |
||
129 | 288 | vcaballero | |
130 | 1050 | vcaballero | // 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 | 4226 | caballero | /*if (x.length < 2) {
|
137 | 1050 | vcaballero | return 0.0;
|
138 | 4226 | caballero | }*/
|
139 | 288 | vcaballero | |
140 | 4226 | caballero | //i = 0;
|
141 | 1050 | vcaballero | elArea = 0.0;
|
142 | 288 | vcaballero | |
143 | 1050 | vcaballero | ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
144 | 288 | vcaballero | |
145 | 4226 | caballero | for (int pos = 0; pos < x.length; pos++) { |
146 | 1050 | vcaballero | pPixel = new Point2D.Double(((Double) x[pos]).doubleValue(), |
147 | ((Double) y[pos]).doubleValue());
|
||
148 | 4226 | caballero | p = pPixel;//vp.toMapPoint(pPixel);
|
149 | 288 | vcaballero | |
150 | 1050 | vcaballero | // 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 | 288 | vcaballero | |
155 | 4226 | caballero | |
156 | if (pos == 0) { |
||
157 | 1050 | vcaballero | pAnt.x = aux.getX(); |
158 | pAnt.y = aux.getY(); |
||
159 | } |
||
160 | 288 | vcaballero | |
161 | 1050 | vcaballero | elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
162 | pAnt.setLocation(p); |
||
163 | } |
||
164 | 291 | vcaballero | |
165 | 1050 | vcaballero | elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
166 | elArea = Math.abs(elArea / 2.0); |
||
167 | |||
168 | return elArea;
|
||
169 | } |
||
170 | |||
171 | 291 | vcaballero | /**
|
172 | * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
||
173 | */
|
||
174 | public Cursor getCursor() { |
||
175 | return cur;
|
||
176 | } |
||
177 | 363 | fernando | |
178 | /**
|
||
179 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
180 | 363 | fernando | */
|
181 | public void pointFixed(MeasureEvent event) { |
||
182 | } |
||
183 | |||
184 | /**
|
||
185 | 1221 | fernando | * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
186 | 363 | fernando | */
|
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 | 291 | vcaballero | } |