Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / geo / UtmZone.java @ 8026

History | View | Annotate | Download (11.6 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 geojava.GeoUtm;
27
import geojava.Ra2De;
28
import geojava.UtmGeo;
29

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

    
34
import org.cresques.px.Extent;
35

    
36
import java.awt.Graphics2D;
37
import java.awt.geom.AffineTransform;
38
import java.awt.geom.Point2D;
39

    
40

    
41
public class UtmZone extends Projection {
42
    static String name = "UTM";
43
    static String abrev = "UTM";
44
    private final static UtmZone ed5028N = new UtmZone(Ellipsoid.ed50, 28, 0,
45
                                                       0.0);
46
    private final static UtmZone ed5029N = new UtmZone(Ellipsoid.ed50, 29, 0,
47
                                                       0.0);
48
    private final static UtmZone ed5030N = new UtmZone(Ellipsoid.ed50, 30, 0,
49
                                                       0.0);
50
    private final static UtmZone ed5031N = new UtmZone(Ellipsoid.ed50, 31, 0,
51
                                                       0.0);
52
    private final static UtmZone hayford28N = new UtmZone(Ellipsoid.hayford,
53
                                                          28, 0, 0.0);
54
    private final static UtmZone hayford29N = new UtmZone(Ellipsoid.hayford,
55
                                                          29, 0, 0.0);
56
    private final static UtmZone hayford30N = new UtmZone(Ellipsoid.hayford,
57
                                                          30, 0, 0.0);
58
    private final static UtmZone hayford31N = new UtmZone(Ellipsoid.hayford,
59
                                                          31, 0, 0.0);
60
    static GeoUtm geoutm = new GeoUtm();
61
    static UtmGeo utmgeo = new UtmGeo();
62
    public int Zone = 30;
63
    public int Hemisphere = 0;
64
    public double H = 0.0;
65

    
66
    public UtmZone(Ellipsoid eli, int zone, int hemisphere, double h) {
67
        super(eli);
68
        Zone = zone;
69
        Hemisphere = hemisphere;
70
        H = h;
71
        grid = new Graticule(this);
72
    }
73

    
74
    public String getAbrev() {
75
        return abrev + Zone;
76
    }
77

    
78
    public static UtmZone getProjection(Ellipsoid eli, int zone, int hemisphere) {
79
        if ((eli == Ellipsoid.hayford) && (hemisphere == 0)) {
80
            switch (zone) {
81
            case 28:
82
                return hayford28N;
83

    
84
            case 29:
85
                return hayford29N;
86

    
87
            case 30:
88
                return hayford30N;
89

    
90
            case 31:
91
                return hayford31N;
92
            }
93
        } else if ((eli == Ellipsoid.ed50) && (hemisphere == 0)) {
94
            switch (zone) {
95
            case 28:
96
                return ed5028N;
97

    
98
            case 29:
99
                return ed5029N;
100

    
101
            case 30:
102
                return ed5030N;
103

    
104
            case 31:
105
                return ed5031N;
106
            }
107
        }
108

    
109
        System.err.println("UtmZone.getProjection(): new " + zone);
110

    
111
        return new UtmZone(eli, zone, hemisphere, 0.0);
112
    }
113

    
114
    /**
115
     *
116
     */
117
    public static IProjection getProjectionByName(IDatum eli, String name) {
118
        int zone;
119
        int hemisphere = NORTH;
120

    
121
        if (name.indexOf("UTM") < 0) {
122
            return null;
123
        }
124

    
125
        if (name.substring(0, 1).compareTo("S") == 0) {
126
            hemisphere = SOUTH;
127
        }
128

    
129
        zone = Integer.parseInt(name.substring(name.length() - 2));
130

    
131
        return getProjection((Ellipsoid) eli, zone, hemisphere);
132
    }
133

    
134
    /**
135
     *
136
     */
137
    public Point2D createPoint(double x, double y) {
138
        return new UtmPoint(this, x, y);
139
    }
140

    
141
    /**
142
     *
143
     * @param uPt
144
     * @return
145
     */
146
    public Point2D toGeo(Point2D uPt) {
147
        GeoPoint gPt = new GeoPoint();
148

    
149
        return toGeo((UtmPoint) uPt, gPt);
150
    }
151

    
152
    /**
153
     *
154
     * @param uPt
155
     * @param gPt
156
     * @return
157
     */
158
    public GeoPoint toGeo(UtmPoint uPt, GeoPoint gPt) {
159
        int[] ai = new int[3];
160
        double[] ad = new double[3];
161
        ai[1] = ((UtmZone) uPt.proj).Zone;
162
        ai[2] = ((UtmZone) uPt.proj).Hemisphere;
163
        ad[1] = uPt.X;
164
        ad[2] = uPt.Y;
165

    
166
        utmgeo.set(((Projection) uPt.proj).getElliPar(), ai, ad);
167
        utmgeo.go();
168

    
169
        gPt.Longitude = Ra2De.go(utmgeo.Ge[1]);
170
        gPt.Latitude = Ra2De.go(utmgeo.Ge[2]);
171
        gPt.proj = Geodetic.getProjection(((Projection) uPt.proj).eli);
172

    
173
        return gPt;
174
    }
175

    
176
    /**
177
     *
178
     * @param gPt
179
     * @param uPt
180
     * @return
181
     */
182
    public Point2D fromGeo(Point2D gPt, Point2D uPt) {
183
        int[] ai = { 0, 0, 2 };
184

    
185
        return fromGeo((GeoPoint) gPt, (UtmPoint) uPt, ai);
186
    }
187

    
188
    /**
189
     *
190
     * @param gPt
191
     * @param uPt
192
     * @param proj
193
     * @return
194
     */
195
    public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, UtmZone proj) {
196
        int[] ai = { 0, proj.Zone, proj.Hemisphere };
197

    
198
        return fromGeo(gPt, uPt, ai);
199
    }
200

    
201
    /**
202
     *
203
     * @param gPt
204
     * @param uPt
205
     * @param ai
206
     * @return
207
     */
208
    public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, int[] ai) {
209
        double[] ad = new double[4];
210
        ad[1] = gPt.Longitude.ToRadians();
211
        ad[2] = gPt.Latitude.ToRadians();
212
        geoutm.set(((Projection) gPt.proj).getElliPar(), ai, ad);
213
        geoutm.go();
214

    
215
        if (geoutm.Ier != 0) {
216
            return null;
217
        }
218

    
219
        uPt.setLocation(geoutm.Pr[1], geoutm.Pr[2]);
220

    
221
        if ((((UtmZone) uPt.proj).Zone != geoutm.Iopar[1]) ||
222
                (((UtmZone) uPt.proj).Hemisphere != geoutm.Iopar[2])) {
223
            uPt.proj = UtmZone.getProjection(((Projection) uPt.proj).eli,
224
                                             geoutm.Iopar[1], geoutm.Iopar[2]);
225
        }
226

    
227
        return uPt;
228
    }
