Statistics
| Revision:

svn-gvsig-desktop / tags / Root_CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / geo / UtmZone.java @ 1732

History | View | Annotate | Download (7.72 KB)

1 2 luisw
package org.cresques.geo;
2
3
import java.awt.Graphics2D;
4
import java.awt.geom.AffineTransform;
5
import java.awt.geom.Point2D;
6
7 94 luisw
import org.cresques.cts.IDatum;
8
import org.cresques.cts.IProjection;
9 2 luisw
import org.cresques.px.Extent;
10
11
import geojava.UtmGeo;
12
import geojava.Ra2De;
13
import geojava.GeoUtm;
14
15
public class UtmZone extends Projection {
16
        static String name = "UTM";
17
        static String abrev = "UTM";
18
        private final static UtmZone ed5028N = new UtmZone(Ellipsoid.ed50, 28, 0, 0.0);
19
        private final static UtmZone ed5029N = new UtmZone(Ellipsoid.ed50, 29, 0, 0.0);
20
        private final static UtmZone ed5030N = new UtmZone(Ellipsoid.ed50, 30, 0, 0.0);
21
        private final static UtmZone ed5031N = new UtmZone(Ellipsoid.ed50, 31, 0, 0.0);
22
        private final static UtmZone hayford28N = new UtmZone(Ellipsoid.hayford, 28, 0, 0.0);
23
        private final static UtmZone hayford29N = new UtmZone(Ellipsoid.hayford, 29, 0, 0.0);
24
        private final static UtmZone hayford30N = new UtmZone(Ellipsoid.hayford, 30, 0, 0.0);
25
        private final static UtmZone hayford31N = new UtmZone(Ellipsoid.hayford, 31, 0, 0.0);
26
        public int Zone = 30;
27
        public int Hemisphere = 0;
28
        public double H = 0.0;
29
30
        static GeoUtm geoutm = new GeoUtm();
31
        static UtmGeo utmgeo = new UtmGeo();
32
33
        public UtmZone(Ellipsoid eli, int zone, int hemisphere, double h) {
34
                super(eli);
35
                Zone = zone;
36
                Hemisphere = hemisphere;
37
                H = h;
38
                grid = new Graticule(this);
39
        }
40
41
        public String getAbrev() { return abrev+Zone;}
42
43
        public static UtmZone getProjection(Ellipsoid eli, int zone, int hemisphere) {
44
                if (eli == Ellipsoid.hayford && hemisphere == 0) {
45
                        switch (zone) {
46
                        case 28: return hayford28N;
47
                        case 29: return hayford29N;
48
                        case 30: return hayford30N;
49
                        case 31: return hayford31N;
50
                        }
51
                } else if (eli == Ellipsoid.ed50 && hemisphere == 0) {
52
                        switch (zone) {
53
                        case 28: return ed5028N;
54
                        case 29: return ed5029N;
55
                        case 30: return ed5030N;
56
                        case 31: return ed5031N;
57
                        }
58
                }
59
                System.err.println("UtmZone.getProjection(): new "+zone);
60
                return new UtmZone(eli, zone, hemisphere, 0.0);
61
        }
62
63
        /**
64
         *
65
         */
66 94 luisw
        public static IProjection getProjectionByName(IDatum eli, String name) {
67 2 luisw
                int zone, hemisphere = NORTH;
68
                if (name.indexOf("UTM") < 0)
69
                        return null;
70
                if (name.substring(0,1).compareTo("S")==0)
71
                        hemisphere = SOUTH;
72
                zone = Integer.parseInt(name.substring(name.length()-2));
73 94 luisw
                return getProjection((Ellipsoid) eli, zone, hemisphere);
74 2 luisw
        }
75
76
        /**
77
         *
78
         */
79
        public Point2D createPoint(double x, double y){
80
                return new UtmPoint(this, x, y);
81
        }
82
83
        /**
84
         *
85
         * @param uPt
86
         * @return
87
         */
88 988 luisw
        public Point2D toGeo(Point2D uPt) {
89 2 luisw
                GeoPoint gPt = new GeoPoint();
90
                return toGeo((UtmPoint) uPt, gPt);
91
        }
92
93
        /**
94
         *
95
         * @param uPt
96
         * @param gPt
97
         * @return
98
         */
99
100
        public GeoPoint toGeo(UtmPoint uPt, GeoPoint gPt) {
101
                int ai[] = new int[3];
102
                double ad[] = new double[3];
103
                ai[1] = ((UtmZone) uPt.proj).Zone;
104
                ai[2] = ((UtmZone) uPt.proj).Hemisphere;
105
                ad[1] = uPt.X;
106
                ad[2] = uPt.Y;
107
108 94 luisw
                utmgeo.set(((Projection) uPt.proj).getElliPar(), ai, ad);
109 2 luisw
                utmgeo.go();
110
111
                gPt.Longitude = Ra2De.go(utmgeo.Ge[1]);
112
                gPt.Latitude = Ra2De.go(utmgeo.Ge[2]);
113 94 luisw
                gPt.proj = Geodetic.getProjection(((Projection) uPt.proj).eli);
114 2 luisw
                return gPt;
115
        }
116
117
        /**
118
         *
119
         * @param gPt
120
         * @param uPt
121
         * @return
122
         */
123 94 luisw
        public Point2D fromGeo(Point2D gPt, Point2D uPt) {
124 2 luisw
                int ai[] = { 0, 0, 2 };
125 94 luisw
                return fromGeo((GeoPoint) gPt, (UtmPoint) uPt, ai);
126 2 luisw
        }
127
128
        /**
129
         *
130
         * @param gPt
131
         * @param uPt
132
         * @param proj
133
         * @return
134
         */
135
        public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, UtmZone proj) {
136
                int ai[] = { 0, proj.Zone, proj.Hemisphere };
137
                return fromGeo(gPt, uPt, ai);
138
        }
