Statistics
| Revision:

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 @ 42118

History | View | Annotate | Download (4.66 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
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
 * as published by the Free Software Foundation; either version 3
9
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.view.toolListeners;
25

    
26
import java.awt.geom.Point2D;
27
import java.text.NumberFormat;
28
import java.util.Locale;
29

    
30
import org.cresques.cts.IProjection;
31
import org.geotools.measure.AngleFormat;
32
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
                double angle = 0;
74
                double sin = 0;
75
                double cos = 0;
76
                double dm = 0;
77

    
78
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
79

    
80
                IProjection proj = mapCtrl.getMapContext().getProjection();
81
                
82
                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
                        
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
                        p = p2;
99
                }
100

    
101
                NumberFormat nf = NumberFormat.getInstance();
102
                AngleFormat af = AngleFormat.getInstance(Locale.getDefault());
103
        nf.setMaximumFractionDigits(2);
104
        if (PluginServices.getMainFrame() != null)
105
        {
106
                double[] trans2Meter=MapContext.getDistanceTrans2Meter();
107
            int distanceUnits = mapCtrl.getViewPort().getDistanceUnits();
108
                        PluginServices.getMainFrame().getStatusBar().setMessage("4",
109
                            "P=" + nf.format(dist/trans2Meter[distanceUnits]) + " " + MapContext.getDistanceAbbr()[distanceUnits]);
110
                    PluginServices.getMainFrame().getStatusBar().setMessage("5",
111
                            "T=" + nf.format(distAll/trans2Meter[distanceUnits]) + " " + MapContext.getDistanceAbbr()[distanceUnits]);
112

    
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
        }
126
        }
127
}