Statistics
| Revision:

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

History | View | Annotate | Download (7.24 KB)

1
package org.cresques.geo;
2

    
3
import java.awt.Color;
4
import java.awt.Graphics2D;
5
import java.awt.geom.AffineTransform;
6
import java.awt.geom.Point2D;
7

    
8
import org.cresques.px.Extent;
9

    
10
import geojava.UtmGeo;
11
import geojava.Ra2De;
12
import geojava.GeoUtm;
13

    
14
public class UtmZone extends Projection {
15
        static String name = "UTM";
16
        static String abrev = "UTM";
17
        private final static UtmZone ed5028N = new UtmZone(Ellipsoid.ed50, 28, 0, 0.0);
18
        private final static UtmZone ed5029N = new UtmZone(Ellipsoid.ed50, 29, 0, 0.0);
19
        private final static UtmZone ed5030N = new UtmZone(Ellipsoid.ed50, 30, 0, 0.0);
20
        private final static UtmZone ed5031N = new UtmZone(Ellipsoid.ed50, 31, 0, 0.0);
21
        private final static UtmZone hayford28N = new UtmZone(Ellipsoid.hayford, 28, 0, 0.0);
22
        private final static UtmZone hayford29N = new UtmZone(Ellipsoid.hayford, 29, 0, 0.0);
23
        private final static UtmZone hayford30N = new UtmZone(Ellipsoid.hayford, 30, 0, 0.0);
24
        private final static UtmZone hayford31N = new UtmZone(Ellipsoid.hayford, 31, 0, 0.0);
25
        public int Zone = 30;
26
        public int Hemisphere = 0;
27
        public double H = 0.0;
28

    
29
        static GeoUtm geoutm = new GeoUtm();
30
        static UtmGeo utmgeo = new UtmGeo();
31
        
32
        public UtmZone(Ellipsoid eli, int zone, int hemisphere, double h) {
33
                super(eli);
34
                Zone = zone;
35
                Hemisphere = hemisphere;
36
                H = h;
37
                grid = new Graticule(this);
38
        }
39

    
40
        public String getAbrev() { return abrev+Zone;}
41

    
42
        public static UtmZone getProjection(Ellipsoid eli, int zone, int hemisphere) {
43
                if (eli == Ellipsoid.hayford && hemisphere == 0) {
44
                        switch (zone) {
45
                        case 28: return hayford28N;
46
                        case 29: return hayford29N;
47
                        case 30: return hayford30N;
48
                        case 31: return hayford31N;
49
                        }
50
                } else if (eli == Ellipsoid.ed50 && hemisphere == 0) {
51
                        switch (zone) {
52
                        case 28: return ed5028N;
53
                        case 29: return ed5029N;
54
                        case 30: return ed5030N;
55
                        case 31: return ed5031N;
56
                        }
57
                }
58
                System.err.println("UtmZone.getProjection(): new "+zone);
59
                return new UtmZone(eli, zone, hemisphere, 0.0);
60
        }
61
        
62
        /**
63
         * 
64
         */
65
        public static Projection getProjectionByName(Ellipsoid eli, String name) {
66
                int zone, hemisphere = NORTH;
67
                if (name.indexOf("UTM") < 0)
68
                        return null;
69
                if (name.substring(0,1).compareTo("S")==0)
70
                        hemisphere = SOUTH;
71
                zone = Integer.parseInt(name.substring(name.length()-2));
72
                return getProjection(eli, zone, hemisphere);
73
        }
74

    
75
        /**
76
         * 
77
         */        
78
        public Point2D createPoint(double x, double y){
79
                return new UtmPoint(this, x, y);
80
        }
81

    
82
        /**
83
         * 
84
         * @param uPt
85
         * @return
86
         */        
87
        public GeoPoint toGeo(Point2D uPt) {
88
                GeoPoint gPt = new GeoPoint();
89
                return toGeo((UtmPoint) uPt, gPt);
90
        }
91
        
92
        /**
93
         * 
94
         * @param uPt
95
         * @param gPt
96
         * @return
97
         */
98
                
99
        public GeoPoint toGeo(UtmPoint uPt, GeoPoint gPt) {
100
                int ai[] = new int[3];
101
                double ad[] = new double[3];
102
                ai[1] = ((UtmZone) uPt.proj).Zone;
103
                ai[2] = ((UtmZone) uPt.proj).Hemisphere;
104
                ad[1] = uPt.X;
105
                ad[2] = uPt.Y;
106
        
107
                utmgeo.set(uPt.proj.getElliPar(), ai, ad);
108
                utmgeo.go();
109

    
110
                gPt.Longitude = Ra2De.go(utmgeo.Ge[1]);
111
                gPt.Latitude = Ra2De.go(utmgeo.Ge[2]);
112
                gPt.proj = Geodetic.getProjection(uPt.proj.eli);
113
                return gPt;
114
        }
115

    
116
        /**
117
         * 
118
         * @param gPt
119
         * @param uPt
120
         * @return
121
         */        
122
        public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt) {
123
                int ai[] = { 0, 0, 2 };
124
                return fromGeo(gPt,uPt, ai);
125
        }
126

    
127
        /**
128
         * 
129
         * @param gPt
130
         * @param uPt
131
         * @param proj
132
         * @return
133
         */
134
        public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, UtmZone proj) {
135
                int ai[] = { 0, proj.Zone, proj.Hemisphere };
136
                return fromGeo(gPt, uPt, ai);
137
        }
138

    
139
        /**
140
         * 
141
         * @param gPt
142
         * @param uPt
143
         * @param ai
144
         * @return
145
         */
