]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/MapSource.java
e3ac2ed2227a1825d01526c87800c3abdd9819d4
[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         /** File extensions */
15         protected String[] _extensions = null;
16
17
18         /**
19          * @return the number of layers used in this source
20          */
21         public abstract int getNumLayers();
22
23         /**
24          * @return the name of the source
25          */
26         public abstract String getName();
27
28         /**
29          * @return the base url for the specified layer
30          */
31         public abstract String getBaseUrl(int inLayerNum);
32
33         /**
34          * @return the site name for the specified layer
35          */
36         public abstract String getSiteName(int inLayerNum);
37
38         /**
39          * @return the file extension for the specified layer
40          */
41         public final String getFileExtension(int inLayerNum) {
42                 return _extensions[inLayerNum];
43         }
44
45         /**
46          * Make the URL to get the specified tile
47          * @param inLayerNum number of layer, from 0 (base) to getNumLayers-1 (top)
48          * @param inZoom zoom level
49          * @param inX x coordinate of tile
50          * @param inY y coordinate of tile
51          * @return URL as string
52          */
53         public abstract String makeURL(int inLayerNum, int inZoom, int inX, int inY);
54
55         /**
56          * @return the maximum zoom level for this source
57          */
58         public abstract int getMaxZoomLevel();
59
60         /**
61          * Make a relative file path from the base directory including site name
62          * @param inLayerNum layer number
63          * @param inZoom zoom level
64          * @param inX x coordinate
65          * @param inY y coordinate
66          * @return relative file path as String
67          */
68         public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
69         {
70                 return getSiteName(inLayerNum) + inZoom + "/" + inX + "/" + inY + "." + getFileExtension(inLayerNum);
71         }
72
73         /**
74          * Checks the given url for having the right prefix and trailing slash
75          * @param inUrl url to check
76          * @return validated url with correct prefix and trailing slash, or null
77          */
78         public static String fixBaseUrl(String inUrl)
79         {
80                 if (inUrl == null || inUrl.equals("")) {return null;}
81                 String urlstr = inUrl;
82                 // check prefix
83                 try {
84                         new URL(urlstr);
85                 }
86                 catch (MalformedURLException e) {
87                         // fail if protocol specified
88                         if (urlstr.indexOf("://") >= 0) {return null;}
89                         // add the http protocol
90                         urlstr = "http://" + urlstr;
91                 }
92                 // check trailing /
93                 if (!urlstr.endsWith("/")) {
94                         urlstr = urlstr + "/";
95                 }
96                 // Validate current url, return null if not ok
97                 try {
98                         URL url = new URL(urlstr);
99                         // url host must contain a dot
100                         if (url.getHost().indexOf('.') < 0) {return null;}
101                 }
102                 catch (MalformedURLException e) {
103                         urlstr = null;
104                 }
105                 return urlstr;
106         }
107
108         /**
109          * Fix the site name by stripping off protocol and www.
110          * This is used to create the file path for disk caching
111          * @param inUrl url to strip
112          * @return stripped url
113          */
114         protected static String fixSiteName(String inUrl)
115         {
116                 if (inUrl == null || inUrl.equals("")) {return null;}
117                 String url = inUrl.toLowerCase();
118                 int idx = url.indexOf("://");
119                 if (idx >= 0) {url = url.substring(idx + 3);}
120                 if (url.startsWith("www.")) {url = url.substring(4);}
121                 return url;
122         }
123
124         /**
125          * @return string which can be written to the Config
126          */
127         public abstract String getConfigString();
128
129         /**
130          * @return semicolon-separated list of base urls and extensions in order
131          */
132         public String getSiteStrings()
133         {
134                 StringBuilder sb = new StringBuilder();
135                 for (int i=0; i<getNumLayers(); i++)
136                 {
137                         String url = getBaseUrl(i);
138                         if (url != null)
139                         {
140                                 sb.append(url);
141                                 sb.append(';');
142                                 sb.append(getFileExtension(i));
143                                 sb.append(';');
144                         }
145                 }
146                 return sb.toString();
147         }
148 }