svn-gvsig-desktop / branches / libProjection_v2_0_prep / libraries / libProjection / src / org / cresques / impl / geo / Mercator.java @ 27135
History | View | Annotate | Download (12.2 KB)
1 | 27135 | cmartinez | /*
|
---|---|---|---|
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.impl.geo; |
||
25 | |||
26 | import java.awt.FontMetrics; |
||
27 | import java.awt.Graphics2D; |
||
28 | import java.awt.geom.AffineTransform; |
||
29 | import java.awt.geom.Point2D; |
||
30 | import java.awt.geom.Rectangle2D; |
||
31 | import java.util.TreeMap; |
||
32 | |||
33 | import org.cresques.cts.ICoordTrans; |
||
34 | import org.cresques.cts.IDatum; |
||
35 | import org.cresques.cts.IProjection; |
||
36 | import org.cresques.geo.ViewPortData; |
||
37 | import org.cresques.px.Extent; |
||
38 | |||
39 | |||
40 | /**
|
||
41 | * Proyeccion Mercator
|
||
42 | * @author "Luis W. Sevilla" <sevilla_lui@gva.es>* @author administrador
|
||
43 | */
|
||
44 | public class Mercator extends Projection { |
||
45 | static String name = "Mercator"; |
||
46 | static String abrev = "Mer"; |
||
47 | private static TreeMap projPool = new TreeMap(); |
||
48 | public static final Mercator hayford = new Mercator(Ellipsoid.hayford); |
||
49 | public static final Mercator wgs84 = new Mercator(Ellipsoid.wgs84); |
||
50 | private double a; |
||
51 | private double f; |
||
52 | private double b; |
||
53 | private double Eps2; |
||
54 | private double EE2; |
||
55 | private double EE3; |
||
56 | private double Epps2; |
||
57 | |||
58 | public Mercator(Ellipsoid eli) {
|
||
59 | super(eli);
|
||
60 | grid = new Graticule(this); |
||
61 | |||
62 | double[] p = eli.getParam(); |
||
63 | a = p[1];
|
||
64 | f = 1 / p[2]; |
||
65 | b = p[3];
|
||
66 | |||
67 | Eps2 = p[5];
|
||
68 | EE2 = Eps2 * Eps2; |
||
69 | EE3 = EE2 * Eps2; |
||
70 | Epps2 = p[7];
|
||
71 | } |
||
72 | |||
73 | public String getAbrev() { |
||
74 | return abrev;
|
||
75 | } |
||
76 | |||
77 | public static Mercator getProjection(Ellipsoid eli) { |
||
78 | Mercator ret = null;
|
||
79 | |||
80 | if (projPool.containsKey(eli.getName())) {
|
||
81 | ret = (Mercator) Mercator.projPool.get(eli.getName()); |
||
82 | } else {
|
||
83 | if (eli == Ellipsoid.hayford) {
|
||
84 | ret = hayford; |
||
85 | } else if (eli == Ellipsoid.wgs84) { |
||
86 | ret = wgs84; |
||
87 | } else {
|
||
88 | ret = new Mercator(eli);
|
||
89 | } |
||
90 | |||
91 | projPool.put(eli.getName(), ret); |
||
92 | } |
||
93 | |||
94 | return ret;
|
||
95 | } |
||
96 | |||
97 | /**
|
||
98 | *
|
||
99 | */
|
||
100 | public static IProjection getProjectionByName(IDatum eli, String name) { |
||
101 | if (name.indexOf("ME") < 0) { |
||
102 | return null; |
||
103 | } |
||
104 | |||
105 | return getProjection((Ellipsoid) eli);
|
||
106 | } |
||
107 | |||
108 | /**
|
||
109 | *
|
||
110 | */
|
||
111 | public Point2D createPoint(double x, double y) { |
||
112 | return new ProjPoint(this, x, y); |
||
113 | } |
||
114 | |||
115 | /**
|
||
116 | *
|
||
117 | * @param uPt
|
||
118 | * @return
|
||
119 | */
|
||
120 | public Point2D toGeo(Point2D mPt) { |
||
121 | GeoPoint gPt = new GeoPoint();
|
||
122 | |||
123 | return toGeo((ProjPoint) mPt, gPt);
|
||
124 | } |
||
125 | |||
126 | /**
|
||
127 | *
|
||
128 | * @param mPt
|
||
129 | * @param gPt
|
||
130 | * @return
|
||
131 | */
|
||
132 | public GeoPoint toGeo(ProjPoint mPt, GeoPoint gPt) {
|
||
133 | double t = Math.pow(Math.E, (-mPt.getY() / a)); |
||
134 | |||
135 | double x1;
|
||
136 | double x = ((Math.PI / 2) - (2 * Math.atan(t))); |
||
137 | |||
138 | do {
|
||
139 | x1 = x; |
||
140 | x = (Math.PI / 2) - |
||
141 | (2 * Math.atan(t * (Math.pow((1 - |
||
142 | (Math.sqrt(Eps2) * Math.sin(x))) / (1 + |
||
143 | (Math.sqrt(Eps2) * Math.sin(x))), |
||
144 | (Math.sqrt(Eps2) / 2))))); |
||
145 | } while ((x - x1) > 0.0000000001); |
||
146 | |||
147 | double lat = (Math.PI / 2) - |
||
148 | (2 * Math.atan(t * (Math.pow((1 - |
||
149 | (Math.sqrt(Eps2) * Math.sin(x))) / (1 + |
||
150 | (Math.sqrt(Eps2) * Math.sin(x))), |
||
151 | (Math.sqrt(Eps2) / 2))))); |
||
152 | |||
153 | double lng = mPt.getX() / a;
|
||
154 | gPt.setLocation((lng * 180.0) / Math.PI, (lat * 180.0) / Math.PI); |
||
155 | gPt.proj = Geodetic.getProjection(((Projection) mPt.proj).eli); |
||
156 | |||
157 | return gPt;
|
||
158 | } |
||
159 | |||
160 | /**
|
||
161 | *
|
||
162 | * @param gPt
|
||
163 | * @param uPt
|
||
164 | * @return
|
||
165 | */
|
||
166 | public Point2D fromGeo(Point2D gPt, Point2D mPt) { |
||
167 | double sl = Math.sin(((GeoPoint) gPt).Latitude.ToRadians()); |
||
168 | double cl = Math.cos(((GeoPoint) gPt).Latitude.ToRadians()); |
||
169 | double tl = (1 + sl) / (1 - sl); |
||
170 | |||
171 | // Calcula Easting
|
||
172 | double x = a * ((GeoPoint) gPt).Longitude.ToRadians();
|
||
173 | |||
174 | // Calcula Northing
|
||
175 | double y = Math.pow(((1 - (Math.sqrt(Eps2) * sl)) / (1 + |
||
176 | (Math.sqrt(Eps2) * sl))), (Math.sqrt(Eps2))); |
||
177 | y = a / 2 * (Math.log(tl * y)); |
||
178 | ((ProjPoint) mPt).setLocation(x, y); |
||
179 | ((ProjPoint) mPt).proj = this;
|
||
180 | |||
181 | return mPt;
|
||
182 | } |
||
183 | |||
184 | // Calcula el step en funci?n del zoom
|
||
185 | private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat) { |
||
186 | // calculo del step en funci?n del zoom
|
||
187 | Point2D pt1 = extent.getMin();
|
||
188 | |||
189 | double step = 3.0; |
||
190 | double x = pt1.getX();
|
||
191 | double dist = 0.0; |
||
192 | ProjPoint ppt1; |
||
193 | ProjPoint ppt2; |
||
194 | GeoPoint gp1; |
||
195 | GeoPoint gp2; |
||
196 | ppt1 = (ProjPoint) createPoint(x, pt1.getY()); |
||
197 | ppt2 = (ProjPoint) createPoint(x + 100, pt1.getY() - 100); |
||
198 | gp1 = (GeoPoint) ppt1.toGeo(); |
||
199 | gp2 = (GeoPoint) ppt2.toGeo(); |
||
200 | |||
201 | /* GeoPoint gp1, gp2;
|
||
202 | gp1 = (GeoPoint) createPoint( x, (int) pt1.getY());
|
||
203 | mat.transform(gp1, gp1);
|
||
204 | gp2 = (GeoPoint) createPoint(gp1.getX()+100, gp1.getY()-100);
|
||
205 | try {
|
||
206 | mat.inverseTransform(gp2, gp2);
|
||
207 | } catch (NoninvertibleTransformException e) {
|
||
208 | // TODO Auto-generated catch block
|
||
209 | e.printStackTrace();
|
||
210 | }
|
||
211 | dist = (gp2.getX()-x);
|
||
212 | System.err.println("distX = " + dist);
|
||
213 | |||
214 | if (dist > 30.0) { step = 30.0;
|
||
215 | } else if (dist > 18.0) { step = 18.0;
|
||
216 | } else if (dist > 12.0) { step = 12.0;
|
||
217 | } else if (dist > 6.0) { step = 6.0;
|
||
218 | } else if (dist > 3.0) { step = 3.0;
|
||
219 | } else if (dist > 2.0) { step = 2.0;
|
||
220 | } else if (dist > 1.0) { step = 1.0;
|
||
221 | } else if (dist > .5) { step =.5;
|
||
222 | } else if (dist > .25) { step =.25;
|
||
223 | } else if (dist > 1.0/60*5.0) { step = 1.0/60*5.0;
|
||
224 | } else { step = 1.0/60*2.0;
|
||
225 | }
|
||
226 | //step = 1.0;
|
||
227 | */
|
||
228 | generateGrid(g, extent, mat, step); |
||
229 | } |
||
230 | |||
231 | private void generateGrid(Graphics2D g, Extent extent, AffineTransform mat, |
||
232 | double step) {
|
||
233 | grid = new Graticule(this); |
||
234 | |||
235 | Point2D pt1 = extent.getMin();
|
||
236 | Point2D pt2 = extent.getMax();
|
||
237 | Point2D.Double ptx = new Point2D.Double(0.0, 0.0); |
||
238 | GeoPoint gp1; |
||
239 | GeoPoint gp2; |
||
240 | ProjPoint up1 = (ProjPoint) createPoint(0, 0); |
||
241 | ProjPoint up2 = (ProjPoint) createPoint(0, 0); |
||
242 | Geodetic geoProj = Geodetic.getProjection((Ellipsoid) getDatum()); |
||
243 | double xAxis;
|
||
244 | double yAxis;
|
||
245 | |||
246 | // Calculos para el texto
|
||
247 | FontMetrics fm = g.getFontMetrics();
|
||
248 | int fmWidth = 0; |
||
249 | int fmHeight = fm.getAscent();
|
||
250 | String tit = ""; |
||
251 | String fmt = "%G?%N"; |
||
252 | |||
253 | if (step < 1.0) { |
||
254 | fmt = "%G?%M'%N";
|
||
255 | } |
||
256 | |||
257 | // Lineas Horzontales
|
||
258 | gp1 = (GeoPoint) toGeo(new ProjPoint(pt1));
|
||
259 | gp2 = (GeoPoint) toGeo(new ProjPoint(pt2));
|
||
260 | xAxis = gp1.getX(); |
||
261 | yAxis = gp2.getY(); |
||
262 | System.err.println(name + ": ViewPort Extent = (" + gp1 + "," + gp2 + |
||
263 | ")");
|
||
264 | |||
265 | double xMin = (int) gp1.getX() - 1.0; |
||
266 | xMin -= (xMin % step); |
||
267 | |||
268 | double xMax = (int) gp2.getX() + 1.0; |
||
269 | double yMin = (int) gp1.getY() - 1.0; |
||
270 | yMin -= (yMin % step); |
||
271 | |||
272 | double yMax = (int) gp2.getY() + 1.0; |
||
273 | |||
274 | if (xMin < -180.0) { |
||
275 | xMin = -180.0;
|
||
276 | } |
||
277 | |||
278 | if (xMax > 180.0) { |
||
279 | xMax = 180.0;
|
||
280 | } |
||
281 | |||
282 | if (yMin < -80.0) { |
||
283 | yMin = -80.0;
|
||
284 | } |
||
285 | |||
286 | if (yMax > 80.0) { |
||
287 | yMax = 80.0;
|
||
288 | } |
||
289 | |||
290 | if (xAxis < -180.0) { |
||
291 | xAxis = -180.0;
|
||
292 | } |
||
293 | |||
294 | if (yAxis > 80.0) { |
||
295 | yAxis = 80.0;
|
||
296 | } |
||
297 | |||
298 | for (double y = yMin; y <= yMax; y += step) { |
||
299 | gp1 = (GeoPoint) geoProj.createPoint(xAxis, y); |
||
300 | gp2 = (GeoPoint) geoProj.createPoint(xMax, y); |
||
301 | fromGeo(gp1, up1); |
||
302 | fromGeo(gp2, up2); |
||
303 | mat.transform(up1, up1); |
||
304 | mat.transform(up2, up2); |
||
305 | grid.addLine(up1, up2); |
||
306 | |||
307 | tit = coordToString(y, fmt, true);
|
||
308 | |||
309 | //fmWidth = fm.stringWidth(tit);
|
||
310 | ptx.setLocation(up1.getX() + 3, up1.getY() - 2); |
||
311 | grid.addText(tit, ptx); |
||
312 | } |
||
313 | |||
314 | // Lineas Verticales
|
||
315 | for (double x = xMin; x <= xMax; x += step) { |
||
316 | gp1 = (GeoPoint) geoProj.createPoint(x, yMin); |
||
317 | gp2 = (GeoPoint) geoProj.createPoint(x, yAxis); |
||
318 | fromGeo(gp1, up1); |
||
319 | fromGeo(gp2, up2); |
||
320 | mat.transform(up1, up1); |
||
321 | mat.transform(up2, up2); |
||
322 | grid.addLine(up1, up2); |
||
323 | |||
324 | tit = coordToString(x, fmt, false);
|
||
325 | |||
326 | //fmWidth = fm.stringWidth(tit);
|
||
327 | ptx.setLocation(up2.getX() + 3, up2.getY() + fmHeight);
|
||
328 | grid.addText(tit, ptx); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | public void drawGrid(Graphics2D g, ViewPortData vp) { |
||
333 | generateGrid(g, vp.getExtent(), vp.getMat()); |
||
334 | grid.setColor(gridColor); |
||
335 | grid.draw(g, vp); |
||
336 | } |
||
337 | |||
338 | /* (non-Javadoc)
|
||
339 | * @see org.cresques.cts.IProjection#getScale(double, double, double, double)
|
||
340 | */
|
||
341 | public double getScale(double minX, double maxX, double width, double dpi) { |
||
342 | Projection prj = Geodetic.getProjection((Ellipsoid) getDatum()); |
||
343 | GeoPoint pt1 = (GeoPoint) prj.createPoint(1.0, 0.0); |
||
344 | GeoPoint pt2 = (GeoPoint) prj.createPoint(2.0, 0.0); |
||
345 | ProjPoint ppt1 = (ProjPoint) createPoint(0.0, 0.0); |
||
346 | ProjPoint ppt2 = (ProjPoint) createPoint(0.0, 0.0); |
||
347 | fromGeo(pt1, ppt1); |
||
348 | fromGeo(pt2, ppt2); |
||
349 | |||
350 | //scale = ppt2.getX()-ppt1.getX();
|
||
351 | double scale = (((maxX - minX) / (ppt2.getX() - ppt1.getX())) *
|
||
352 | //scale = ((extent.maxX()-extent.minX())/ getWidth());// *
|
||
353 | (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / width; |
||
354 | |||
355 | return scale;
|
||
356 | } |
||
357 | |||
358 | public ICoordTrans getCT(IProjection dest) {
|
||
359 | // TODO Auto-generated method stub
|
||
360 | return null; |
||
361 | } |
||
362 | |||
363 | public Rectangle2D getExtent(Rectangle2D extent, double scale, double wImage, double hImage, double mapUnits,double distanceUnits, double dpi) { |
||
364 | Projection prj = Geodetic.getProjection((Ellipsoid) getDatum()); |
||
365 | GeoPoint pt1 = (GeoPoint) prj.createPoint(1.0, 0.0); |
||
366 | GeoPoint pt2 = (GeoPoint) prj.createPoint(2.0, 0.0); |
||
367 | ProjPoint ppt1 = (ProjPoint) createPoint(0.0, 0.0); |
||
368 | ProjPoint ppt2 = (ProjPoint) createPoint(0.0, 0.0); |
||
369 | fromGeo(pt1, ppt1); |
||
370 | fromGeo(pt2, ppt2); |
||
371 | double w =0; |
||
372 | double h =0; |
||
373 | double wExtent =0; |
||
374 | double hExtent =0; |
||
375 | w = ((wImage / dpi) * 2.54);
|
||
376 | h = ((hImage / dpi) * 2.54);
|
||
377 | wExtent =((w*scale*distanceUnits)/ (ppt2.getX() - ppt1.getX()))/ (mapUnits*1852.0*60.0); |
||
378 | hExtent =((h*scale*distanceUnits)/ (ppt2.getX() - ppt1.getX()))/ (mapUnits*1852.0*60.0); |
||
379 | double xExtent = extent.getCenterX() - wExtent/2; |
||
380 | double yExtent = extent.getCenterY() - hExtent/2; |
||
381 | Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent); |
||
382 | return rec;
|
||
383 | } |
||
384 | |||
385 | /* (non-Javadoc)
|
||
386 | * @see org.cresques.cts.IProjection#getFullCode()
|
||
387 | */
|
||
388 | public String getFullCode() { |
||
389 | return getAbrev();
|
||
390 | } |
||
391 | } |