]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MapSource.java
35590a6e9a44964179050986d7ce6dc7c8475386
[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         public static String fixBaseUrl(String inUrl)
73         {
74                 if (inUrl == null || inUrl.equals("")) {return null;}
75                 String urlstr = inUrl;
76                 // check prefix
77                 try {
78                         new URL(urlstr);
79                 }
80                 catch (MalformedURLException e) {
81                         // fail if protocol specified
82                         if (urlstr.indexOf("://") >= 0) {return null;}
83                         // add the http protocol
84                         urlstr = "http://" + urlstr;
85                 }
86                 // check trailing /
87                 if (!urlstr.endsWith("/")) {
88                         urlstr = urlstr + "/";
89                 }
90                 // Validate current url, return null if not ok
91                 try {
92                         URL url = new URL(urlstr);
93                         // url host must contain a dot
94                         if (url.getHost().indexOf('.') < 0) {return null;}
95                 }
96                 catch (MalformedURLException e) {
97                         urlstr = null;
98                 }
99                 return urlstr;
100         }
101
102         /**
103          * Fix the site name by stripping off protocol and www.
104          * This is used to create the file path for disk caching
105          * @param inUrl url to strip
106          * @return stripped url
107          */
108         protected static String fixSiteName(String inUrl)
109         {
110                 if (inUrl == null || inUrl.equals("")) {return null;}
111                 String url = inUrl.toLowerCase();
112                 int idx = url.indexOf("://");
113                 if (idx >= 0) {url = url.substring(idx + 3);}
114                 if (url.startsWith("www.")) {url = url.substring(4);}
115                 return url;
116         }
117
118         /**
119          * @return string which can be written to the Config
120          */
121         public abstract String getConfigString();
122
123         /**
124          * @return semicolon-separated list of base urls in order
125          */
126         public String getSiteStrings()
127         {
128                 StringBuilder sb = new StringBuilder();
129                 for (int i=0; i<getNumLayers(); i++) {
130                         String url = getBaseUrl(i);
131                         if (url != null) {
132                                 sb.append(url);
133                                 sb.append(';');
134                         }
135                 }
136                 return sb.toString();
137         }
138 }