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 protected static String fixBaseUrl(String inUrl)
74 if (inUrl == null || inUrl.equals("")) {return null;}
80 catch (MalformedURLException e) {
81 // add the http protocol
82 url = "http://" + url;
85 if (!url.endsWith("/")) {
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
97 protected static String fixSiteName(String inUrl)
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);}
108 * @return string which can be written to the Config
110 public abstract String getConfigString();
113 * @return semicolon-separated list of base urls in order
115 public String getSiteStrings()
118 for (int i=0; i<getNumLayers(); i++) {
119 String url = getBaseUrl(i);
120 if (url != null) {s = s + url + ";";}