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 @ 40559

History | View | Annotate | Download (12.5 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
    public String getAbrev() {
77
        return abrev + Zone;
78
    }
79

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

    
86
            case 29:
87
                return hayford29N;
88

    
89
            case 30:
90
                return hayford30N;
91

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

    
100
            case 29:
101
                return ed5029N;
102

    
103
            case 30:
104
                return ed5030N;
105

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
175
        return gPt;
176
    }
177

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

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

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

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

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

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

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

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

    
229
        return uPt;
230
    }
231

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
335
            esUso = false;
336
        }
337

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

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

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

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

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

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

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

    
375
        return scale;
376
    }
377

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

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

    
398
        /* (non-Javadoc)
399
         * @see org.cresques.cts.IProjection#getFullCode()
400
         */
401
        public String getFullCode() {
402
                return getAbrev();
403
        }
404
}