Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.projection / org.gvsig.projection.cresques / org.gvsig.projection.cresques.impl / src / main / java / org / cresques / impl / geo / UtmZone.java @ 42543

History | View | Annotate | Download (12.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.cresques.impl.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
import org.cresques.geo.ViewPortData;
34

    
35
import org.cresques.px.Extent;
36

    
37
import java.awt.Graphics2D;
38
import java.awt.geom.AffineTransform;
39
import java.awt.geom.Point2D;
40
import java.awt.geom.Rectangle2D;
41

    
42

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

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

    
76
    @Override
77
    public String getAbrev() {
78
        return abrev + Zone;
79
    }
80

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

    
87
            case 29:
88
                return hayford29N;
89

    
90
            case 30:
91
                return hayford30N;
92

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

    
101
            case 29:
102
                return ed5029N;
103

    
104
            case 30:
105
                return ed5030N;
106

    
107
            case 31:
108
                return ed5031N;
109
            }
110
        }
111

    
112
        System.err.println("UtmZone.getProjection(): new " + zone);
113

    
114
        return new UtmZone(eli, zone, hemisphere, 0.0);
115
    }
116

    
117
    /**
118
     *
119
     * @param eli
120
     * @param name
121
     * @return 
122
     */
123
    public static IProjection getProjectionByName(IDatum eli, String name) {
124
        int zone;
125
        int hemisphere = NORTH;
126

    
127
        if (name.indexOf("UTM") < 0) {
128
            return null;
129
        }
130

    
131
        if (name.substring(0, 1).compareTo("S") == 0) {
132
            hemisphere = SOUTH;
133
        }
134

    
135
        zone = Integer.parseInt(name.substring(name.length() - 2));
136

    
137
        return getProjection((Ellipsoid) eli, zone, hemisphere);
138
    }
139

    
140
    /**
141
     *
142
     * @param x
143
     * @param y
144
     * @return 
145
     */
146
    @Override
147
    public Point2D createPoint(double x, double y) {
148
        return new UtmPoint(this, x, y);
149
    }
150

    
151
    /**
152
     *
153
     * @param uPt
154
     * @return
155
     */
156
    @Override
157
    public Point2D toGeo(Point2D uPt) {
158
        GeoPoint gPt = new GeoPoint();
159

    
160
        return toGeo((UtmPoint) uPt, gPt);
161
    }
162

    
163
    /**
164
     *
165
     * @param uPt
166
     * @param gPt
167
     * @return
168
     */
169
    public GeoPoint toGeo(UtmPoint uPt, GeoPoint gPt) {
170
        int[] ai = new int[3];
171
        double[] ad = new double[3];
172
        ai[1] = ((UtmZone) uPt.proj).Zone;
173
        ai[2] = ((UtmZone) uPt.proj).Hemisphere;
174
        ad[1] = uPt.X;
175
        ad[2] = uPt.Y;
176

    
177
        utmgeo.set(((Projection) uPt.proj).getElliPar(), ai, ad);
178
        utmgeo.go();
179

    
180
        gPt.Longitude = Ra2De.go(utmgeo.Ge[1]);
181
        gPt.Latitude = Ra2De.go(utmgeo.Ge[2]);
182
        gPt.proj = Geodetic.getProjection(((Projection) uPt.proj).eli);
183

    
184
        return gPt;
185
    }
186

    
187
    /**
188
     *
189
     * @param gPt
190
     * @param uPt
191
     * @return
192
     */
193
    @Override
194
    public Point2D fromGeo(Point2D gPt, Point2D uPt) {
195
        int[] ai = { 0, 0, 2 };
196

    
197
        return fromGeo((GeoPoint) gPt, (UtmPoint) uPt, ai);
198
    }
199

    
200
    /**
201
     *
202
     * @param gPt
203
     * @param uPt
204
     * @param proj
205
     * @return
206
     */
207
    public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, UtmZone proj) {
208
        int[] ai = { 0, proj.Zone, proj.Hemisphere };
209

    
210
        return fromGeo(gPt, uPt, ai);
211
    }
