]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - src/tim/prune/gui/map/OsmMapSource.java
Allow to use maps with custom format
[GpsPrune.git] / src / tim / prune / gui / map / OsmMapSource.java
index 925fcf623ea456154c512c5e5e89454eb5b46bf8..a12116ed1aab0349fc8aedd9c4fc28b8eed097c3 100644 (file)
@@ -1,6 +1,5 @@
 package tim.prune.gui.map;
 
-import java.util.regex.Matcher;
 import tim.prune.I18nManager;
 
 /**
@@ -84,8 +83,8 @@ public class OsmMapSource extends MapSource
                _extensions[0] = inExt1;
                _extensions[1] = inExt2;
                _siteNames = new String[2];
-               _siteNames[0] = fixSiteName(_baseUrls[0]);
-               _siteNames[1] = fixSiteName(_baseUrls[1]);
+               _siteNames[0] = SiteNameUtils.convertUrlToDirectory(_baseUrls[0]);
+               _siteNames[1] = SiteNameUtils.convertUrlToDirectory(_baseUrls[1]);
                // Swap layers if second layer given without first
                if (_baseUrls[0] == null && _baseUrls[1] != null)
                {
@@ -152,12 +151,31 @@ public class OsmMapSource extends MapSource
 
        /**
         * Make the URL to get the specified tile
+        * @param inLayerNum layer number
+        * @param inZoom zoom level
+        * @param inX x coordinate
+        * @param inY y coordinate
+        * @return relative file path as String
         */
        public String makeURL(int inLayerNum, int inZoom, int inX, int inY)
        {
                // Check if the base url has a [1234], if so replace at random
-               StringBuffer url = new StringBuffer();
-               url.append(pickServerUrl(_baseUrls[inLayerNum]));
+               String baseUrl = SiteNameUtils.pickServerUrl(_baseUrls[inLayerNum]);
+               return makeUrl(baseUrl, inLayerNum, inZoom, inX, inY);
+       }
+
+       public String makeUrl(String baseUrl, int inLayerNum, int inZoom, int inX, int inY)
+       {
+               // If the base URL has {x}/{y} placeholders, use them
+               if (baseUrl.contains("{x}")) {
+                       baseUrl = baseUrl.replace("{z}", Integer.toString(inZoom))
+                               .replace("{x}", Integer.toString(inX))
+                               .replace("{y}", Integer.toString(inY));
+                       return baseUrl;
+               }
+
+               // Else simply append the tile indices and file extension
+               StringBuffer url = new StringBuffer(baseUrl);
                url.append(inZoom).append('/').append(inX).append('/').append(inY);
                url.append('.').append(getFileExtension(inLayerNum));
                if (_apiKey != null)
@@ -168,41 +186,31 @@ public class OsmMapSource extends MapSource
        }
 
        /**
-        * @return maximum zoom level
+        * Make a relative file path from the base directory including site name
+        * @param inLayerNum layer number
+        * @param inZoom zoom level
+        * @param inX x coordinate
+        * @param inY y coordinate
+        * @return relative file path as String
         */
-       public final int getMaxZoomLevel()
+       public String makeFilePath(int inLayerNum, int inZoom, int inX, int inY)
        {
-               return _maxZoom;
+               String siteName = getSiteName(inLayerNum);
+               String filePath = makeUrl(siteName, inLayerNum, inZoom, inX, inY);
+               int indexParam = filePath.indexOf("?");
+               if (indexParam > 0)
+               {
+                       filePath = filePath.substring(0, indexParam);
+               }
+               return filePath;
        }
 
        /**
-        * If the base url contains something like [1234], then pick a server
-        * @param inBaseUrl base url
-        * @return modified base url
+        * @return maximum zoom level
         */
-       protected static final String pickServerUrl(String inBaseUrl)
+       public final int getMaxZoomLevel()
        {
-               if (inBaseUrl == null || inBaseUrl.indexOf('[') < 0) {
-                       return inBaseUrl;
-               }
-               // Check for [.*] (once only)
-               // Only need to support one, make things a bit easier
-               final Matcher matcher = WILD_PATTERN.matcher(inBaseUrl);
-               // if not, return base url unchanged
-               if (!matcher.matches()) {
-                       return inBaseUrl;
-               }
-               // if so, pick one at random and replace in the String
-               final String match = matcher.group(2);
-               final int numMatches = match.length();
-               String server = null;
-               if (numMatches > 0)
-               {
-                       int matchNum = (int) Math.floor(Math.random() * numMatches);
-                       server = "" + match.charAt(matchNum);
-               }
-               final String result = matcher.group(1) + (server==null?"":server) + matcher.group(3);
-               return result;
+               return _maxZoom;
        }
 
        /**