Statistics
| Revision:

gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.extension / src / main / java / org / gvsig / geoprocess / core / MapTools.java @ 172

History | View | Annotate | Download (3.89 KB)

1
/*
2
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2010 Generalitat Valenciana.
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 2
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 */
21
package org.gvsig.geoprocess.core;
22

    
23
import org.cresques.cts.IProjection;
24

    
25
import org.gvsig.andami.PluginServices;
26
import org.gvsig.andami.ui.mdiManager.IWindow;
27
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
28
import org.gvsig.fmap.mapcontext.MapContext;
29

    
30

    
31
/**
32
 * Map utilities
33
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
34
 */
35
public class MapTools {
36
        public static final double EARTH_RADIUS = 6378137d;
37
        
38
        /**
39
         * For computing with geodetic coordinates:
40
         * returns the angular measure (in radians)
41
         * for a distance over the earth along a
42
         * meridiam.
43
         * Because this consideration is an approximation,
44
         * we consideer the eart like an sphere (not an
45
         * ellipsoid)
46
         * @param dist distance in meters
47
         * @return arc of meridian whose length is the specified
48
         * distance
49
         */
50
        public static double toSexaAngularMeasure(double dist) {
51
                /*
52
                 *  dist = 6378km(terrestrial radius) -> 2PI
53
                 *  passed distance -> incognite
54
                 */
55
                return Math.toDegrees((2 * Math.PI * dist) / EARTH_RADIUS);
56
        }
57

    
58
        /**
59
         * Converts a distance entered by user in the GUI in the same distance
60
         * in internal units (measure units)
61
         *
62
         * */
63
        public static double getInInternalUnits(double userEntryDistance,
64
                                                                                                        IProjection proj,
65
                                                                                                        int distanceUnits,
66
                                                                                                        int mapUnits) {
67
                double[] trans2Meter = MapContext.getDistanceTrans2Meter();
68
                double distInInternalUnits = (userEntryDistance/trans2Meter[mapUnits]) *
69
                                                                trans2Meter[distanceUnits];
70

    
71
                if( (proj != null) && !(proj.isProjected())) 
72
                        distInInternalUnits = toSexaAngularMeasure(distInInternalUnits);
73
                
74
                return distInInternalUnits;
75
        }
76
        
77
        /**
78
         * <p>Gets the measurement unit used by this view port for the map.</p>
79
         *
80
         * @return Returns the current map measure unit
81
         */
82
        public static int getMapUnits() {
83
                IWindow w = PluginServices.getMDIManager().getActiveWindow();
84
                if(!(w instanceof AbstractViewPanel)) {
85
                        IWindow[] wList = PluginServices.getMDIManager().getAllWindows();
86
                        for (int i = 0; i < wList.length; i++) {
87
                                if(wList[i] instanceof AbstractViewPanel)
88
                                        w = (IWindow)wList[i];
89
                        }
90
                }
91

    
92
                if(w != null && w instanceof AbstractViewPanel) {
93
                        IProjection proj = ((AbstractViewPanel)w).getMapControl().getViewPort().getProjection();
94
                        if(proj.isProjected())
95
                                return ((AbstractViewPanel)w).getMapControl().getViewPort().getMapUnits();
96
                } 
97
                return 1;
98
        }
99
        
100
        /**
101
         * <p>Returns the measurement unit of this view port used for measuring distances and displaying information.</p>
102
         *
103
         * @return the measurement unit of this view used for measuring distances and displaying information
104
         */
105
        public static int getDistanceUnits() {
106
                IWindow w = PluginServices.getMDIManager().getActiveWindow();
107
                if(!(w instanceof AbstractViewPanel)) {
108
                        IWindow[] wList = PluginServices.getMDIManager().getAllWindows();
109
                        for (int i = 0; i < wList.length; i++) {
110
                                if(wList[i] instanceof AbstractViewPanel)
111
                                        w = (IWindow)wList[i];
112
                        }
113
                }
114
                
115
                if(w != null && w instanceof AbstractViewPanel)
116
                        return ((AbstractViewPanel)w).getMapControl().getViewPort().getDistanceUnits();
117
                else 
118
                        return 1;
119
        }
120
        
121
}