Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1008 / libraries / libCq_CMS_praster / src / org / cresques / geo / Geodetic.java @ 12520

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

    
39
import java.util.TreeMap;
40

    
41

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

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

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

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

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

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

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

    
78
        return null;
79
    }
80

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

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

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

    
97
        return ret;
98
    }
99

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
278
    /* (non-Javadoc)
279
     * @see org.cresques.cts.IProjection#getScale(double, double, double, double)
280
     */
281
    public double getScale(double minX, double maxX, double width, double dpi) {
282
        double scale = ((maxX - minX) * // grados
283

    
284
        // 1852.0 metros x minuto de meridiano
285
        (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
286
                       width; // pixels
287

    
288
        return scale;
289
    }
290

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

    
296
        public Rectangle2D getExtent(Rectangle2D extent, double scale, double wImage, double hImage, double changeUnits, double dpi) {
297
                double w =0;
298
                double h =0;
299
                double wExtent =0;
300
                double hExtent =0;
301
            w = ((wImage / dpi) * 2.54);
302
                h = ((hImage / dpi) * 2.54);
303
                wExtent =(w*scale)/ (changeUnits*1852.0*60.0);
304
                hExtent =(h*scale)/ (changeUnits*1852.0*60.0);
305
                double xExtent = extent.getCenterX() - wExtent/2;
306
                double yExtent = extent.getCenterY() - hExtent/2;
307
                Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent);
308
            return  rec;
309
        }
310

    
311
        public String getFullCode() {
312
                return getAbrev();
313
        }
314
}