]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/map/OsmMapSource.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / gui / map / OsmMapSource.java
index 09def4597eb72977357930b9ea492c1c9d198a45..925fcf623ea456154c512c5e5e89454eb5b46bf8 100644 (file)
@@ -1,5 +1,6 @@
 package tim.prune.gui.map;
 
+import java.util.regex.Matcher;
 import tim.prune.I18nManager;
 
 /**
@@ -18,6 +19,9 @@ public class OsmMapSource extends MapSource
        private String[] _siteNames = null;
        /** Maximum zoom level */
        private int _maxZoom = 0;
+       /** API key, usually remains empty */
+       private String _apiKey = null;
+
 
        /**
         * Constructor giving single name and url
@@ -39,9 +43,9 @@ public class OsmMapSource extends MapSource
        public OsmMapSource(String inName, String inStr1, String inStr2, int inMaxZoom)
        {
                if (inStr2 != null && inStr2.length() == 3)
-                       init(inName, inStr1, inStr2, null, null, 18);
+                       init(inName, inStr1, inStr2, null, null, inMaxZoom);
                else
-                       init(inName, inStr1, "png", inStr2, "png", 18);
+                       init(inName, inStr1, "png", inStr2, "png", inMaxZoom);
        }
 
        /**
@@ -92,6 +96,12 @@ public class OsmMapSource extends MapSource
                _maxZoom = inMaxZoom;
        }
 
+       /** Set the API key (if required) */
+       public void setApiKey(String inKey)
+       {
+               _apiKey = inKey;
+       }
+
        /**
         * Construct a new map source from its config string
         * @param inConfigString string from Config, separated by semicolons
@@ -145,7 +155,16 @@ public class OsmMapSource extends MapSource
         */
        public String makeURL(int inLayerNum, int inZoom, int inX, int inY)
        {
-               return _baseUrls[inLayerNum] + inZoom + "/" + inX + "/" + inY + "." + getFileExtension(inLayerNum);
+               // Check if the base url has a [1234], if so replace at random
+               StringBuffer url = new StringBuffer();
+               url.append(pickServerUrl(_baseUrls[inLayerNum]));
+               url.append(inZoom).append('/').append(inX).append('/').append(inY);
+               url.append('.').append(getFileExtension(inLayerNum));
+               if (_apiKey != null)
+               {
+                       url.append("?apikey=").append(_apiKey);
+               }
+               return url.toString();
        }
 
        /**
@@ -156,6 +175,35 @@ public class OsmMapSource extends MapSource
                return _maxZoom;
        }
 
+       /**
+        * If the base url contains something like [1234], then pick a server
+        * @param inBaseUrl base url
+        * @return modified base url
+        */
+       protected static final String pickServerUrl(String inBaseUrl)
+       {
+               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 semicolon-separated list of all fields