229

    
230
    // Calcula el step en funci?n del zoom
231
    private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
232
        // calculo del step en funci?n del zoom
233
        Point2D pt1 = extent.getMin();
234

    
235
        double step = 1.0;
236
        double x = (int) pt1.getX();
237
        double dist = 0.0;
238

    
239
        /*        GeoPoint gp1, gp2;
240
                gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
241
                mat.transform(gp1, gp1);
242
                gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
243
                try {
244
                        mat.inverseTransform(gp2, gp2);
245
                } catch (NoninvertibleTransformException e) {
246
                        // TODO Auto-generated catch block
247
                        e.printStackTrace();
248
                }
249
                dist = (gp2.getX()-x);
250
                System.err.println("distX = " + dist);
251

252
                if (dist > 30.0) {                         step = 30.0;
253
                } else if (dist > 18.0) {         step = 18.0;
254
                } else if (dist > 12.0) {        step = 12.0;
255
                } else if (dist > 6.0) {        step = 6.0;
256
                } else if (dist > 3.0) {        step = 3.0;
257
                } else if (dist > 2.0) {        step = 2.0;
258
                } else if (dist > 1.0) {        step = 1.0;
259
                } else if (dist > .5) {                step =.5;
260
                } else if (dist > .25) {        step =.25;
261
                } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
262
                } else {                                        step = 1.0/60*2.0;
263
                }
264
                        //step = 1.0;
265
                */
266
        generateGrid(g, extent, mat, step);
267
    }
268

    
269
    private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat,
