1 package tim.prune.gui.map;
4 * Class to manage coordinate conversions for maps
6 public abstract class MapUtils
9 * Transform a longitude into an x coordinate
10 * @param inLon longitude in degrees
11 * @return scaled X value from 0 to 1
13 public static double getXFromLongitude(double inLon)
15 return (inLon + 180.0) / 360.0;
19 * Transform a latitude into a y coordinate
20 * @param inLat latitude in degrees
21 * @return scaled Y value from 0 to 1
23 public static double getYFromLatitude(double inLat)
25 return (1 - Math.log(Math.tan(inLat * Math.PI / 180) + 1 / Math.cos(inLat * Math.PI / 180)) / Math.PI) / 2;
29 * Transform an x coordinate into a longitude
30 * @param inX scaled X value from 0(-180deg) to 1(+180deg)
31 * @return longitude in degrees
33 public static double getLongitudeFromX(double inX)
35 // Ensure x is really between 0 and 1 (to wrap longitudes)
36 double x = ((inX % 1.0) + 1.0) % 1.0;
37 // Note: First %1.0 restricts range to (-1,1), then +1.0 shifts to (0,2)
38 // Finally, %1.0 to give (0,1)
39 return x * 360.0 - 180.0;
43 * Transform a y coordinate into a latitude
44 * @param inY scaled Y value from 0 to 1
45 * @return latitude in degrees
47 public static double getLatitudeFromY(double inY)
49 double n = Math.PI * (1 - 2 * inY);
50 return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));