svn-gvsig-desktop / tags / Root_v06 / libraries / libCq CMS for java.old / src / org / cresques / geo / Geodetic.java @ 4811
History | View | Annotate | Download (7.64 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.IDatum; |
27 |
import org.cresques.cts.IProjection; |
28 |
|
29 |
import org.cresques.px.Extent; |
30 |
|
31 |
import java.awt.FontMetrics; |
32 |
import java.awt.Graphics2D; |
33 |
import java.awt.geom.AffineTransform; |
34 |
import java.awt.geom.NoninvertibleTransformException; |
35 |
import java.awt.geom.Point2D; |
36 |
|
37 |
import java.util.TreeMap; |
38 |
|
39 |
|
40 |
public class Geodetic extends Projection { |
41 |
private static TreeMap projList = new TreeMap(); |
42 |
static String name = "Geodesica"; |
43 |
static String abrev = "Geo"; |
44 |
public static Geodetic hayford = new Geodetic(Ellipsoid.hayford); |
45 |
|
46 |
public Geodetic() {
|
47 |
super();
|
48 |
grid = new Graticule(this); |
49 |
} |
50 |
|
51 |
public Geodetic(Ellipsoid eli) {
|
52 |
super(eli);
|
53 |
grid = new Graticule(this); |
54 |
} |
55 |
|
56 |
public String getAbrev() { |
57 |
return abrev;
|
58 |
} |
59 |
|
60 |
/**
|
61 |
*
|
62 |
*/
|
63 |
public static IProjection getProjectionByName(IDatum eli, String name) { |
64 |
if (name.indexOf("GEO") < 0) { |
65 |
return null; |
66 |
} |
67 |
|
68 |
if (name.indexOf("WGS84") >= 0) { |
69 |
return getProjection(Ellipsoid.wgs84);
|
70 |
} |
71 |
|
72 |
if (name.indexOf("ED50") >= 0) { |
73 |
return getProjection(Ellipsoid.wgs84);
|
74 |
} |
75 |
|
76 |
return null; |
77 |
} |
78 |
|
79 |
public static Geodetic getProjection(Ellipsoid eli) { |
80 |
Geodetic ret = null;
|
81 |
String key = eli.toString();
|
82 |
|
83 |
if (Geodetic.projList.containsKey(key)) {
|
84 |
ret = (Geodetic) Geodetic.projList.get(key); |
85 |
} else {
|
86 |
if (eli == Ellipsoid.hayford) {
|
87 |
ret = hayford; |
88 |
} else {
|
89 |
ret = new Geodetic(eli);
|
90 |
} |
91 |
|
92 |
Geodetic.projList.put(key, ret); |
93 |
} |
94 |
|
95 |
return ret;
|
96 |
} |
97 |
|
98 |
public Point2D createPoint(double x, double y) { |
99 |
return new GeoPoint(this, x, y); |
100 |
} |
101 |
|
102 |
/**
|
103 |
*
|
104 |
* @param gPt Punto para pasar a GeoPoint
|
105 |
* @return
|
106 |
*/
|
107 |
public Point2D toGeo(Point2D gPt) { |
108 |
return (GeoPoint) gPt;
|
109 |
} |
110 |
|
111 |
public Point2D fromGeo(Point2D gPt, Point2D pPt) { |
112 |
return gPt;
|
113 |
} |
114 |
|
115 |
// Calcula el step en funci?n del zoom
|
116 |
private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) { |
117 |
// calculo del step en funci?n del zoom
|
118 |
Point2D pt1 = extent.getMin();
|
119 |
|
120 |
double step = 1.0; |
121 |
double x = (int) pt1.getX(); |
122 |
double dist = 0.0; |
123 |
GeoPoint gp1; |
124 |
GeoPoint gp2; |
125 |
gp1 = (GeoPoint) createPoint(x, (int) pt1.getY());
|
126 |
mat.transform(gp1, gp1); |
127 |
gp2 = (GeoPoint) createPoint(gp1.getX() + 100, gp1.getY() - 100); |
128 |
|
129 |
try {
|
130 |
mat.inverseTransform(gp2, gp2); |
131 |
} catch (NoninvertibleTransformException e) { |
132 |
// TODO Auto-generated catch block
|
133 |
e.printStackTrace(); |
134 |
} |
135 |
|
136 |
dist = (gp2.getX() - x); |
137 |
System.err.println("distX = " + dist); |
138 |
|
139 |
if (dist > 30.0) { |
140 |
step = 30.0;
|
141 |
} else if (dist > 15.0) { |
142 |
step = 15.0;
|
143 |
} else if (dist > 10.0) { |
144 |
step = 10.0;
|
145 |
} else if (dist > 5.0) { |
146 |
step = 5.0;
|
147 |
} else if (dist > 3.0) { |
148 |
step = 3.0;
|
149 |
} else if (dist > 2.0) { |
150 |
step = 2.0;
|
151 |
} else if (dist > 1.0) { |
152 |
step = 1.0;
|
153 |
} else if (dist > .5) { |
154 |
step = .5;
|
155 |
} else if (dist > .25) { |
156 |
step = .25;
|
157 |
} else if (dist > (1.0 / 60 * 5.0)) { |
158 |
step = 1.0 / 60 * 5.0; |
159 |
} else {
|
160 |
step = 1.0 / 60 * 2.0; |
161 |
} |
162 |
|
163 |
//step = 1.0;
|
164 |
generateGrid(g, extent, mat, step); |
165 |
} |
166 |
|
167 |
private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat, |
168 |
double step) {
|
169 |
grid = new Graticule(this); |
170 |
|
171 |
GeoPoint gp1; |
172 |
GeoPoint gp2; |
173 |
Point2D.Double ptx = new Point2D.Double(0.0, 0.0); |
174 |
|
175 |
Point2D pt1 = extent.getMin();
|
176 |
Point2D pt2 = extent.getMax();
|
177 |
System.err.println(name + ": ViewPort Extent = (" + pt1 + "," + pt2 + |
178 |
")");
|
179 |
|
180 |
// Calculos para el texto
|
181 |
FontMetrics fm = g.getFontMetrics();
|
182 |
int fmWidth = 0; |
183 |
int fmHeight = fm.getAscent();
|
184 |
String tit = ""; |
185 |
String fmt = "%G?%N"; |
186 |
|
187 |
if (step < 1.0) { |
188 |
fmt = "%G?%M'%N";
|
189 |
} |
190 |
|
191 |
// Lineas Verticales
|
192 |
double y1 = pt1.getY();
|
193 |
|
194 |
// Lineas Verticales
|
195 |
double y2 = pt2.getY();
|
196 |
double xIni = (int) pt1.getX() - 1; |
197 |
xIni -= (xIni % step); |
198 |
|
199 |
double xFin = ((int) pt2.getX()) + 1; |
200 |
|
201 |
if (y1 < -90.0) { |
202 |
y1 = -90.0;
|
203 |
} |
204 |
|
205 |
if (y2 > 90.0) { |
206 |
y2 = 90.0;
|
207 |
} |
208 |
|
209 |
if (xIni < -180.0) { |
210 |
xIni = -180.0;
|
211 |
} |
212 |
|
213 |
if (xFin > 180.0) { |
214 |
xFin = 180.0;
|
215 |
} |
216 |
|
217 |
for (double x = xIni; x <= xFin; x += step) { |
218 |
gp1 = (GeoPoint) createPoint(x, y1); |
219 |
gp2 = (GeoPoint) createPoint(x, y2); |
220 |
mat.transform(gp1, gp1); |
221 |
mat.transform(gp2, gp2); |
222 |
grid.addLine(gp1, gp2); |
223 |
tit = coordToString(x, fmt, false);
|
224 |
|
225 |
//fmWidth = fm.stringWidth(tit);
|
226 |
ptx.setLocation(gp2.getX() + 3, gp2.getY() + fmHeight);
|
227 |
grid.addText(tit, ptx); |
228 |
} |
229 |
|
230 |
// Lineas Horizontales
|
231 |
double x1 = pt1.getX();
|
232 |
|
233 |
// Lineas Horizontales
|
234 |
double x2 = pt2.getX();
|
235 |
double yIni = (int) pt1.getY() - 1; |
236 |
yIni -= (yIni % step); |
237 |
|
238 |
double yFin = ((int) pt2.getY()) + 1; |
239 |
|
240 |
if (x1 < -180.0) { |
241 |
x1 = -180.0;
|
242 |
} |
243 |
|
244 |
if (x2 > 180.0) { |
245 |
x2 = 180.0;
|
246 |
} |
247 |
|
248 |
if (yIni < -90.0) { |
249 |
yIni = -90.0;
|
250 |
} |
251 |
|
252 |
if (yFin > 90.0) { |
253 |
yFin = 90.0;
|
254 |
} |
255 |
|
256 |
for (double y = yIni; y <= yFin; y += step) { |
257 |
gp1 = (GeoPoint) createPoint(x1, y); |
258 |
gp2 = (GeoPoint) createPoint(x2, y); |
259 |
mat.transform(gp1, gp1); |
260 |
mat.transform(gp2, gp2); |
261 |
grid.addLine(gp1, gp2); |
262 |
tit = coordToString(y, fmt, true);
|
263 |
|
264 |
//fmWidth = fm.stringWidth(tit);
|
265 |
ptx.setLocation(gp1.getX() + 3, gp1.getY() - 2); |
266 |
grid.addText(tit, ptx); |
267 |
} |
268 |
} |
269 |
|
270 |
public void drawGrid(Graphics2D g, ViewPortData vp) { |
271 |
generateGrid(g, vp.getExtent(), vp.getMat()); |
272 |
grid.setColor(gridColor); |
273 |
grid.draw(g, vp); |
274 |
} |
275 |
|
276 |
public boolean isProjected() { |
277 |
return true; |
278 |
} |
279 |
|
280 |
/* (non-Javadoc)
|
281 |
* @see org.cresques.cts.IProjection#getScale(double, double, double, double)
|
282 |
*/
|
283 |
public double getScale(double minX, double maxX, double width, double dpi) { |
284 |
double scale = ((maxX - minX) * // grados |
285 |
|
286 |
// 1852.0 metros x minuto de meridiano
|
287 |
(dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro |
288 |
width; // pixels
|
289 |
|
290 |
return scale;
|
291 |
} |
292 |
} |