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