Statistics
| Revision:

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

History | View | Annotate | Download (12.2 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
import java.awt.geom.Rectangle2D;
40

    
41

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

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

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

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

    
85
            case 29:
86
                return hayford29N;
87

    
88
            case 30:
89
                return hayford30N;
90

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

    
99
            case 29:
100
                return ed5029N;
101

    
102
            case 30:
103
                return ed5030N;
104

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
174
        return gPt;
175
    }
176

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

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

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

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

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

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

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

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

    
228
        return uPt;
229
    }
230

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
334
            esUso = false;
335
        }
336

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

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

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

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

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

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

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

    
374
        return scale;
375
    }
376

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

    
382
        public Rectangle2D getExtent(Rectangle2D extent, double scale, double wImage, double hImage, double changeUnits, double dpi) {
383
                double w =0;
384
                double h =0;
385
                double wExtent =0;
386
                double hExtent =0;
387
                   w = ((wImage / dpi) * 2.54);
388
                h = ((hImage / dpi) * 2.54);
389
                wExtent =w * scale/ changeUnits;
390
                hExtent =h * scale/ changeUnits;
391
                double xExtent = extent.getCenterX() - wExtent/2;
392
                double yExtent = extent.getCenterY() - hExtent/2;
393
                Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent);
394
            return  rec;
395
        }
396

    
397
        public String getFullCode() {
398
                return getAbrev();
399
        }
400
}