146
        public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, int[] ai) {
147
                double ad[] = new double[4];
148
                ad[1] = gPt.Longitude.ToRadians();
149
                ad[2] = gPt.Latitude.ToRadians();
150
                geoutm.set(gPt.proj.getElliPar(), ai, ad);
151
                geoutm.go();
152
                if(geoutm.Ier != 0)
153
                        return null;
154
                uPt.setLocation(geoutm.Pr[1], geoutm.Pr[2]);
155
                if (((UtmZone) uPt.proj).Zone != geoutm.Iopar[1] || ((UtmZone) uPt.proj).Hemisphere != geoutm.Iopar[2]) {
156
                        uPt.proj = UtmZone.getProjection(uPt.proj.eli, geoutm.Iopar[1], geoutm.Iopar[2]);
157
                }
158
                return uPt;
159
        }
160
        
161
        // Calcula el step en funci?n del zoom
162
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
163
                // calculo del step en funci?n del zoom
164
                Point2D pt1 = extent.getMin();
165

    
166
                double step = 1.0, x = (int) pt1.getX(), dist = 0.0;
167
        /*        GeoPoint gp1, gp2;
168
                gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
169
                mat.transform(gp1, gp1);
170
                gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
171
                try {
172
                        mat.inverseTransform(gp2, gp2);
173
                } catch (NoninvertibleTransformException e) {
174
                        // TODO Auto-generated catch block
175
                        e.printStackTrace();
176
                }
177
                dist = (gp2.getX()-x);
178
                System.err.println("distX = " + dist);
179
                
180
                if (dist > 30.0) {                         step = 30.0;
181
                } else if (dist > 18.0) {         step = 18.0;
182
                } else if (dist > 12.0) {        step = 12.0;
183
                } else if (dist > 6.0) {        step = 6.0;
184
                } else if (dist > 3.0) {        step = 3.0;
185
                } else if (dist > 2.0) {        step = 2.0;
186
                } else if (dist > 1.0) {        step = 1.0;
187
                } else if (dist > .5) {                step =.5;
188
                } else if (dist > .25) {        step =.25;
189
                } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
190
                } else {                                        step = 1.0/60*2.0;
191
                }
192
                        //step = 1.0;
193
                */
194
                generateGrid(g, extent, mat, step);
195
        }
196
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat, double step) {
197
                grid = new Graticule(this);
198
                Point2D pt1 = extent.getMin(), pt2 = extent.getMax();
199
                GeoPoint gp1, gp2;
200
                UtmPoint up1 = (UtmPoint) createPoint(0,0),
201
                        up2 = (UtmPoint) createPoint(0,0);
202
                Point2D.Double mp1 = new Point2D.Double(0.0, 0.0),
203
                        mp2 = new Point2D.Double(0.0, 0.0);
204
                Geodetic geoProj = Geodetic.getProjection(this.getEllipsoid());
205
                boolean esUso = false;        
206
                        
207
                System.err.println(name+": ViewPort Extent = ("+pt1+","+pt2+")");
208
                gp1 = toGeo(new UtmPoint(pt1.getX(), pt2.getY()));
209
                gp2 = toGeo(new UtmPoint(pt2));
210
                double xMin = (int) gp1.getX() - 1.0;
211
                double xMax = (int) gp2.getX() + 1.0;
212
                gp1 = toGeo(new UtmPoint(pt1.getX()+(pt2.getX()-pt1.getX())/2.0, pt2.getY()));
213
                double yMax = (int) gp1.getY() + 2.0;
214
                gp1 = toGeo(new UtmPoint(pt1));
215
                double yMin = (int) gp1.getY() - 1.0;
216
                xMin = -30.0;
217
                xMax = 30.0;
218
                yMin = 20.0;
219
                yMax = 60.0;
220
                for (double x=xMin; x<=xMax; x+=step) {
221
                        up1 = null;
222
                        if (Math.abs( (x) % 6) < .001 ) esUso = true;
223
                        for (double y=yMin; y<=yMax; y+=step) {
224
                                gp2 = (GeoPoint) geoProj.createPoint(x, y);
225
                                fromGeo(gp2, up2, this);
226
                                if (up1 != null && (extent.isAt(up1) || extent.isAt(up2))) {
227
                                        mat.transform(up2, mp2);
228
                                        if (up1 != null) {
229
                                                if (esUso)
230
                                                        grid.addLine(mp1, mp2, 1);
231
                                                else
232
                                                        grid.addLine(mp1, mp2);
233
                                        }
234
                                } else
235
                                        mat.transform(up2, mp2);
236
                                up1 = (UtmPoint) up2.clone();
237
                                mp1 = (Point2D.Double) mp2.clone();
238
                        }
239
                        esUso = false;
240
                }
241

    
242
                for (double y=yMin; y<=yMax; y+=step) {
243
                        up1 = null;
244
                        for (double x=xMin; x<=xMax; x+=step) {
245
                                gp2 = (GeoPoint) geoProj.createPoint(x, y);
246
                                fromGeo(gp2, up2, this);
247
                                if (up1 != null && (extent.isAt(up1) || extent.isAt(up2))) {
248
                                        mat.transform(up2, mp2);
249
                                        if (up1 != null) {
250
                                                grid.addLine(mp1, mp2);
251
                                        }
252
                                } else
253
                                        mat.transform(up2, mp2);
254
                                up1 = (UtmPoint) up2.clone();
255
                                mp1 = (Point2D.Double) mp2.clone();
256
                        }
257
                }
258
        }
259
        
260
        public void drawGrid(Graphics2D g, ViewPort vp) {
261
                generateGrid(g, vp.getExtent(), vp.getMat());
262
                grid.setColor(gridColor);
263
                grid.draw(g, vp);
264
        }
265
}