139
140
        /**
141
         *
142
         * @param gPt
143
         * @param uPt
144
         * @param ai
145
         * @return
146
         */
147
        public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, int[] ai) {
148
                double ad[] = new double[4];
149
                ad[1] = gPt.Longitude.ToRadians();
150
                ad[2] = gPt.Latitude.ToRadians();
151 94 luisw
                geoutm.set(((Projection) gPt.proj).getElliPar(), ai, ad);
152 2 luisw
                geoutm.go();
153
                if(geoutm.Ier != 0)
154
                        return null;
155
                uPt.setLocation(geoutm.Pr[1], geoutm.Pr[2]);
156
                if (((UtmZone) uPt.proj).Zone != geoutm.Iopar[1] || ((UtmZone) uPt.proj).Hemisphere != geoutm.Iopar[2]) {
157 94 luisw
                        uPt.proj = UtmZone.getProjection(((Projection) uPt.proj).eli, geoutm.Iopar[1], geoutm.Iopar[2]);
158 2 luisw
                }
159
                return uPt;
160
        }
161
162
        // Calcula el step en funci?n del zoom
163
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
164
                // calculo del step en funci?n del zoom
165
                Point2D pt1 = extent.getMin();
166
167
                double step = 1.0, x = (int) pt1.getX(), dist = 0.0;
168
        /*        GeoPoint gp1, gp2;
169
                gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
170
                mat.transform(gp1, gp1);
171
                gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
172
                try {
173
                        mat.inverseTransform(gp2, gp2);
174
                } catch (NoninvertibleTransformException e) {
175
                        // TODO Auto-generated catch block
176
                        e.printStackTrace();
177
                }
178
                dist = (gp2.getX()-x);
179
                System.err.println("distX = " + dist);
180

181
                if (dist > 30.0) {                         step = 30.0;
182
                } else if (dist > 18.0) {         step = 18.0;
183
                } else if (dist > 12.0) {        step = 12.0;
184
                } else if (dist > 6.0) {        step = 6.0;
185
                } else if (dist > 3.0) {        step = 3.0;
186
                } else if (dist > 2.0) {        step = 2.0;
187
                } else if (dist > 1.0) {        step = 1.0;
188
                } else if (dist > .5) {                step =.5;
189
                } else if (dist > .25) {        step =.25;
190
                } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
191
                } else {                                        step = 1.0/60*2.0;
192
                }
193
                        //step = 1.0;
194
                */