212

    
213
    /**
214
     *
215
     * @param gPt
216
     * @param uPt
217
     * @param ai
218
     * @return
219
     */
220
    public UtmPoint fromGeo(GeoPoint gPt, UtmPoint uPt, int[] ai) {
221
        double[] ad = new double[4];
222
        ad[1] = gPt.Longitude.ToRadians();
223
        ad[2] = gPt.Latitude.ToRadians();
224
        geoutm.set(((Projection) gPt.proj).getElliPar(), ai, ad);
225
        geoutm.go();
226

    
227
        if (geoutm.Ier != 0) {
228
            return null;
229
        }
230

    
231
        uPt.setLocation(geoutm.Pr[1], geoutm.Pr[2]);
232

    
233
        if ((((UtmZone) uPt.proj).Zone != geoutm.Iopar[1]) ||
234
                (((UtmZone) uPt.proj).Hemisphere != geoutm.Iopar[2])) {
235
            uPt.proj = UtmZone.getProjection(((Projection) uPt.proj).eli,
236
                                             geoutm.Iopar[1], geoutm.Iopar[2]);
237
        }
238

    
239
        return uPt;
240
    }
241

    
242
    // Calcula el step en funci?n del zoom
243
    private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
244
        // calculo del step en funci?n del zoom
245
        Point2D pt1 = extent.getMin();
246

    
247
        double step = 1.0;
248
        double x = (int) pt1.getX();
249
        double dist = 0.0;
250

    
251
        /*        GeoPoint gp1, gp2;
252
                gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
253
                mat.transform(gp1, gp1);
254
                gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
255
                try {
256
                        mat.inverseTransform(gp2, gp2);
257
                } catch (NoninvertibleTransformException e) {
258
                        // TODO Auto-generated catch block
259
                        e.printStackTrace();
260
                }
261
                dist = (gp2.getX()-x);
262
                System.err.println("distX = " + dist);
263

264
                if (dist > 30.0) {                         step = 30.0;
265
                } else if (dist > 18.0) {         step = 18.0;
266
                } else if (dist > 12.0) {        step = 12.0;
267
                } else if (dist > 6.0) {        step = 6.0;
268
                } else if (dist > 3.0) {        step = 3.0;
269
                } else if (dist > 2.0) {        step = 2.0;
270
                } else if (dist > 1.0) {        step = 1.0;
271
                } else if (dist > .5) {                step =.5;
272
                } else if (dist > .25) {        step =.25;
273
                } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
274
                } else {                                        step = 1.0/60*2.0;
275
                }
276
                        //step = 1.0;
277
                */
278
        generateGrid(g, extent, mat, step);
279
    }
280

    
281
    private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat,
