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 / AreaListener.java @ 40558

History | View | Annotate | Download (4.88 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

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.ui.mdiFrame.MainFrame;
31
import org.gvsig.fmap.mapcontext.MapContext;
32
import org.gvsig.fmap.mapcontext.ViewPort;
33
import org.gvsig.fmap.mapcontrol.MapControl;
34
import org.gvsig.fmap.mapcontrol.tools.AreaListenerImpl;
35
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
36

    
37

    
38

    
39
/**
40
 * <p>Listener for calculating the area of a polygon, defined in the associated {@link MapControl MapControl}
41
 *  object.</p>
42
 * <p>Moves the extent of the associated {@link MapControl MapControl} object
43
 *  according the movement between the initial and final points of the line determined by the movement
44
 *  dragged with the third button of the mouse.</p>
45
 *
46
 * <p>Updates the status bar of the main frame of the application with the current area.</p>
47
 *
48
 * @see AreaListenerImpl
49
 *
50
 * @author Vicente Caballero Navarro
51
 */
52
public class AreaListener extends AreaListenerImpl {
53
        /**
54
          * <p>Creates a new listener for calculating the area of a polygon and displaying it at the status bar
55
          *  of the main frame of the application.</p>
56
         *
57
         * @param mc the <code>MapControl</code> where is calculated the area
58
         */
59
    public AreaListener(MapControl mc) {
60
            super(mc);
61
    }
62

    
63
    /*
64
     * (non-Javadoc)
65
     * @see com.iver.cit.gvsig.fmap.tools.AreaListenerImpl#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
66
     */
67
    public void points(MeasureEvent event) {
68
        this.event = event;
69

    
70
        double dist = 0;
71
        double distAll = 0;
72

    
73
        ViewPort vp = mapCtrl.getMapContext().getViewPort();
74
        for (int i = 0; i < (event.getXs().length - 1); i++) {
75
            dist = 0;
76

    
77
            Point2D p = new Point2D.Double(event.getXs()[i].doubleValue(), event.getYs()[i].doubleValue());//vp.toMapPoint(new Point(event.getXs()[i].intValue(), event.getYs()[i].intValue()));
78
            Point2D p2 = new Point2D.Double(event.getXs()[i + 1].doubleValue(), event.getYs()[i + 1].doubleValue());//vp.toMapPoint(new Point(event.getXs()[i + 1].intValue(), event.getYs()[i + 1].intValue()));
79
            ///dist = vp.toMapDistance((int) p.distance(p2));
80
            dist = vp.distanceWorld(p,p2);
81
//            System.out.println("distancia parcial = "+dist);
82
            distAll += dist;
83
        }
84

    
85
       // System.out.println("Per?metro = " + distAll + " ?rea = " +
86
       //     (returnArea(vp.toMapPoint(
87
       //             new Point2D.Double(
88
       //                 event.getXs()[event.getXs().length - 2].doubleValue(),
89
       //                 event.getYs()[event.getYs().length - 2].doubleValue())))));
90
        NumberFormat nf = NumberFormat.getInstance();
91
        nf.setMaximumFractionDigits(2);
92
        MainFrame mF = PluginServices.getMainFrame();
93
        if (mF != null)
94
        {
95
                int distanceUnits=mapCtrl.getViewPort().getDistanceUnits();
96
                int distanceArea=mapCtrl.getViewPort().getDistanceArea();
97
            mF.getStatusBar().setMessage("4",
98
                                "P:" + nf.format(distAll/MapContext.getDistanceTrans2Meter()[distanceUnits]) + "");
99
                        mF.getStatusBar().setMessage("5",
100
                                ///"A:" + nf.format(returnArea(vp.toMapPoint(
101
                    ///        new Point2D.Double(
102
                    ///                event.getXs()[event.getXs().length - 2].doubleValue(),
103
                    ///                event.getYs()[event.getYs().length - 2].doubleValue())))/FMap.CHANGEM[mapCtrl.getViewPort().getDistanceUnits()]) + "");
104
                                        "A:" + nf.format(returnArea(
105
                                new Point2D.Double(
106
                                        event.getXs()[event.getXs().length - 2].doubleValue(),
107
                                        event.getYs()[event.getYs().length - 2].doubleValue()))/Math.pow(MapContext.getAreaTrans2Meter()[distanceArea],2)) + "");
108
                        mF.getStatusBar().setMessage("distancearea",MapContext.getAreaAbbr()[distanceArea]);
109
        }
110
    }
111
}