Statistics
| Revision:

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

History | View | Annotate | Download (7.72 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 org.cresques.cts.ICoordTrans;
27
import org.cresques.cts.IDatum;
28
import org.cresques.cts.IProjection;
29

    
30
import org.cresques.px.Extent;
31

    
32
import java.awt.FontMetrics;
33
import java.awt.Graphics2D;
34
import java.awt.geom.AffineTransform;
35
import java.awt.geom.NoninvertibleTransformException;
36
import java.awt.geom.Point2D;
37

    
38
import java.util.TreeMap;
39

    
40

    
41
public class Geodetic extends Projection {
42
    private static TreeMap projList = new TreeMap();
43
    static String name = "Geodesica";
44
    static String abrev = "Geo";
45
    public static Geodetic hayford = new Geodetic(Ellipsoid.hayford);
46

    
47
    public Geodetic() {
48
        super();
49
        grid = new Graticule(this);
50
    }
51

    
52
    public Geodetic(Ellipsoid eli) {
53
        super(eli);
54
        grid = new Graticule(this);
55
    }
56

    
57
    public String getAbrev() {
58
        return abrev;
59
    }
60

    
61
    /**
62
     *
63
     */
64
    public static IProjection getProjectionByName(IDatum eli, String name) {
65
        if (name.indexOf("GEO") < 0) {
66
            return null;
67
        }
68

    
69
        if (name.indexOf("WGS84") >= 0) {
70
            return getProjection(Ellipsoid.wgs84);
71
        }
72

    
73
        if (name.indexOf("ED50") >= 0) {
74
            return getProjection(Ellipsoid.wgs84);
75
        }
76

    
77
        return null;
78
    }
79

    
80
    public static Geodetic getProjection(Ellipsoid eli) {
81
        Geodetic ret = null;
82
        String key = eli.toString();
83

    
84
        if (Geodetic.projList.containsKey(key)) {
85
            ret = (Geodetic) Geodetic.projList.get(key);
86
        } else {
87
            if (eli == Ellipsoid.hayford) {
88
                ret = hayford;
89
            } else {
90
                ret = new Geodetic(eli);
91
            }
92

    
93
            Geodetic.projList.put(key, ret);
94
        }
95

    
96
        return ret;
97
    }
98

    
99
    public Point2D createPoint(double x, double y) {
100
        return new GeoPoint(this, x, y);
101
    }
102

    
103
    /**
104
     *
105
     * @param gPt Punto para pasar a GeoPoint
106
     * @return
107
     */
108
    public Point2D toGeo(Point2D gPt) {
109
        return (GeoPoint) gPt;
110
    }
111

    
112
    public Point2D fromGeo(Point2D gPt, Point2D pPt) {
113
        return gPt;
114
    }
115

    
116
    // Calcula el step en funci?n del zoom
117
    private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) {
118
        // calculo del step en funci?n del zoom
119
        Point2D pt1 = extent.getMin();
120

    
121
        double step = 1.0;
122
        double x = (int) pt1.getX();
123
        double dist = 0.0;
124
        GeoPoint gp1;
125
        GeoPoint gp2;
126
        gp1 = (GeoPoint) createPoint(x, (int) pt1.getY());
127
        mat.transform(gp1, gp1);
128
        gp2 = (GeoPoint) createPoint(gp1.getX() + 100, gp1.getY() - 100);
129

    
130
        try {
131
            mat.inverseTransform(gp2, gp2);
132
        } catch (NoninvertibleTransformException e) {
133
            // TODO Auto-generated catch block
134
            e.printStackTrace();
135
        }
136

    
137
        dist = (gp2.getX() - x);
138
        System.err.println("distX = " + dist);
139

    
140
        if (dist > 30.0) {
141
            step = 30.0;
142
        } else if (dist > 15.0) {
143
            step = 15.0;
144
        } else if (dist > 10.0) {
145
            step = 10.0;
146
        } else if (dist > 5.0) {
147
            step = 5.0;
148
        } else if (dist > 3.0) {
149
            step = 3.0;
150
        } else if (dist > 2.0) {
151
            step = 2.0;
152
        } else if (dist > 1.0) {
153
            step = 1.0;
154
        } else if (dist > .5) {
155
            step = .5;
156
        } else if (dist > .25) {
157
            step = .25;
158
        } else if (dist > (1.0 / 60 * 5.0)) {
159
            step = 1.0 / 60 * 5.0;
160
        } else {
161
            step = 1.0 / 60 * 2.0;
162
        }
163

    
164
        //step = 1.0;
165
        generateGrid(g, extent, mat, step);
166
    }
167

    
168
    private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat,
169
                              double step) {
170
        grid = new Graticule(this);
171

    
172
        GeoPoint gp1;
173
        GeoPoint gp2;
174
        Point2D.Double ptx = new Point2D.Double(0.0, 0.0);
175

    
176
        Point2D pt1 = extent.getMin();
177
        Point2D pt2 = extent.getMax();
178
        System.err.println(name + ": ViewPort Extent = (" + pt1 + "," + pt2 +
179
                           ")");
180

    
181
        // Calculos para el texto
182
        FontMetrics fm = g.getFontMetrics();
183
        int fmWidth = 0;
184
        int fmHeight = fm.getAscent();
185
        String tit = "";
186
        String fmt = "%G?%N";
187

    
188
        if (step < 1.0) {
189
            fmt = "%G?%M'%N";
190
        }
191

    
192
        // Lineas Verticales
193
        double y1 = pt1.getY();
194

    
195
        // Lineas Verticales
196
        double y2 = pt2.getY();
197
        double xIni = (int) pt1.getX() - 1;
198
        xIni -= (xIni % step);
199

    
200
        double xFin = ((int) pt2.getX()) + 1;
201

    
202
        if (y1 < -90.0) {
203
            y1 = -90.0;
204
        }
205

    
206
        if (y2 > 90.0) {
207
            y2 = 90.0;
208
        }
209

    
210
        if (xIni < -180.0) {
211
            xIni = -180.0;
212
        }
213

    
214
        if (xFin > 180.0) {
215
            xFin = 180.0;
216
        }
217

    
218
        for (double x = xIni; x <= xFin; x += step) {
219
            gp1 = (GeoPoint) createPoint(x, y1);
220
            gp2 = (GeoPoint) createPoint(x, y2);
221
            mat.transform(gp1, gp1);
222
            mat.transform(gp2, gp2);
223
            grid.addLine(gp1, gp2);
224
            tit = coordToString(x, fmt, false);
225

    
226
            //fmWidth = fm.stringWidth(tit);
227
            ptx.setLocation(gp2.getX() + 3, gp2.getY() + fmHeight);
228
            grid.addText(tit, ptx);
229
        }
230

    
231
        // Lineas Horizontales
232
        double x1 = pt1.getX();
233

    
234
        // Lineas Horizontales
235
        double x2 = pt2.getX();
236
        double yIni = (int) pt1.getY() - 1;
237
        yIni -= (yIni % step);
238

    
239
        double yFin = ((int) pt2.getY()) + 1;
240

    
241
        if (x1 < -180.0) {
242
            x1 = -180.0;
243
        }
244

    
245
        if (x2 > 180.0) {
246
            x2 = 180.0;
247
        }
248

    
249
        if (yIni < -90.0) {
250
            yIni = -90.0;
251
        }
252

    
253
        if (yFin > 90.0) {
254
            yFin = 90.0;
255
        }
256

    
257
        for (double y = yIni; y <= yFin; y += step) {
258
            gp1 = (GeoPoint) createPoint(x1, y);
259
            gp2 = (GeoPoint) createPoint(x2, y);
260
            mat.transform(gp1, gp1);
261
            mat.transform(gp2, gp2);
262
            grid.addLine(gp1, gp2);
263
            tit = coordToString(y, fmt, true);
264

    
265
            //fmWidth = fm.stringWidth(tit);
266
            ptx.setLocation(gp1.getX() + 3, gp1.getY() - 2);
267
            grid.addText(tit, ptx);
268
        }
269
    }
270

    
271
    public void drawGrid(Graphics2D g, ViewPortData vp) {
272
        generateGrid(g, vp.getExtent(), vp.getMat());
273
        grid.setColor(gridColor);
274
        grid.draw(g, vp);
275
    }
276

    
277
    /* (non-Javadoc)
278
     * @see org.cresques.cts.IProjection#getScale(double, double, double, double)
279
     */
280
    public double getScale(double minX, double maxX, double width, double dpi) {
281
        double scale = ((maxX - minX) * // grados
282
                       
283
        // 1852.0 metros x minuto de meridiano
284
        (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
285
                       width; // pixels
286

    
287
        return scale;
288
    }
289

    
290
        public ICoordTrans getCT(IProjection dest) {
291
                // TODO Auto-generated method stub
292
                return null;
293
        }
294
}