Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / geo / Mercator.java @ 9

History | View | Annotate | Download (6.03 KB)

1
/*
2
 * Created on 01-may-2004
3
 *
4
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
5
 */
6
package org.cresques.geo;
7

    
8
import java.awt.FontMetrics;
9
import java.awt.Graphics2D;
10
import java.awt.geom.AffineTransform;
11
import java.awt.geom.Point2D;
12

    
13
import org.cresques.px.Extent;
14

    
15
/**
16
 * Proyeccion Mercator
17
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>* @author administrador
18
 */
19
public class Mercator  extends Projection {
20
        static String name = "Mercator";
21
        static String abrev = "Mer";
22

    
23
        private double a, f, b, Eps2, EE2, EE3, Epps2;
24
        
25
        public Mercator(Ellipsoid eli) {
26
                super(eli);
27
                grid = new Graticule(this);
28
                double [] p = eli.getParam();
29
                a      =p[1];
30
                f      =1/p[2];
31
                b      =p[3];
32

    
33
                Eps2   =p[5];
34
                EE2    =Eps2*Eps2;
35
                EE3    =EE2*Eps2;
36
                Epps2  =p[7];
37
        }
38

    
39
        public String getAbrev() { return abrev;}
40

    
41
        public static Mercator getProjection(Ellipsoid eli) {
42
                return new Mercator(eli);
43
        }
44
        
45
        /**
46
         * 
47
         */
48
        public static Projection getProjectionByName(Ellipsoid eli, String name) {
49
                if (name.indexOf("Me") < 0)
50
                        return null;
51
                return getProjection(eli);
52
        }
53
        /**
54
         * 
55
         */        
56
        public Point2D createPoint(double x, double y){
57
                return new ProjPoint(this, x, y);
58
        }
59

    
60
        /**
61
         * 
62
         * @param uPt
63
         * @return
64
         */        
65
        public GeoPoint toGeo(Point2D mPt) {
66
                GeoPoint gPt = new GeoPoint();
67
                return toGeo((ProjPoint) mPt, gPt);
68
        }
69
        /**
70
         * 
71
         * @param mPt
72
         * @param gPt
73
         * @return
74
         */
75
                
76
        public GeoPoint toGeo(ProjPoint mPt, GeoPoint gPt) {
77
                double t = Math.pow(Math.E,(-mPt.getY()/a));
78
                
79
                double x1, x=(Math.PI/2-2*Math.atan(t));
80
                do {
81
                        x1 =x;
82
                        x = Math.PI/2-2*Math.atan(t*(Math.pow((1-Math.sqrt(Eps2)*Math.sin(x))/
83
                                (1+Math.sqrt(Eps2)*Math.sin(x)),(Math.sqrt(Eps2)/2))));
84
                } while ((x-x1)>0.0000000001);
85
                double lat = Math.PI / 2-2 * Math.atan(t*(Math.pow((1-Math.sqrt(Eps2)*Math.sin(x))/
86
                        (1+Math.sqrt(Eps2)*Math.sin(x)),(Math.sqrt(Eps2)/2))));
87

    
88
                double lng = mPt.getX()/a;
89
                gPt.setLocation(lng*180.0/Math.PI, lat*180.0/Math.PI);
90
                gPt.proj = Geodetic.getProjection(mPt.proj.eli);
91
                return gPt;
92
        }
93

    
94
        /**
95
         * 
96
         * @param gPt
97
         * @param uPt
98
         * @return
99
         */        
100
        public Point2D fromGeo(GeoPoint gPt, ProjPoint mPt) {
101
                 double sl = Math.sin(gPt.Latitude.ToRadians());
102
                double cl = Math.cos(gPt.Latitude.ToRadians());
103
                double tl =(1+sl)/(1-sl);
104
                // Calcula Easting
105
                double x = a * gPt.Longitude.ToRadians();
106
                // Calcula Northing
107
                double y=Math.pow(((1-Math.sqrt(Eps2)*sl)/(1+Math.sqrt(Eps2)*sl)),(Math.sqrt(Eps2)));
108
                y= a/2 *(Math.log(tl*y));
109
                mPt.setLocation(x, y);
110
                mPt.proj = this;
111
                 return mPt;
112
        }
113
        
114
        // Calcula el step en funci?n del zoom
115
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
116
                // calculo del step en funci?n del zoom
117
                Point2D pt1 = extent.getMin();
118

    
119
                double step = 3.0, x = pt1.getX(), dist = 0.0;
120
                ProjPoint ppt1, ppt2;
121
                GeoPoint gp1, gp2;
122
                ppt1 = (ProjPoint) createPoint( x, pt1.getY());
123
                ppt2 = (ProjPoint) createPoint(x+100, pt1.getY()-100);
124
                gp1 = ppt1.toGeo();
125
                gp2 = ppt2.toGeo();
126
                
127
        /*        GeoPoint gp1, gp2;
128
                gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
129
                mat.transform(gp1, gp1);
130
                gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
131
                try {
132
                        mat.inverseTransform(gp2, gp2);
133
                } catch (NoninvertibleTransformException e) {
134
                        // TODO Auto-generated catch block
135
                        e.printStackTrace();
136
                }
137
                dist = (gp2.getX()-x);
138
                System.err.println("distX = " + dist);
139
                
140
                if (dist > 30.0) {                         step = 30.0;
141
                } else if (dist > 18.0) {         step = 18.0;
142
                } else if (dist > 12.0) {        step = 12.0;
143
                } else if (dist > 6.0) {        step = 6.0;
144
                } else if (dist > 3.0) {        step = 3.0;
145
                } else if (dist > 2.0) {        step = 2.0;
146
                } else if (dist > 1.0) {        step = 1.0;
147
                } else if (dist > .5) {                step =.5;
148
                } else if (dist > .25) {        step =.25;
149
                } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
150
                } else {                                        step = 1.0/60*2.0;
151
                }
152
                        //step = 1.0;
153
                */
154
                generateGrid(g, extent, mat, step);
155
        }
