1 package tim.prune.gui.map;
3 import java.net.MalformedURLException;
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
12 public abstract class MapSource
15 * @return the number of layers used in this source
17 public abstract int getNumLayers();
20 * @return the name of the source
22 public abstract String getName();
25 * @return the base url for the specified layer
27 public abstract String getBaseUrl(int inLayerNum);
30 * @return the site name for the specified layer
32 public abstract String getSiteName(int inLayerNum);
35 * @return the file extension for the specified layer
37 public abstract String getFileExtension(int inLayerNum);
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
47 public abstract String makeURL(int inLayerNum, int inZoom, int inX, int inY);
50 * @return the maximum zoom level for this source
52 public abstract int getMaxZoomLevel();
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
62 public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
64 return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + getFileExtension(inLayerNum);
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
72 public static String fixBaseUrl(String inUrl)
74 if (inUrl == null || inUrl.equals("")) {return null;}
75 String urlstr = inUrl;
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;
87 if (!urlstr.endsWith("/")) {
88 urlstr = urlstr + "/";
90 // Validate current url, return null if not ok
92 URL url = new URL(urlstr);
93 // url host must contain a dot
94 if (url.getHost().indexOf('.') < 0) {return null;}
96 catch (MalformedURLException e) {
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
108 protected static String fixSiteName(String inUrl)
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);}
119 * @return string which can be written to the Config
121 public abstract String getConfigString();
124 * @return semicolon-separated list of base urls in order
126 public String getSiteStrings()
128 StringBuilder sb = new StringBuilder();
129 for (int i=0; i<getNumLayers(); i++) {
130 String url = getBaseUrl(i);
136 return sb.toString();