Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / geo / Geodetic.java @ 2

History | View | Annotate | Download (4.34 KB)

1
package org.cresques.geo;
2

    
3
import java.awt.Color;
4
import java.awt.FontMetrics;
5
import java.awt.Graphics2D;
6
import java.awt.geom.AffineTransform;
7
import java.awt.geom.NoninvertibleTransformException;
8
import java.awt.geom.Point2D;
9

    
10
import org.cresques.px.Extent;
11

    
12
public class Geodetic extends Projection {
13
        static String name = "Geodesica";
14
        static String abrev = "Geo";
15
        public static Geodetic hayford = new Geodetic(Ellipsoid.hayford);
16
        
17
        public Geodetic() {
18
                super();
19
                grid = new Graticule(this);
20
        }
21
        
22
        public Geodetic(Ellipsoid eli) {
23
                super(eli);
24
                grid = new Graticule(this);
25
        }
26
        
27
        public String getAbrev() { return abrev;}
28
        
29
        /**
30
         * 
31
         */
32
        public static Projection getProjectionByName(Ellipsoid eli, String name) {
33
                if (name.indexOf("GEO") < 0)        return null;
34
                if (name.indexOf("WGS84") >= 0) return getProjection(Ellipsoid.wgs84);
35
                if (name.indexOf("ED50") >= 0) return getProjection(Ellipsoid.wgs84);
36
                return null;
37
        }
38
        
39
        public static Geodetic getProjection(Ellipsoid eli) {
40
                if (eli == Ellipsoid.hayford)
41
                        return hayford;
42
                return new Geodetic(eli);
43
        }
44
        
45
        public Point2D createPoint(double x, double y){
46
                return new GeoPoint(this, x, y);
47
        }
48
        
49
        /**
50
         * 
51
         * @param gPt Punto para pasar a GeoPoint
52
         * @return
53
         */        
54
        public GeoPoint toGeo(Point2D gPt) {
55
                return (GeoPoint) gPt;
56
        }
57

    
58
        // Calcula el step en funci?n del zoom
59
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
60
                // calculo del step en funci?n del zoom
61
                Point2D pt1 = extent.getMin();
62

    
63
                double step = 1.0, x = (int) pt1.getX(), dist = 0.0;
64
                GeoPoint gp1, gp2;
65
                gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
66
                mat.transform(gp1, gp1);
67
                gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
68
                try {
69
                        mat.inverseTransform(gp2, gp2);
70
                } catch (NoninvertibleTransformException e) {
71
                        // TODO Auto-generated catch block
72
                        e.printStackTrace();
73
                }
74
                dist = (gp2.getX()-x);
75
                System.err.println("distX = " + dist);
76
                
77
                if (dist > 30.0) {                         step = 30.0;
78
                } else if (dist > 15.0) {         step = 15.0;
79
                } else if (dist > 10.0) {        step = 10.0;
80
                } else if (dist > 5.0) {        step = 5.0;
81
                } else if (dist > 3.0) {        step = 3.0;
82
                } else if (dist > 2.0) {        step = 2.0;
83
                } else if (dist > 1.0) {        step = 1.0;
84
                } else if (dist > .5) {                step =.5;
85
                } else if (dist > .25) {        step =.25;
86
                } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
87
                } else {                                        step = 1.0/60*2.0;
88
                }
89
                        //step = 1.0;
90
                
91
                generateGrid(g, extent, mat, step);
92
        }
93
        
94
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat, double step) {
95
                grid = new Graticule(this);
96
                GeoPoint gp1, gp2;
97
                Point2D.Double ptx = new Point2D.Double(0.0, 0.0);
98
                
99
                Point2D pt1 = extent.getMin(), pt2 = extent.getMax();
100
                System.err.println(name+": ViewPort Extent = ("+pt1+","+pt2+")");
101

    
102
                // Calculos para el texto
103
                FontMetrics fm = g.getFontMetrics();
104
                int fmWidth = 0, fmHeight = fm.getAscent();
105
                String tit = "";
106
                String fmt = "%G?%N";
107
                if (step < 1.0)
108
                        fmt = "%G?%M'%N";
109
                
110
                // Lineas Verticales
111
                double y1 = pt1.getY(), y2 = pt2.getY();
112
                double xIni = (int) pt1.getX() -1;
113
                xIni -= (xIni % step);
114
                double xFin = ((int) pt2.getX()) + 1;
115
                if (y1 < -90.0) y1 = -90.0;
116
                if (y2 > 90.0) y2 = 90.0;
117
                if (xIni < -180.0) xIni = -180.0;
118
                if (xFin > 180.0) xFin = 180.0;
119
                for (double x=xIni; x<=xFin; x+=step) {
120
                        gp1 = (GeoPoint) createPoint(x, y1);
121
                        gp2 = (GeoPoint) createPoint(x, y2);
122
                        mat.transform(gp1, gp1);
123
                        mat.transform(gp2, gp2);
124
                        grid.addLine(gp1, gp2);
125
                        tit = coordToString(x, fmt, false);
126
                        //fmWidth = fm.stringWidth(tit);
127
                        ptx.setLocation(gp2.getX()+3, gp2.getY()+fmHeight);
128
                        grid.addText(tit, ptx);
129
                }
130
                // Lineas Horizontales
131
                double x1 = pt1.getX(), x2 = pt2.getX();
132
                double yIni = (int) pt1.getY() - 1;
133
                yIni -= (yIni % step);
134
                double yFin = ((int) pt2.getY()) + 1;
135
                if (x1 < -180.0) x1 = -180.0;
136
                if (x2 > 180.0) x2 = 180.0;
137
                if (yIni < -90.0) yIni = -90.0;
138
                if (yFin > 90.0) yFin = 90.0;
139
                for (double y=yIni; y<=yFin; y+=step) {
140
                        gp1 = (GeoPoint) createPoint(x1, y);
141
                        gp2 = (GeoPoint) createPoint(x2, y);
142
                        mat.transform(gp1, gp1);
143
                        mat.transform(gp2, gp2);
144
                        grid.addLine(gp1, gp2);
145
                        tit = coordToString(y, fmt, true);
146
                        //fmWidth = fm.stringWidth(tit);
147
                        ptx.setLocation(gp1.getX()+3, gp1.getY()-2);
148
                        grid.addText(tit, ptx);
149
                }
150
        }
151

    
152
        public void drawGrid(Graphics2D g, ViewPort vp) {
153
                generateGrid(g, vp.getExtent(), vp.getMat());
154
                grid.setColor(gridColor);
155
                grid.draw(g, vp);
156
        }
157
}