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 / Geodetic.java @ 40559

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

    
31
import org.cresques.px.Extent;
32

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

    
40
import java.util.TreeMap;
41

    
42

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

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

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

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

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

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

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

    
79
        return null;
80
    }
81

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

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

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

    
98
        return ret;
99
    }
100

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
289
        return scale;
290
    }
291

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

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

    
312
        /* (non-Javadoc)
313
         * @see org.cresques.cts.IProjection#getFullCode()
314
         */
315
        public String getFullCode() {
316
                return getAbrev();
317
        }
318
}