Revision 20098 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/tools/AreaListenerImpl.java

View differences:

AreaListenerImpl.java
61 61

  
62 62

  
63 63
/**
64
 * Implementaci?n de la interfaz MeasureListener como herramienta para medir el
65
 * ?rea.
64
 * <p>Listener for calculating the area of a polygon, defined in the associated {@link MapControl MapControl}
65
 *  object.</p>
66
 * 
67
 * <p>If the view port of the associated <code>MapControl</code> isn't projected gets the area according the
68
 *  geographical coordinates.</p>
66 69
 *
67 70
 * @author Vicente Caballero Navarro
68 71
 */
69 72
public class AreaListenerImpl implements PolylineListener {
73
	/**
74
	 * The image to display when the cursor is active.
75
	 */
70 76
	private final Image iarea = new ImageIcon(MapControl.class.getResource(
71 77
				"images/AreaCursor.gif")).getImage();
78

  
79
	/**
80
	 * The cursor used to work with this tool listener.
81
	 * 
82
	 * @see #getCursor()
83
	 */
72 84
	private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(iarea,
73 85
			new Point(16, 16), "");
86

  
87
	/**
88
	 * Reference to the <code>MapControl</code> object that uses.
89
	 */
74 90
	protected MapControl mapCtrl;
91

  
92
	/**
93
	 * Information about all vertexes and {@link GeneralPathX GeneralPathX}s of the polyline. 
94
	 */
75 95
	protected MeasureEvent event;
76 96

  
77 97
	/**
78
	 * Crea un nuevo AreaListenerImpl.
98
 	 * <p>Creates a new listener for calculating the area of a polygon.</p>
79 99
	 *
80
	 * @param mc MapControl.
100
	 * @param mc the <code>MapControl</code> where is calculated the area
81 101
	 */
82 102
	public AreaListenerImpl(MapControl mc) {
83 103
		this.mapCtrl = mc;
84 104
	}
85 105

  
86
	/**
106
	/*
107
	 * (non-Javadoc)
87 108
	 * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
88 109
	 */
89 110
	public void points(MeasureEvent event) {
......
113 134
						event.getXs()[event.getXs().length - 2].doubleValue(),
114 135
						event.getYs()[event.getYs().length - 2].doubleValue())))));
115 136
	}
137

  
116 138
	/**
117
	 * Calcula el ?rea de un pol?gono simple, no de multipol?gonos,
118
	 * para estos ?ltimos se tendr? que dividir en poligonos simples
119
	 * y sumar los resultados.
139
	 * <p>Returns the area of the polygon, using {@link #returnCoordsArea(Double[], Double[], Point2D) returnCoordsArea}
140
	 *  if the <code>ViewPort</code> of the associated <code>MapControl</code> is projected, or using
141
	 *  {@link #returnGeoCArea(Double[], Double[], Point2D) returnGeoCArea} if isn't.</p>
120 142
	 *
121
	 * @param point ?ltimo punto.
143
	 * @param point unused parameter
122 144
	 *
123
	 * @return ?rea.
145
	 * @return area from the vertexes stored at the measure event in real coordinates, or, if the <code>MapControl</code>
146
	 *  isn't projected, in geographical coordinates
147
	 * 
148
	 * @see #returnCoordsArea(Double[], Double[], Point2D)
149
	 * @see #returnGeoCArea(Double[], Double[], Point2D)
124 150
	 */
125 151
	protected double returnArea(Point2D point) {
126 152
		Double[] xs=event.getXs();
......
132 158
	}
133 159

  
134 160
	/**
135
	 * Calcula el ?rea de un pol?gono simple de una proyecci?n, no de multipol?gonos,
136
	 * para estos ?ltimos se tendr? que dividir en poligonos simples
137
	 * y sumar los resultados.
161
	 * <p>Returns the area of the polygon using <i>point</i> as initial, in
162
	 *  real values with the current measure unit, according the projection in the <code>ViewPort</code>
163
	 *  of the <code>MapControl</code>.</p>
138 164
	 *
139
	 * @param aux ?ltimo punto.
165
	 * @param xs abscissa coordinate of all vertexes of the polygon
166
	 * @param ys ordinate coordinate of all vertexes of the polygon
167
	 * @param point point 2D used as first vertex in the calculation of the area
140 168
	 *
141
	 * @return ?rea.
169
	 * @return the area of the polygon
142 170
	 */
143 171
	public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) {
144 172
		Point2D aux=point;
......
163 191
		elArea = Math.abs(elArea / 2.0);
164 192
		return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2)));
165 193
	}
