]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/browser/UrlGenerator.java
Version 6, October 2008
[GpsPrune.git] / tim / prune / browser / UrlGenerator.java
1 package tim.prune.browser;
2
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5 import java.util.Locale;
6
7 import tim.prune.I18nManager;
8 import tim.prune.data.DataPoint;
9 import tim.prune.data.DoubleRange;
10 import tim.prune.data.TrackInfo;
11
12 /**
13  * Class to manage the generation of map urls
14  * for display in an external browser
15  */
16 public abstract class UrlGenerator
17 {
18         /** Number formatter for five dp */
19         public static final NumberFormat FIVE_DP = NumberFormat.getNumberInstance(Locale.UK);
20         // Select the UK locale for this formatter so that decimal point is always used (not comma)
21         static {
22                 if (FIVE_DP instanceof DecimalFormat) ((DecimalFormat) FIVE_DP).applyPattern("0.00000");
23         }
24
25         /** Constant for Google Maps */
26         public static final int MAP_SOURCE_GOOGLE = 0;
27         /** Constant for Open Street Maps */
28         public static final int MAP_SOURCE_OSM    = 1;
29
30         // TODO: Add other map sources, eg Yahoo, MSN, search.ch ?
31
32         /**
33          * Generate a URL for the given source and track info
34          * @param inSource source to use, either google or openstreetmap
35          * @param inTrackInfo track info
36          * @return url for map
37          */
38         public static String generateUrl(int inSource, TrackInfo inTrackInfo)
39         {
40                 if (inSource == MAP_SOURCE_GOOGLE) {
41                         return generateGoogleUrl(inTrackInfo);
42                 }
43                 return generateOpenStreetMapUrl(inTrackInfo);
44         }
45
46         /**
47          * Generate a url for Google maps
48          * @param inTrackInfo track information
49          * @return URL
50          */
51         private static String generateGoogleUrl(TrackInfo inTrackInfo)
52         {
53                 // Check if any data to display
54                 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
55                 {
56                         return null;
57                 }
58                 double medianLat = getMedianValue(inTrackInfo.getTrack().getLatRange());
59                 double medianLon = getMedianValue(inTrackInfo.getTrack().getLonRange());
60                 double latSpan = getSpan(inTrackInfo.getTrack().getLatRange());
61                 double lonSpan = getSpan(inTrackInfo.getTrack().getLonRange());
62                 // Build basic url with centre position and span
63                 String url = "http://" + I18nManager.getText("url.googlemaps")
64                         + "/?ll=" + FIVE_DP.format(medianLat) + "," + FIVE_DP.format(medianLon)
65                         + "&spn=" + FIVE_DP.format(latSpan) + "," + FIVE_DP.format(lonSpan);
66                 DataPoint currPoint = inTrackInfo.getCurrentPoint();
67                 // Add selected point, if any
68                 if (currPoint != null) {
69                         url = url + "&q=" + FIVE_DP.format(currPoint.getLatitude().getDouble()) + ","
70                                 + FIVE_DP.format(currPoint.getLongitude().getDouble());
71                         if (currPoint.getWaypointName() != null) {
72                                 url = url + "(" + currPoint.getWaypointName() + ")";
73                         }
74                 }
75                 //System.out.println(url);
76                 return url;
77         }
78
79         /**
80          * Generate a url for Open Street Map
81          * @param inTrackInfo track information
82          * @return URL
83          */
84         private static String generateOpenStreetMapUrl(TrackInfo inTrackInfo)
85         {
86                 // Check if any data to display
87                 if (inTrackInfo == null || inTrackInfo.getTrack() == null || inTrackInfo.getTrack().getNumPoints() < 1)
88                 {
89                         return null;
90                 }
91                 DoubleRange latRange = inTrackInfo.getTrack().getLatRange();
92                 DoubleRange lonRange = inTrackInfo.getTrack().getLonRange();
93                 // Build basic url using min and max lat and long
94                 String url = "http://openstreetmap.org/?minlat=" + FIVE_DP.format(latRange.getMinimum())
95                         + "&maxlat=" + FIVE_DP.format(latRange.getMaximum())
96                         + "&minlon=" + FIVE_DP.format(lonRange.getMinimum()) + "&maxlon=" + FIVE_DP.format(lonRange.getMaximum());
97                 DataPoint currPoint = inTrackInfo.getCurrentPoint();
98                 // Add selected point, if any (no way to add point name?)
99                 if (currPoint != null) {
100                         url = url + "&mlat=" + FIVE_DP.format(currPoint.getLatitude().getDouble())
101                                 + "&mlon=" + FIVE_DP.format(currPoint.getLongitude().getDouble());
102                 }
103                 return url;
104         }
105
106         /**
107          * Get the median value from the given lat/long range
108          * @param inRange range of values
109          * @return median value
110          */
111         private static double getMedianValue(DoubleRange inRange)
112         {
113                 return (inRange.getMaximum() + inRange.getMinimum()) / 2.0;
114         }
115
116         /**
117          * Get the span of the given lat/long range
118          * @param inRange range of values
119          * @return span
120          */
121         private static double getSpan(DoubleRange inRange)
122         {
123                 return inRange.getMaximum() - inRange.getMinimum();
124         }
125 }