282
                              double step) {
283
        grid = new Graticule(this);
284

    
285
        Point2D pt1 = extent.getMin();
286
        Point2D pt2 = extent.getMax();
287
        GeoPoint gp1;
288
        GeoPoint gp2;
289
        UtmPoint up1 = (UtmPoint) createPoint(0, 0);
290
        UtmPoint up2 = (UtmPoint) createPoint(0, 0);
291
        Point2D.Double mp1 = new Point2D.Double(0.0, 0.0);
292
        Point2D.Double mp2 = new Point2D.Double(0.0, 0.0);
293
        Geodetic geoProj = Geodetic.getProjection((Ellipsoid) getDatum());
294
        boolean esUso = false;
295

    
296
        System.err.println(name + ": ViewPort Extent = (" + pt1 + "," + pt2 +
297
                           ")");
298
        gp1 = (GeoPoint) toGeo(new UtmPoint(pt1.getX(), pt2.getY()));
299
        gp2 = (GeoPoint) toGeo(new UtmPoint(pt2));
300

    
301
        double xMin = (int) gp1.getX() - 1.0;
302
        double xMax = (int) gp2.getX() + 1.0;
303
        gp1 = (GeoPoint) toGeo(new UtmPoint(pt1.getX() +
304
                                            ((pt2.getX() - pt1.getX()) / 2.0),
305
                                            pt2.getY()));
306

    
307
        double yMax = (int) gp1.getY() + 2.0;
308
        gp1 = (GeoPoint) toGeo(new UtmPoint(pt1));
309

    
310
        double yMin = (int) gp1.getY() - 1.0;
311
        xMin = -30.0;
312
        xMax = 30.0;
313
        yMin = 20.0;
314
        yMax = 60.0;
315

    
316
        for (double x = xMin; x <= xMax; x += step) {
317
            up1 = null;
318

    
319
            if (Math.abs((x) % 6) < .001) {
320
                esUso = true;
321
            }
322

    
323
            for (double y = yMin; y <= yMax; y += step) {
324
                gp2 = (GeoPoint) geoProj.createPoint(x, y);
325
                fromGeo(gp2, up2, this);
326

    
327
                if ((up1 != null) && (extent.isAt(up1) || extent.isAt(up2))) {
328
                    mat.transform(up2, mp2);
329

    
330
                    if (up1 != null) {
331
                        if (esUso) {
332
                            grid.addLine(mp1, mp2, 1);
333
                        } else {
334
                            grid.addLine(mp1, mp2);
335
                        }
336
                    }
337
                } else {
338
                    mat.transform(up2, mp2);
339
                }
340

    
341
                up1 = (UtmPoint) up2.clone();
342
                mp1 = (Point2D.Double) mp2.clone();
343
            }
344

    
345
            esUso = false;
346
        }
347

    
348
        for (double y = yMin; y <= yMax; y += step) {
349
            up1 = null;
350

    
351
            for (double x = xMin; x <= xMax; x += step) {
352
                gp2 = (GeoPoint) geoProj.createPoint(x, y);
353
                fromGeo(gp2, up2, this);
354

    
355
                if ((up1 != null) && (extent.isAt(up1) || extent.isAt(up2))) {
356
                    mat.transform(up2, mp2);
357

    
358
                    if (up1 != null) {
359
                        grid.addLine(mp1, mp2);
360
                    }
361
                } else {
362
                    mat.transform(up2, mp2);
363
                }
364

    
365
                up1 = (UtmPoint) up2.clone();
366
                mp1 = (Point2D.Double) mp2.clone();
367
            }
368
        }
369
    }
370

    
371
    @Override
372
    public void drawGrid(Graphics2D g, ViewPortData vp) {
373
        generateGrid(g, vp.getExtent(), vp.getMat());
374
        grid.setColor(gridColor);
375
        grid.draw(g, vp);
376
    }
377

    
378
    /* (non-Javadoc)
379
     * @see org.cresques.cts.IProjection#getScale(double, double, double, double)
380
     */
381
    @Override
382
    public double getScale(double minX, double maxX, double width, double dpi) {
383
        double scale = ((maxX - minX) * // metros
384
                       (dpi / 2.54 * 100.0)) / // px / metro
385
                       width; // pixels
386

    
387
        return scale;
388
    }
389

    
390
    @Override
391
        public ICoordTrans getCT(IProjection dest) {
392
                // TODO Auto-generated method stub
393
                return null;
394
        }
395

    
396
    @Override
397
        public Rectangle2D getExtent(Rectangle2D extent, double scale, double wImage, double hImage, double mapUnits,double distanceUnits, double dpi) {
398
                double w =0;
399
                double h =0;
400
                double wExtent =0;
401
                double hExtent =0;
402
                   w = ((wImage / dpi) * 2.54);
403
                h = ((hImage / dpi) * 2.54);
404
                wExtent =w * scale * distanceUnits / mapUnits;
405
                hExtent =h * scale * distanceUnits / mapUnits;
406
                double xExtent = extent.getCenterX() - wExtent/2;
407
                double yExtent = extent.getCenterY() - hExtent/2;
408
                Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent);
409
            return  rec;
410
        }
411

    
412
        /* (non-Javadoc)
413
         * @see org.cresques.cts.IProjection#getFullCode()
414
         */
415
    @Override
416
        public String getFullCode() {
417
                return getAbrev();
418
        }
419
}