X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fgui%2Fmap%2FOsmMapSource.java;h=2f8d0ba97980c366ebaf824a515c0912e12f2174;hp=09def4597eb72977357930b9ea492c1c9d198a45;hb=4d5796d02a15808311c09448d79e6e7d1de9d636;hpb=f1b92378a792131ac8fb33a869405851d5b2d1f7 diff --git a/tim/prune/gui/map/OsmMapSource.java b/tim/prune/gui/map/OsmMapSource.java index 09def45..2f8d0ba 100644 --- a/tim/prune/gui/map/OsmMapSource.java +++ b/tim/prune/gui/map/OsmMapSource.java @@ -1,5 +1,6 @@ package tim.prune.gui.map; +import java.util.regex.Matcher; import tim.prune.I18nManager; /** @@ -145,7 +146,9 @@ 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 + return pickServerUrl(_baseUrls[inLayerNum]) + + inZoom + "/" + inX + "/" + inY + "." + getFileExtension(inLayerNum); } /** @@ -156,6 +159,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