svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toolListeners / MeasureListener.java @ 45732
History | View | Annotate | Download (4.67 KB)
1 | 40558 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40558 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40558 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | 40558 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40558 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.app.project.documents.view.toolListeners; |
||
25 | |||
26 | import java.awt.geom.Point2D; |
||
27 | import java.text.NumberFormat; |
||
28 | 41038 | jjdelcerro | import java.util.Locale; |
29 | 40435 | jjdelcerro | |
30 | 41038 | jjdelcerro | import org.cresques.cts.IProjection; |
31 | import org.geotools.measure.AngleFormat; |
||
32 | 40435 | jjdelcerro | import org.gvsig.andami.PluginServices; |
33 | import org.gvsig.fmap.mapcontext.MapContext; |
||
34 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
35 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
36 | import org.gvsig.fmap.mapcontrol.tools.MeasureListenerImpl; |
||
37 | import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent; |
||
38 | |||
39 | |||
40 | |||
41 | /**
|
||
42 | * <p>Listener for calculating distances using vertexes of a polyline, defined in the associated {@link MapControl MapControl}
|
||
43 | * object, and displaying that information at the status bar of the application's main frame.</p>
|
||
44 | *
|
||
45 | * <p>Calculates and displays:
|
||
46 | * <ul>
|
||
47 | * <li>Distance of the last segment of the polyline.</li>
|
||
48 | * <li>Accumulated distance of all segments of the polyline.</li>
|
||
49 | * </ul>
|
||
50 | * </p>
|
||
51 | *
|
||
52 | * @see MeasureListenerImpl
|
||
53 | *
|
||
54 | * @author Vicente Caballero Navarro
|
||
55 | */
|
||
56 | public class MeasureListener extends MeasureListenerImpl { |
||
57 | /**
|
||
58 | * <p>Creates a new listener for calculating distances using points of a polyline.</p>
|
||
59 | *
|
||
60 | * @param mc the <code>MapControl</code> where is calculated the length
|
||
61 | */
|
||
62 | public MeasureListener(MapControl mc) {
|
||
63 | super(mc);
|
||
64 | } |
||
65 | |||
66 | /*
|
||
67 | * (non-Javadoc)
|
||
68 | * @see com.iver.cit.gvsig.fmap.tools.MeasureListenerImpl#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
|
||
69 | */
|
||
70 | public void points(MeasureEvent event) { |
||
71 | double dist = 0; |
||
72 | double distAll = 0; |
||
73 | 41038 | jjdelcerro | double angle = 0; |
74 | double sin = 0; |
||
75 | double cos = 0; |
||
76 | double dm = 0; |
||
77 | 40435 | jjdelcerro | |
78 | ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
||
79 | |||
80 | 41038 | jjdelcerro | IProjection proj = mapCtrl.getMapContext().getProjection(); |
81 | |||
82 | 40435 | jjdelcerro | Point2D p = new Point2D.Double(event.getXs()[0].doubleValue(),event.getYs()[0].doubleValue());//vp.toMapPoint(new Point(event.getXs()[0].intValue(),event.getYs()[0].intValue())); |
83 | for (int i = 1; i < (event.getXs().length); i++) { |
||
84 | Point2D p2 = new Point2D.Double(event.getXs()[i].doubleValue(), event.getYs()[i].doubleValue());// vp.toMapPoint(new Point(event.getXs()[i].intValue(),event.getYs()[i].intValue())); |
||
85 | dist = vp.distanceWorld(p, p2); |
||
86 | distAll += dist; |
||
87 | 41038 | jjdelcerro | |
88 | if (proj.isProjected()) {
|
||
89 | dm = p.distance(p2); |
||
90 | sin = (p2.getX() - p.getX()) / dm; |
||
91 | cos = (p2.getY() - p.getY()) / dm; |
||
92 | if (sin >= 0) { |
||
93 | angle = Math.toDegrees(Math.acos(cos)); |
||
94 | } else {
|
||
95 | angle = 360 - Math.toDegrees(Math.acos(cos)); |
||
96 | } |
||
97 | } |
||
98 | 40435 | jjdelcerro | p = p2; |
99 | } |
||
100 | |||
101 | NumberFormat nf = NumberFormat.getInstance(); |
||
102 | 41038 | jjdelcerro | AngleFormat af = AngleFormat.getInstance(Locale.getDefault());
|
103 | 40435 | jjdelcerro | nf.setMaximumFractionDigits(2);
|
104 | if (PluginServices.getMainFrame() != null) |
||
105 | { |
||
106 | double[] trans2Meter=MapContext.getDistanceTrans2Meter(); |
||
107 | 42118 | mcompany | int distanceUnits = mapCtrl.getViewPort().getDistanceUnits();
|
108 | PluginServices.getMainFrame().getStatusBar().setMessage("4",
|
||
109 | 42137 | mcompany | "Dist=" + nf.format(dist/trans2Meter[distanceUnits]) + " " + MapContext.getDistanceAbbr()[distanceUnits]); |
110 | 40435 | jjdelcerro | PluginServices.getMainFrame().getStatusBar().setMessage("5",
|
111 | 42118 | mcompany | "T=" + nf.format(distAll/trans2Meter[distanceUnits]) + " " + MapContext.getDistanceAbbr()[distanceUnits]); |
112 | 41038 | jjdelcerro | |
113 | //
|
||
114 | // El calculo del azimut esta pensado para trabajar en coordenadas proyectadas (metros)
|
||
115 | // y probablemente cuando se esten usando coordenadas en lat/lang se generen resultados
|
||
116 | // erroneos, asi que solo se calcula y muestra el azimut si la proyeccion es proyectada.
|
||
117 | //
|
||
118 | if( proj.isProjected() ) {
|
||
119 | PluginServices.getMainFrame().getStatusBar().setMessage("azimut",
|
||
120 | "Azimut:" + af.format(angle) + ""); |
||
121 | } else {
|
||
122 | PluginServices.getMainFrame().getStatusBar().setMessage("azimut",
|
||
123 | "Azimut: (not available)");
|
||
124 | } |
||
125 | 40435 | jjdelcerro | } |
126 | } |
||
127 | } |