166
public static void main(String[] args) {
167
	IProjection projectionUTM = CRSFactory.getCRS("EPSG:23030");
168
	ViewPort vpUTM = new ViewPort(projectionUTM);
169
	MapControl mcUTM=new MapControl();
170
	mcUTM.setMapContext(new MapContext(vpUTM));
171
	AreaListenerImpl areaListenerUTM=new AreaListenerImpl(mcUTM);
172
	IProjection projectionGeo = CRSFactory.getCRS("EPSG:4230");
173
	ViewPort vpGeo = new ViewPort(projectionGeo);
174
	MapControl mcGeo=new MapControl();
175
	mcGeo.setMapContext(new MapContext(vpGeo));
176
	AreaListenerImpl areaListenerGeo=new AreaListenerImpl(mcGeo);
194
	public static void main(String[] args) {
195
		IProjection projectionUTM = CRSFactory.getCRS("EPSG:23030");
196
		ViewPort vpUTM = new ViewPort(projectionUTM);
197
		MapControl mcUTM=new MapControl();
198
		mcUTM.setMapContext(new MapContext(vpUTM));
199
		AreaListenerImpl areaListenerUTM=new AreaListenerImpl(mcUTM);
200
		IProjection projectionGeo = CRSFactory.getCRS("EPSG:4230");
201
		ViewPort vpGeo = new ViewPort(projectionGeo);
202
		MapControl mcGeo=new MapControl();
203
		mcGeo.setMapContext(new MapContext(vpGeo));
204
		AreaListenerImpl areaListenerGeo=new AreaListenerImpl(mcGeo);
205
	
206
		Double[] xsUTMCaseta=new Double[] {new Double(547508.77),new Double(547517.73),new Double(547512.65)};
207
		Double[] ysUTMCaseta=new Double[] {new Double(4704333.97),new Double(4704331.3),new Double(4704315.2)};
208
		double areaUTMCaseta=areaListenerUTM.returnCoordsArea(xsUTMCaseta,ysUTMCaseta,new Point2D.Double(547512.65,4704315.2));
209
		Double[] xsGeoCaseta=new Double[] {new Double(-2.42192383),new Double(-2.42181545),new Double(-2.42187771)};
210
		Double[] ysGeoCaseta=new Double[] {new Double(42.48914909),new Double(42.48912295),new Double(42.48897922)};
211
		double areaGeoCCaseta=areaListenerGeo.returnGeoCArea(xsGeoCaseta,ysGeoCaseta,new Point2D.Double(-2.42187771,42.48897922));
212
	
213
		System.out.println("AreaUTMCaseta = "+ areaUTMCaseta);
214
		System.out.println("AreaGeoCCaseta = "+ areaGeoCCaseta);
215
	
216
	
217
		Double[] xsUTM=new Double[] {new Double(731292),new Double(731901),new Double(730138)};
218
		Double[] ysUTM=new Double[] {new Double(4351223),new Double(4350768),new Double(4349232)};
219
		double areaUTM=areaListenerUTM.returnCoordsArea(xsUTM,ysUTM,new Point2D.Double(730138,4349232));
220
		Double[] xsGeo=new Double[] {new Double(-0.31888183),new Double(-0.31173131),new Double(-0.33268401)};
221
		Double[] ysGeo=new Double[] {new Double(39.27871741),new Double(39.27464327),new Double(39.26117368)};
222
		double areaGeoC=areaListenerGeo.returnGeoCArea(xsGeo,ysGeo,new Point2D.Double(-0.33268401,39.26117368));
223
	
224
		System.out.println("AreaUTM = "+ areaUTM);
225
		System.out.println("AreaGeoC = "+ areaGeoC);
226
	
227
		Double[] xsUTMspain=new Double[] {new Double(-12806),new Double(1025790),new Double(-31353.14)};
228
		Double[] ysUTMspain=new Double[] {new Double(4793276.43),new Double(4719090.94),new Double(4125607.02)};
229
		double areaUTMspain=areaListenerUTM.returnCoordsArea(xsUTMspain,ysUTMspain,new Point2D.Double(730138,4349232));
230
		Double[] xsGeospain=new Double[] {new Double(-9.22743872),new Double(3.33087936),new Double(-9.01458587),new Double(-9.22743872)};
231
		Double[] ysGeospain=new Double[] {new Double(43.02384666),new Double(42.38528811),new Double(37.06396689),new Double(43.02384666)};
232
		double areaGeospainC=areaListenerGeo.returnGeoCArea(xsGeospain,ysGeospain,new Point2D.Double(-9.01458587,37.06396689));
233
	
234
		System.out.println("AreaUTMSpain = "+ areaUTMspain);
235
		System.out.println("AreaGeoSpainC = "+ areaGeospainC);
236
	}
177 237

  
178
	Double[] xsUTMCaseta=new Double[] {new Double(547508.77),new Double(547517.73),new Double(547512.65)};
179
	Double[] ysUTMCaseta=new Double[] {new Double(4704333.97),new Double(4704331.3),new Double(4704315.2)};
180
	double areaUTMCaseta=areaListenerUTM.returnCoordsArea(xsUTMCaseta,ysUTMCaseta,new Point2D.Double(547512.65,4704315.2));
181
	Double[] xsGeoCaseta=new Double[] {new Double(-2.42192383),new Double(-2.42181545),new Double(-2.42187771)};
182
	Double[] ysGeoCaseta=new Double[] {new Double(42.48914909),new Double(42.48912295),new Double(42.48897922)};
183
	double areaGeoCCaseta=areaListenerGeo.returnGeoCArea(xsGeoCaseta,ysGeoCaseta,new Point2D.Double(-2.42187771,42.48897922));
184

  
185
	System.out.println("AreaUTMCaseta = "+ areaUTMCaseta);
186
	System.out.println("AreaGeoCCaseta = "+ areaGeoCCaseta);
187

  
188

  
189
	Double[] xsUTM=new Double[] {new Double(731292),new Double(731901),new Double(730138)};
190
	Double[] ysUTM=new Double[] {new Double(4351223),new Double(4350768),new Double(4349232)};
191
	double areaUTM=areaListenerUTM.returnCoordsArea(xsUTM,ysUTM,new Point2D.Double(730138,4349232));
192
	Double[] xsGeo=new Double[] {new Double(-0.31888183),new Double(-0.31173131),new Double(-0.33268401)};
193
	Double[] ysGeo=new Double[] {new Double(39.27871741),new Double(39.27464327),new Double(39.26117368)};
194
	double areaGeoC=areaListenerGeo.returnGeoCArea(xsGeo,ysGeo,new Point2D.Double(-0.33268401,39.26117368));
195

  
196
	System.out.println("AreaUTM = "+ areaUTM);
197
	System.out.println("AreaGeoC = "+ areaGeoC);
198

  
199
	Double[] xsUTMspain=new Double[] {new Double(-12806),new Double(1025790),new Double(-31353.14)};
200
	Double[] ysUTMspain=new Double[] {new Double(4793276.43),new Double(4719090.94),new Double(4125607.02)};
201
	double areaUTMspain=areaListenerUTM.returnCoordsArea(xsUTMspain,ysUTMspain,new Point2D.Double(730138,4349232));
202
	Double[] xsGeospain=new Double[] {new Double(-9.22743872),new Double(3.33087936),new Double(-9.01458587),new Double(-9.22743872)};
203
	Double[] ysGeospain=new Double[] {new Double(43.02384666),new Double(42.38528811),new Double(37.06396689),new Double(43.02384666)};
204
	double areaGeospainC=areaListenerGeo.returnGeoCArea(xsGeospain,ysGeospain,new Point2D.Double(-9.01458587,37.06396689));
205

  
206
	System.out.println("AreaUTMSpain = "+ areaUTMspain);
207
	System.out.println("AreaGeoSpainC = "+ areaGeospainC);
208
}
209

  
210 238
	/**
211
	 * Calcula el ?rea de un pol?gono simple en geod?sicas, no de
212
	 * multipol?gonos, para estos ?ltimos se tendr? que dividir en poligonos
213
	 * simples y sumar los resultados.
214
	 *
215
	 * @param aux
216
	 *            ?ltimo punto.
217
	 *
218
	 * @return ?rea.
239
	 * <p>Returns the area in geographical coordinates of the polygon, according the
240
	 *  <a href="http://en.wikipedia.org/wiki/Haversine_formula">Haversine function</a>.</p> 
241
	 * 
242
	 * @see Geo#sphericalPolyArea(double[], double[], int)
219 243
	 */
220 244
	public double returnGeoCArea(Double[] xs, Double[] ys, Point2D point) {
221 245
		double[] lat = new double[xs.length];
......
226 250
		}
227 251
		return (Geo.sphericalPolyArea(lat, lon, xs.length - 1) * Geo.SqM);// /1.29132441;//Esto
228 252
	}
229
	/**
253

  
254
	/*
255
	 * (non-Javadoc)
230 256
	 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
231 257
	 */
232 258
	public Cursor getCursor() {
233 259
		return cur;
234 260
	}
235 261

  
236
	/**
262
	/*
263
	 * (non-Javadoc)
237 264
	 * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#pointFixed(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
238 265
	 */
239 266
	public void pointFixed(MeasureEvent event) {
240 267
	}
241 268

  
242
	/**
269
	/*
270
	 * (non-Javadoc)
243 271
	 * @see com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener#polylineFinished(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
244 272
	 */
245 273
	public void polylineFinished(MeasureEvent event) {
246 274
	}
247 275

  
248
	/**
276
	/*
277
	 * (non-Javadoc)
249 278
	 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
250 279
	 */
251 280
	public boolean cancelDrawing() {

Also available in: Unified diff