270
                              double step) {
271
        grid = new Graticule(this);
272

    
273
        Point2D pt1 = extent.getMin();
274
        Point2D pt2 = extent.getMax();
275
        GeoPoint gp1;
276
        GeoPoint gp2;
277
        UtmPoint up1 = (UtmPoint) createPoint(0, 0);
278
        UtmPoint up2 = (UtmPoint) createPoint(0, 0);
279
        Point2D.Double mp1 = new Point2D.Double(0.0, 0.0);
280
        Point2D.Double mp2 = new Point2D.Double(0.0, 0.0);
281
        Geodetic geoProj = Geodetic.getProjection((Ellipsoid) getDatum());
282
        boolean esUso = false;
283

    
284
        System.err.println(name + ": ViewPort Extent = (" + pt1 + "," + pt2 +
285
                           ")");
286
        gp1 = (GeoPoint) toGeo(new UtmPoint(pt1.getX(), pt2.getY()));
287
        gp2 = (GeoPoint) toGeo(new UtmPoint(pt2));
288

    
289
        double xMin = (int) gp1.getX() - 1.0;
290
        double xMax = (int) gp2.getX() + 1.0;
291
        gp1 = (GeoPoint) toGeo(new UtmPoint(pt1.getX() +
292
                                            ((pt2.getX() - pt1.getX()) / 2.0),
293
                                            pt2.getY()));
294

    
295
        double yMax = (int) gp1.getY() + 2.0;
296
        gp1 = (GeoPoint) toGeo(new UtmPoint(pt1));
297

    
298
        double yMin = (int) gp1.getY() - 1.0;
299
        xMin = -30.0;
300
        xMax = 30.0;
301
        yMin = 20.0;
302
        yMax = 60.0;
303

    
304
        for (double x = xMin; x <= xMax; x += step) {
305
            up1 = null;
306

    
307
            if (Math.abs((x) % 6) < .001) {
308
                esUso = true;
309
            }
310

    
311
            for (double y = yMin; y <= yMax; y += step) {
312
                gp2 = (GeoPoint) geoProj.createPoint(x, y);
313
                fromGeo(gp2, up2, this);
314

    
315
                if ((up1 != null) && (extent.isAt(up1) || extent.isAt(up2))) {
316
                    mat.transform(up2, mp2);
317

    
318
                    if (up1 != null) {
319
                        if (esUso) {
320
                            grid.addLine(mp1, mp2, 1);
321
                        } else {
322
                            grid.addLine(mp1, mp2);
323
                        }
324
                    }
325
                } else {
326
                    mat.transform(up2, mp2);
327
                }
328

    
329
                up1 = (UtmPoint) up2.clone();
330
                mp1 = (Point2D.Double) mp2.clone();
331
            }
332

    
333
            esUso = false;
334
        }
335

    
336
        for (double y = yMin; y <= yMax; y += step) {
337
            up1 = null;
338

    
339
            for (double x = xMin; x <= xMax; x += step) {
340
                gp2 = (GeoPoint) geoProj.createPoint(x, y);
341
                fromGeo(gp2, up2, this);
342

    
343
                if ((up1 != null) && (extent.isAt(up1) || extent.isAt(up2))) {
344
                    mat.transform(up2, mp2);
345

    
346
                    if (up1 != null) {
347
                        grid.addLine(mp1, mp2);
348
                    }
349
                } else {
350
                    mat.transform(up2, mp2);
351
                }
352

    
353
                up1 = (UtmPoint) up2.clone();
354
                mp1 = (Point2D.Double) mp2.clone();
355
            }
356
        }
357
    }
358

    
359
    public void drawGrid(Graphics2D g, ViewPortData vp) {
360
        generateGrid(g, vp.getExtent(), vp.getMat());
361
        grid.setColor(gridColor);
362
        grid.draw(g, vp);
363
    }
364

    
365
    /* (non-Javadoc)
366
     * @see org.cresques.cts.IProjection#getScale(double, double, double, double)
367
     */
368
    public double getScale(double minX, double maxX, double width, double dpi) {
369
        double scale = ((maxX - minX) * // metros
370
                       (dpi / 2.54 * 100.0)) / // px / metro
371
                       width; // pixels
372

    
373
        return scale;
374
    }
375

    
376
        public ICoordTrans getCT(IProjection dest) {
377
                // TODO Auto-generated method stub
378
                return null;
379
        }
380
}