Statistics
| Revision:

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

History | View | Annotate | Download (8.59 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.Graphics2D;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.Point2D;
29

    
30
import org.cresques.cts.IDatum;
31
import org.cresques.cts.IProjection;
32
import org.cresques.px.Extent;
33

    
34
import geojava.UtmGeo;
35
import geojava.Ra2De;
36
import geojava.GeoUtm;
37

    
38
public class UtmZone extends Projection {
39
        static String name = "UTM";
40
        static String abrev = "UTM";
41
        private final static UtmZone ed5028N = new UtmZone(Ellipsoid.ed50, 28, 0, 0.0);
42
        private final static UtmZone ed5029N = new UtmZone(Ellipsoid.ed50, 29, 0, 0.0);
43
        private final static UtmZone ed5030N = new UtmZone(Ellipsoid.ed50, 30, 0, 0.0);
44
        private final static UtmZone ed5031N = new UtmZone(Ellipsoid.ed50, 31, 0, 0.0);
45
        private final static UtmZone hayford28N = new UtmZone(Ellipsoid.hayford, 28, 0, 0.0);
46
        private final static UtmZone hayford29N = new UtmZone(Ellipsoid.hayford, 29, 0, 0.0);
47
        private final static UtmZone hayford30N = new UtmZone(Ellipsoid.hayford, 30, 0, 0.0);
48
        private final static UtmZone hayford31N = new UtmZone(Ellipsoid.hayford, 31, 0, 0.0);
49
        public int Zone = 30;
50
        public int Hemisphere = 0;
51
        public double H = 0.0;
52

    
53
        static GeoUtm geoutm = new GeoUtm();
54
        static UtmGeo utmgeo = new UtmGeo();
55
        
56
        public UtmZone(Ellipsoid eli, int zone, int hemisphere, double h) {
57
                super(eli);
58
                Zone = zone;
59
                Hemisphere = hemisphere;
60
                H = h;
61
                grid = new Graticule(this);
62
        }
63

    
64
        public String getAbrev() { return abrev+Zone;}
65

    
66
        public static UtmZone getProjection(Ellipsoid eli, int zone, int hemisphere) {
67
                if (eli == Ellipsoid.hayford && hemisphere == 0) {
68
                        switch (zone) {
69
                        case 28: return hayford28N;
70
                        case 29: return hayford29N;
71
                        case 30: return hayford30N;
72
                        case 31: return hayford31N;
73
                        }
74
                } else if (eli == Ellipsoid.ed50 && hemisphere == 0) {
75
                        switch (zone) {
76
                        case 28: return ed5028N;
77
                        case 29: return ed5029N;
78
                        case 30: return ed5030N;
79
                        case 31: return ed5031N;
80
                        }
81
                }
82
                System.err.println("UtmZone.getProjection(): new "+zone);
83
                return new UtmZone(eli, zone, hemisphere, 0.0);
84
        }
85
        
86
        /**
87
         * 
88
         */
89
        public static IProjection getProjectionByName(IDatum eli, String name) {
90
                int zone, hemisphere = NORTH;
91
                if (name.indexOf("UTM") < 0)
92
                        return null;
93
                if (name.substring(0,1).compareTo("S")==0)
94
                        hemisphere = SOUTH;
95
                zone = Integer.parseInt(name.substring(name.length()-2));
96
                return getProjection((Ellipsoid) eli, zone, hemisphere);
97
        }
98

    
99
        /**
100
         * 
101
         */        
102
        public Point2D createPoint(double x, double y){
103
                return new UtmPoint(this, x, y);
104
        }
105

    
106
        /**
107
         * 
108
         * @param uPt
109
         * @return
110
         */        
111
        public Point2D toGeo(Point2D uPt) {
112
                GeoPoint gPt = new GeoPoint();
113
                return toGeo((UtmPoint) uPt, gPt);
114
        }
115
        
116
        /**
117
         * 
118
         * @param uPt
119
         * @param gPt
120
         * @return
121
         */
122
                
123
        public GeoPoint toGeo(UtmPoint uPt, GeoPoint gPt) {
124
                int ai[] = new int[3];
125
                double ad[] = new double[3];
126
                ai[1] = ((UtmZone) uPt.proj).Zone;
127
                ai[2] = ((UtmZone) uPt.proj).Hemisphere;
128
                ad[1] = uPt.X;
129
                ad[2] = uPt.Y;
130
        
131
                utmgeo.set(((Projection) uPt.proj).getElliPar(), ai, ad);
132
                utmgeo.go();
133

    
134
                gPt.Longitude = Ra2De.go(utmgeo.Ge[1]);
135
                gPt.Latitude = Ra2De.go(utmgeo.Ge[2]);
136
                gPt.proj = Geodetic.getProjection(((Projection) uPt.proj).eli);
137
                return gPt;
138
        }
139

    
140
        /**
141
         * 
142
         * @param gPt
143
         * @param uPt
144
         * @return
145
         */        
146
        public Point2D fromGeo(Point2D gPt, Point2D uPt) {
147
                int ai[] = { 0, 0, 2 };
148
                return fromGeo((GeoPoint) gPt, (UtmPoint) uPt, ai);
149
        }
150

    
151
        /**
152
         * 
153
         * @param gPt
154
         * @param uPt
155
         * @param proj
156
         * @return
157
         */
158
        public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, UtmZone proj) {
159
                int ai[] = { 0, proj.Zone, proj.Hemisphere };
160
                return fromGeo(gPt, uPt, ai);
161
        }
162

    
163
        /**
164
         * 
165
         * @param gPt
166
         * @param uPt
167
         * @param ai
168
         * @return
169
         */
170
        public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, int[] ai) {
171
                double ad[] = new double[4];
172
                ad[1] = gPt.Longitude.ToRadians();
173
                ad[2] = gPt.Latitude.ToRadians();
174
                geoutm.set(((Projection) gPt.proj).getElliPar(), ai, ad);
175
                geoutm.go();
176
                if(geoutm.Ier != 0)
177
                        return null;
178
                uPt.setLocation(geoutm.Pr[1], geoutm.Pr[2]);
179
                if (((UtmZone) uPt.proj).Zone != geoutm.Iopar[1] || ((UtmZone) uPt.proj).Hemisphere != geoutm.Iopar[2]) {
180
                        uPt.proj = UtmZone.getProjection(((Projection) uPt.proj).eli, geoutm.Iopar[1], geoutm.Iopar[2]);
181
                }
182
                return uPt;
183
        }