156
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat, double step) {
157
                grid = new Graticule(this);
158
                Point2D pt1 = extent.getMin(), pt2 = extent.getMax();
159
                Point2D.Double ptx = new Point2D.Double(0.0, 0.0);
160
                GeoPoint gp1, gp2;
161
                ProjPoint up1 = (ProjPoint) createPoint(0,0),
162
                        up2 = (ProjPoint) createPoint(0,0);
163
                Geodetic geoProj = Geodetic.getProjection(this.getEllipsoid());
164
                double xAxis, yAxis;
165
                        
166
                // Calculos para el texto
167
                FontMetrics fm = g.getFontMetrics();
168
                int fmWidth = 0, fmHeight = fm.getAscent();
169
                String tit = "";
170
                String fmt = "%G?%N";
171
                if (step < 1.0)
172
                        fmt = "%G?%M'%N";
173
                
174
                // Lineas Horzontales
175
                gp1 = toGeo(new ProjPoint(pt1));
176
                gp2 = toGeo(new ProjPoint(pt2));
177
                xAxis = gp1.getX(); yAxis = gp2.getY();
178
                System.err.println(name+": ViewPort Extent = ("+gp1+","+gp2+")");
179
                double xMin = (int) gp1.getX() - 1.0;
180
                xMin -= (xMin % step);
181
                double xMax = (int) gp2.getX() + 1.0;
182
                double yMin = (int) gp1.getY() - 1.0;
183
                yMin -= (yMin % step);
184
                double yMax = (int) gp2.getY() + 1.0;
185
                if (xMin < -180.0) xMin = -180.0;
186
                if (xMax > 180.0) xMax = 180.0;
187
                if (yMin < -80.0) yMin = -80.0;
188
                if (yMax > 80.0) yMax = 80.0;
189
                if (xAxis < -180.0) xAxis = -180.0;
190
                if (yAxis > 80.0) yAxis = 80.0;
191
                for (double y=yMin; y<=yMax; y+=step) {
192
                        gp1 = (GeoPoint) geoProj.createPoint(xAxis, y);
193
                        gp2 = (GeoPoint) geoProj.createPoint(xMax, y);
194
                        fromGeo(gp1, up1);
195
                        fromGeo(gp2, up2);
196
                        mat.transform(up1, up1);
197
                        mat.transform(up2, up2);
198
                        grid.addLine(up1, up2);
199
                        
200
                        tit = coordToString(y, fmt, true);
201
                        //fmWidth = fm.stringWidth(tit);
202
                        ptx.setLocation(up1.getX()+3, up1.getY()-2);
203
                        grid.addText(tit, ptx);
204
                }
205
                
206
                // Lineas Verticales
207
                for (double x=xMin; x<=xMax; x+=step) {
208
                        gp1 = (GeoPoint) geoProj.createPoint(x, yMin);
209
                        gp2 = (GeoPoint) geoProj.createPoint(x, yAxis);
210
                        fromGeo(gp1, up1);
211
                        fromGeo(gp2, up2);
212
                        mat.transform(up1, up1);
213
                        mat.transform(up2, up2);
214
                        grid.addLine(up1, up2);
215
                        
216
                        tit = coordToString(x, fmt, false);
217
                        //fmWidth = fm.stringWidth(tit);
218
                        ptx.setLocation(up2.getX()+3, up2.getY()+fmHeight);
219
                        grid.addText(tit, ptx);
220
                }
221
        }
222
        
223
        public void drawGrid(Graphics2D g, ViewPort vp) {
224
                generateGrid(g, vp.getExtent(), vp.getMat());
225
                grid.setColor(gridColor);
226
                grid.draw(g, vp);
227
        }
228
}