]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MapUtils.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / gui / map / MapUtils.java
1 package tim.prune.gui.map;
2
3 /**
4  * Class to manage coordinate conversions for maps
5  */
6 public abstract class MapUtils
7 {
8         /**
9          * Transform a longitude into an x coordinate
10          * @param inLon longitude in degrees
11          * @return scaled X value from 0 to 1
12          */
13         public static double getXFromLongitude(double inLon)
14         {
15                 return (inLon + 180.0) / 360.0;
16         }
17
18         /**
19          * Transform a latitude into a y coordinate
20          * @param inLat latitude in degrees
21          * @return scaled Y value from 0 to 1
22          */
23         public static double getYFromLatitude(double inLat)
24         {
25                 return (1 - Math.log(Math.tan(inLat * Math.PI / 180) + 1 / Math.cos(inLat * Math.PI / 180)) / Math.PI) / 2;
26         }
27
28         /**
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
32          */
33         public static double getLongitudeFromX(double inX)
34         {
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;
40         }
41
42         /**
43          * Transform a y coordinate into a latitude
44          * @param inY scaled Y value from 0 to 1
45          * @return latitude in degrees
46          */
47         public static double getLatitudeFromY(double inY)
48         {
49                 double n = Math.PI * (1 - 2 * inY);
50                 return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
51         }
52 }