From e28d025ef7ee6e479b9cf5cf58930a85fc68ff5f Mon Sep 17 00:00:00 2001
From: Michel Marti <mcdmx@users.sf.net>
Date: Thu, 12 Mar 2009 14:25:22 +0100
Subject: [PATCH v2] Teach WMSGrabber to understand formatting patterns in the WMS URL

Some WMS servers are a little picky about the case of parameters, e.g.
they insist that the 'bbox' parameter must be all uppercase. To enable
a little more control over how the URL is formed, this patch allows
the user to put placeholders in the WMS-URL that will be replaced by the
corresponding parameters:

  {0}   current projection
  {1}   bbox coordinates
  {2}   image width
  {3}   image height

A URL with place-holders would look something like this:

   http://myserver/wms?service=wms&version=1.1.1&request=GetMap&layers=1&styles=&srs={0}&format=image/png&BBOX={1}&width={2}&height={3}

The code first checks if the URL contains at least the bbox-placeholder
({1}) before formatting the string accordingly. If the placeholder is not
present, the old method of appending bbox- and size-parameters is used.
---
 src/wmsplugin/WMSGrabber.java |   32 +++++++++++++++++++++++---------
 1 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/src/wmsplugin/WMSGrabber.java b/src/wmsplugin/WMSGrabber.java
index 77afdd8..fc1cbe3 100644
--- a/src/wmsplugin/WMSGrabber.java
+++ b/src/wmsplugin/WMSGrabber.java
@@ -13,6 +13,7 @@ import java.net.URL;
 import java.net.URLConnection;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
+import java.text.MessageFormat;
 import java.text.NumberFormat;
 import java.util.Locale;
 import java.util.concurrent.TimeUnit;
@@ -33,11 +34,14 @@ public class WMSGrabber extends Grabber {
     protected String baseURL;
     protected Cache cache = new wmsplugin.Cache();
     private static Boolean shownWarning = false;
+    private boolean urlWithPatterns;
 
     WMSGrabber(String baseURL, Bounds b, Projection proj,
             double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) {
         super(b, proj, pixelPerDegree, image, mv, layer);
         this.baseURL = baseURL;
+        /* URL containing placeholders? */
+        urlWithPatterns = baseURL != null && baseURL.contains("{1}");
     }
 
     public void run() {
@@ -73,15 +77,25 @@ public class WMSGrabber extends Grabber {
     protected URL getURL(double w, double s,double e,double n,
             int wi, int ht) throws MalformedURLException {
         String str = baseURL;
-        if(!str.endsWith("?"))
-            str += "&";
-        str += "bbox="
-            + latLonFormat.format(w) + ","
-            + latLonFormat.format(s) + ","
-            + latLonFormat.format(e) + ","
-            + latLonFormat.format(n)
-            + getProjection(baseURL, false)
-            + "&width=" + wi + "&height=" + ht;
+        String bbox = latLonFormat.format(w) + "," +
+                      latLonFormat.format(s) + "," +
+                      latLonFormat.format(e) + "," +
+                      latLonFormat.format(n);
+
+        if (urlWithPatterns) {
+            String proj = Main.proj.toCode();
+            if(proj.equals("EPSG:3785")) // don't use mercator code
+                proj = "EPSG:4326";
+
+            str = MessageFormat.format(str, proj, bbox, wi, ht);
+        } else {
+            if(!str.endsWith("?"))
+                str += "&";
+            str += "bbox="
+                + bbox
+                + getProjection(baseURL, false)
+                + "&width=" + wi + "&height=" + ht;
+        }
         return new URL(str.replace(" ", "%20"));
     }
 
-- 
1.6.1.2

