]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MapSource.java
Version 10, May 2010
[GpsPrune.git] / tim / prune / gui / map / MapSource.java
1 package tim.prune.gui.map;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 /**
7  * Class to represent any map source, whether an OsmMapSource
8  * or one of the more complicated ones.
9  * Map sources may contain just one or several layers, and may
10  * build their URLs in different ways depending on the source
11  */
12 public abstract class MapSource
13 {
14         /**
15          * @return the number of layers used in this source
16          */
17         public abstract int getNumLayers();
18
19         /**
20          * @return the name of the source
21          */
22         public abstract String getName();
23
24         /**
25          * @return the base url for the specified layer
26          */
27         public abstract String getBaseUrl(int inLayerNum);
28
29         /**
30          * @return the site name for the specified layer
31          */
32         public abstract String getSiteName(int inLayerNum);
33
34         /**
35          * @return the file extension for the specified layer
36          */
37         public abstract String getFileExtension(int inLayerNum);
38
39         /**
40          * Make the URL to get the specified tile
41          * @param inLayerNum number of layer, from 0 (base) to getNumLayers-1 (top)
42          * @param inZoom zoom level
43          * @param inX x coordinate of tile
44          * @param inY y coordinate of tile
45          * @return URL as string
46          */
47         public abstract String makeURL(int inLayerNum, int inZoom, int inX, int inY);
48
49         /**
50          * @return the maximum zoom level for this source
51          */
52         public abstract int getMaxZoomLevel();
53
54         /**
55          * Make a relative file path from the base directory including site name
56          * @param inLayerNum layer number
57          * @param inZoom zoom level
58          * @param inX x coordinate
59          * @param inY y coordinate
60          * @return relative file path as String
61          */
62         public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
63         {
64                 return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + getFileExtension(inLayerNum);
65         }
66
67         /**
68          * Checks the given url for having the right prefix and trailing slash
69          * @param inUrl url to check
70          * @return validated url with correct prefix and trailing slash, or null
71          */
72         protected static String fixBaseUrl(String inUrl)
73         {
74                 if (inUrl == null || inUrl.equals("")) {return null;}
75                 String url = inUrl;
76                 // check prefix
77                 try {
78                         new URL(url);
79                 }
80                 catch (MalformedURLException e) {
81                         // add the http protocol
82                         url = "http://" + url;
83                 }
84                 // check trailing /
85                 if (!url.endsWith("/")) {
86                         url = url + "/";
87                 }
88                 return url;
89         }
90
91         /**
92          * Fix the site name by stripping off protocol and www.
93          * This is used to create the file path for disk caching
94          * @param inUrl url to strip
95          * @return stripped url
96          */
97         protected static String fixSiteName(String inUrl)
98         {
99                 if (inUrl == null || inUrl.equals("")) {return null;}
100                 String url = inUrl.toLowerCase();
101                 int idx = url.indexOf("://");
102                 if (idx >= 0) {url = url.substring(idx + 3);}
103                 if (url.startsWith("www.")) {url = url.substring(4);}
104                 return url;
105         }
106
107         /**
108          * @return string which can be written to the Config
109          */
110         public abstract String getConfigString();
111
112         /**
113          * @return semicolon-separated list of base urls in order
114          */
115         public String getSiteStrings()
116         {
117                 String s = "";
118                 for (int i=0; i<getNumLayers(); i++) {
119                         String url = getBaseUrl(i);
120                         if (url != null) {s = s + url + ";";}
121                 }
122                 return s;
123         }
124 }