184
        
185
        // Calcula el step en funci?n del zoom
186
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
187
                // calculo del step en funci?n del zoom
188
                Point2D pt1 = extent.getMin();
189

    
190
                double step = 1.0, x = (int) pt1.getX(), dist = 0.0;
191
        /*        GeoPoint gp1, gp2;
192
                gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
193
                mat.transform(gp1, gp1);
194
                gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
195
                try {
196
                        mat.inverseTransform(gp2, gp2);
197
                } catch (NoninvertibleTransformException e) {
198
                        // TODO Auto-generated catch block
199
                        e.printStackTrace();
200
                }
201
                dist = (gp2.getX()-x);
202
                System.err.println("distX = " + dist);
203
                
204
                if (dist > 30.0) {                         step = 30.0;
205
                } else if (dist > 18.0) {         step = 18.0;
206
                } else if (dist > 12.0) {        step = 12.0;
207
                } else if (dist > 6.0) {        step = 6.0;
208
                } else if (dist > 3.0) {        step = 3.0;
209
                } else if (dist > 2.0) {        step = 2.0;
210
                } else if (dist > 1.0) {        step = 1.0;
211
                } else if (dist > .5) {                step =.5;
212
                } else if (dist > .25) {        step =.25;
213
                } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
214
                } else {                                        step = 1.0/60*2.0;
215
                }
216
                        //step = 1.0;
217
                */
218
                generateGrid(g, extent, mat, step);
219
        }