195
                generateGrid(g, extent, mat, step);
196
        }
197
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat, double step) {
198
                grid = new Graticule(this);
199
                Point2D pt1 = extent.getMin(), pt2 = extent.getMax();
200
                GeoPoint gp1, gp2;
201
                UtmPoint up1 = (UtmPoint) createPoint(0,0),
202
                        up2 = (UtmPoint) createPoint(0,0);
203
                Point2D.Double mp1 = new Point2D.Double(0.0, 0.0),
204
                        mp2 = new Point2D.Double(0.0, 0.0);
205 94 luisw
                Geodetic geoProj = Geodetic.getProjection((Ellipsoid) getDatum());
206 2 luisw
                boolean esUso = false;
207
208
                System.err.println(name+": ViewPort Extent = ("+pt1+","+pt2+")");
209 988 luisw
                gp1 = (GeoPoint) toGeo(new UtmPoint(pt1.getX(), pt2.getY()));
210
                gp2 = (GeoPoint) toGeo(new UtmPoint(pt2));
211 2 luisw
                double xMin = (int) gp1.getX() - 1.0;
212
                double xMax = (int) gp2.getX() + 1.0;
213 988 luisw
                gp1 = (GeoPoint) toGeo(new UtmPoint(pt1.getX()+(pt2.getX()-pt1.getX())/2.0, pt2.getY()));
214 2 luisw
                double yMax = (int) gp1.getY() + 2.0;
215 988 luisw
                gp1 = (GeoPoint) toGeo(new UtmPoint(pt1));
216 2 luisw
                double yMin = (int) gp1.getY() - 1.0;
217
                xMin = -30.0;
218
                xMax = 30.0;
219
                yMin = 20.0;
220
                yMax = 60.0;
221
                for (double x=xMin; x<=xMax; x+=step) {
222
                        up1 = null;
223
                        if (Math.abs( (x) % 6) < .001 ) esUso = true;
224
                        for (double y=yMin; y<=yMax; y+=step) {
225
                                gp2 = (GeoPoint) geoProj.createPoint(x, y);
226
                                fromGeo(gp2, up2, this);
227
                                if (up1 != null && (extent.isAt(up1) || extent.isAt(up2))) {
228
                                        mat.transform(up2, mp2);
229
                                        if (up1 != null) {
230
                                                if (esUso)
231
                                                        grid.addLine(mp1, mp2, 1);
232
                                                else
233
                                                        grid.addLine(mp1, mp2);
234
                                        }
235
                                } else
236
                                        mat.transform(up2, mp2);
237
                                up1 = (UtmPoint) up2.clone();
238
                                mp1 = (Point2D.Double) mp2.clone();
239
                        }
240
                        esUso = false;
241
                }
242
243
                for (double y=yMin; y<=yMax; y+=step) {
244
                        up1 = null;
245
                        for (double x=xMin; x<=xMax; x+=step) {
246
                                gp2 = (GeoPoint) geoProj.createPoint(x, y);
247
                                fromGeo(gp2, up2, this);
248
                                if (up1 != null && (extent.isAt(up1) || extent.isAt(up2))) {
249
                                        mat.transform(up2, mp2);
250
                                        if (up1 != null) {
251
                                                grid.addLine(mp1, mp2);
252
                                        }
253
                                } else
254
                                        mat.transform(up2, mp2);
255
                                up1 = (UtmPoint) up2.clone();
256
                                mp1 = (Point2D.Double) mp2.clone();
257
                        }
258
                }
259
        }
260
261 91 luisw
        public void drawGrid(Graphics2D g, ViewPortData vp) {
262 2 luisw
                generateGrid(g, vp.getExtent(), vp.getMat());
263
                grid.setColor(gridColor);
264
                grid.draw(g, vp);
265
        }
266 210 luisw
267
        /* (non-Javadoc)
268
         * @see org.cresques.cts.IProjection#getScale(double, double, double, double)
269
         */
270
        public double getScale(double minX, double maxX, double width, double dpi) {
271
                double scale = (maxX-minX)*        // metros
272
                (dpi / 2.54 * 100.0)/                // px / metro
273
                width;                                                // pixels
274
                return scale;
275
        }
276 2 luisw
}