Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / geo / Geodetic.java @ 2312

History | View | Annotate | Download (5.97 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
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
 * For more information, contact:
21
 * 
22
 * cresques@gmail.com
23
 */
24
package org.cresques.geo;
25

    
26
import java.awt.FontMetrics;
27
import java.awt.Graphics2D;
28
import java.awt.geom.AffineTransform;
29
import java.awt.geom.NoninvertibleTransformException;
30
import java.awt.geom.Point2D;
31
import java.util.TreeMap;
32

    
33
import org.cresques.cts.IDatum;
34
import org.cresques.cts.IProjection;
35
import org.cresques.px.Extent;
36

    
37
public class Geodetic extends Projection {
38
        private static TreeMap projList = new TreeMap();
39
        static String name = "Geodesica";
40
        static String abrev = "Geo";
41
        public static Geodetic hayford = new Geodetic(Ellipsoid.hayford);
42
        
43
        public Geodetic() {
44
                super();
45
                grid = new Graticule(this);
46
        }
47
        
48
        public Geodetic(Ellipsoid eli) {
49
                super(eli);
50
                grid = new Graticule(this);
51
        }
52
        
53
        public String getAbrev() { return abrev;}
54
        
55
        /**
56
         * 
57
         */
58
        public static IProjection getProjectionByName(IDatum eli, String name) {
59
                if (name.indexOf("GEO") < 0)        return null;
60
                if (name.indexOf("WGS84") >= 0) return getProjection(Ellipsoid.wgs84);
61
                if (name.indexOf("ED50") >= 0) return getProjection(Ellipsoid.wgs84);
62
                return null;
63
        }
64
        
65
        public static Geodetic getProjection(Ellipsoid eli) {
66
                Geodetic ret = null;
67
                String key = eli.toString();
68
                if (Geodetic.projList.containsKey(key))
69
                        ret = (Geodetic) Geodetic.projList.get(key);
70
                else {
71
                        if (eli == Ellipsoid.hayford)
72
                                ret = hayford;
73
                        else
74
                                ret = new Geodetic(eli);
75
                        Geodetic.projList.put(key, ret);
76
                }
77
                return ret;
78
        }
79
        
80
        public Point2D createPoint(double x, double y){
81
                return new GeoPoint(this, x, y);
82
        }
83
        
84
        /**
85
         * 
86
         * @param gPt Punto para pasar a GeoPoint
87
         * @return
88
         */        
89
        
90
        public Point2D toGeo(Point2D gPt) { return (GeoPoint) gPt;        }
91
        
92
        public Point2D fromGeo(Point2D gPt, Point2D pPt) { return gPt; }
93

    
94
        // Calcula el step en funci?n del zoom
95
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
96
                // calculo del step en funci?n del zoom
97
                Point2D pt1 = extent.getMin();
98

    
99
                double step = 1.0, x = (int) pt1.getX(), dist = 0.0;
100
                GeoPoint gp1, gp2;
101
                gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
102
                mat.transform(gp1, gp1);
103
                gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
104
                try {
105
                        mat.inverseTransform(gp2, gp2);
106
                } catch (NoninvertibleTransformException e) {
107
                        // TODO Auto-generated catch block
108
                        e.printStackTrace();
109
                }
110
                dist = (gp2.getX()-x);
111
                System.err.println("distX = " + dist);
112
                
113
                if (dist > 30.0) {                         step = 30.0;
114
                } else if (dist > 15.0) {         step = 15.0;
115
                } else if (dist > 10.0) {        step = 10.0;
116
                } else if (dist > 5.0) {        step = 5.0;
117
                } else if (dist > 3.0) {        step = 3.0;
118
                } else if (dist > 2.0) {        step = 2.0;
119
                } else if (dist > 1.0) {        step = 1.0;
120
                } else if (dist > .5) {                step =.5;
121
                } else if (dist > .25) {        step =.25;
122
                } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
123
                } else {                                        step = 1.0/60*2.0;
124
                }
125
                        //step = 1.0;
126
                
127
                generateGrid(g, extent, mat, step);
128
        }