220
        private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat, double step) {
221
                grid = new Graticule(this);
222
                Point2D pt1 = extent.getMin(), pt2 = extent.getMax();
223
                GeoPoint gp1, gp2;
224
                UtmPoint up1 = (UtmPoint) createPoint(0,0),
225
                        up2 = (UtmPoint) createPoint(0,0);
226
                Point2D.Double mp1 = new Point2D.Double(0.0, 0.0),
227
                        mp2 = new Point2D.Double(0.0, 0.0);
228
                Geodetic geoProj = Geodetic.getProjection((Ellipsoid) getDatum());
229
                boolean esUso = false;        
230
                        
231
                System.err.println(name+": ViewPort Extent = ("+pt1+","+pt2+")");
232
                gp1 = (GeoPoint) toGeo(new UtmPoint(pt1.getX(), pt2.getY()));
233
                gp2 = (GeoPoint) toGeo(new UtmPoint(pt2));
234
                double xMin = (int) gp1.getX() - 1.0;
235
                double xMax = (int) gp2.getX() + 1.0;
236
                gp1 = (GeoPoint) toGeo(new UtmPoint(pt1.getX()+(pt2.getX()-pt1.getX())/2.0, pt2.getY()));
237
                double yMax = (int) gp1.getY() + 2.0;
238
                gp1 = (GeoPoint) toGeo(new UtmPoint(pt1));
239
                double yMin = (int) gp1.getY() - 1.0;
240
                xMin = -30.0;
241
                xMax = 30.0;
242
                yMin = 20.0;
243
                yMax = 60.0;
244
                for (double x=xMin; x<=xMax; x+=step) {
245
                        up1 = null;
246
                        if (Math.abs( (x) % 6) < .001 ) esUso = true;
247
                        for (double y=yMin; y<=yMax; y+=step) {
248
                                gp2 = (GeoPoint) geoProj.createPoint(x, y);
249
                                fromGeo(gp2, up2, this);
250
                                if (up1 != null && (extent.isAt(up1) || extent.isAt(up2))) {
251
                                        mat.transform(up2, mp2);
252
                                        if (up1 != null) {
253
                                                if (esUso)
254
                                                        grid.addLine(mp1, mp2, 1);
255
                                                else
256
                                                        grid.addLine(mp1, mp2);
257
                                        }
258
                                } else
259
                                        mat.transform(up2, mp2);
260
                                up1 = (UtmPoint) up2.clone();
261
                                mp1 = (Point2D.Double) mp2.clone();
262
                        }
263
                        esUso = false;
264
                }
265

    
266
                for (double y=yMin; y<=yMax; y+=step) {
267
                        up1 = null;
268
                        for (double x=xMin; x<=xMax; x+=step) {
269
                                gp2 = (GeoPoint) geoProj.createPoint(x, y);
270
                                fromGeo(gp2, up2, this);
271
                                if (up1 != null && (extent.isAt(up1) || extent.isAt(up2))) {
272
                                        mat.transform(up2, mp2);
273
                                        if (up1 != null) {
274
                                                grid.addLine(mp1, mp2);
275
                                        }
276
                                } else
277
                                        mat.transform(up2, mp2);
278
                                up1 = (UtmPoint) up2.clone();
279
                                mp1 = (Point2D.Double) mp2.clone();
280
                        }
281
                }
282
        }
283
        
284
        public void drawGrid(Graphics2D g, ViewPortData vp) {
285
                generateGrid(g, vp.getExtent(), vp.getMat());
286
                grid.setColor(gridColor);
287
                grid.draw(g, vp);
288
        }
289

    
290
        /* (non-Javadoc)
291
         * @see org.cresques.cts.IProjection#getScale(double, double, double, double)
292
         */
293
        public double getScale(double minX, double maxX, double width, double dpi) {
294
                double scale = (maxX-minX)*        // metros
295
                (dpi / 2.54 * 100.0)/                // px / metro
296
                width;                                                // pixels
297
                return scale;
298
        }
299
}