129
        
130
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat, double step) {
131
                grid = new Graticule(this);
132
                GeoPoint gp1, gp2;
133
                Point2D.Double ptx = new Point2D.Double(0.0, 0.0);
134
                
135
                Point2D pt1 = extent.getMin(), pt2 = extent.getMax();
136
                System.err.println(name+": ViewPort Extent = ("+pt1+","+pt2+")");
137

    
138
                // Calculos para el texto
139
                FontMetrics fm = g.getFontMetrics();
140
                int fmWidth = 0, fmHeight = fm.getAscent();
141
                String tit = "";
142
                String fmt = "%G?%N";
143
                if (step < 1.0)
144
                        fmt = "%G?%M'%N";
145
                
146
                // Lineas Verticales
147
                double y1 = pt1.getY(), y2 = pt2.getY();
148
                double xIni = (int) pt1.getX() -1;
149
                xIni -= (xIni % step);
150
                double xFin = ((int) pt2.getX()) + 1;
151
                if (y1 < -90.0) y1 = -90.0;
152
                if (y2 > 90.0) y2 = 90.0;
153
                if (xIni < -180.0) xIni = -180.0;
154
                if (xFin > 180.0) xFin = 180.0;
155
                for (double x=xIni; x<=xFin; x+=step) {
156
                        gp1 = (GeoPoint) createPoint(x, y1);
157
                        gp2 = (GeoPoint) createPoint(x, y2);
158
                        mat.transform(gp1, gp1);
159
                        mat.transform(gp2, gp2);
160
                        grid.addLine(gp1, gp2);
161
                        tit = coordToString(x, fmt, false);
162
                        //fmWidth = fm.stringWidth(tit);
163
                        ptx.setLocation(gp2.getX()+3, gp2.getY()+fmHeight);
164
                        grid.addText(tit, ptx);
165
                }
166
                // Lineas Horizontales
167
                double x1 = pt1.getX(), x2 = pt2.getX();
168
                double yIni = (int) pt1.getY() - 1;
169
                yIni -= (yIni % step);
170
                double yFin = ((int) pt2.getY()) + 1;
171
                if (x1 < -180.0) x1 = -180.0;
172
                if (x2 > 180.0) x2 = 180.0;
173
                if (yIni < -90.0) yIni = -90.0;
174
                if (yFin > 90.0) yFin = 90.0;
175
                for (double y=yIni; y<=yFin; y+=step) {
176
                        gp1 = (GeoPoint) createPoint(x1, y);
177
                        gp2 = (GeoPoint) createPoint(x2, y);
178
                        mat.transform(gp1, gp1);
179
                        mat.transform(gp2, gp2);
180
                        grid.addLine(gp1, gp2);
181
                        tit = coordToString(y, fmt, true);
182
                        //fmWidth = fm.stringWidth(tit);
183
                        ptx.setLocation(gp1.getX()+3, gp1.getY()-2);
184
                        grid.addText(tit, ptx);
185
                }
186
        }
187

    
188
        public void drawGrid(Graphics2D g, ViewPortData vp) {
189
                generateGrid(g, vp.getExtent(), vp.getMat());
190
                grid.setColor(gridColor);
191
                grid.draw(g, vp);
192
        }
193

    
194
        /* (non-Javadoc)
195
         * @see org.cresques.cts.IProjection#getScale(double, double, double, double)
196
         */
197
        public double getScale(double minX, double maxX, double width, double dpi) {
198
                double scale = (maxX-minX)*                // grados
199
                // 1852.0 metros x minuto de meridiano
200
                (dpi / 2.54 * 100.0 * 1852.0 * 60.0)/        // px / metro
201
                width;                                                                // pixels
202
                return scale;
203
        }
204
}