Ticket #2291: 0001-Teach-WMSGrabber-to-understand-formatting-patterns-i.patch

File 0001-Teach-WMSGrabber-to-understand-formatting-patterns-i.patch, 3.5 KB (added by anonymous, 17 years ago)
  • src/wmsplugin/WMSGrabber.java

    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 b import java.net.URL;  
    1313import java.net.URLConnection;
    1414import java.text.DecimalFormat;
    1515import java.text.DecimalFormatSymbols;
     16import java.text.MessageFormat;
    1617import java.text.NumberFormat;
    1718import java.util.Locale;
    1819import java.util.concurrent.TimeUnit;
    public class WMSGrabber extends Grabber {  
    3334    protected String baseURL;
    3435    protected Cache cache = new wmsplugin.Cache();
    3536    private static Boolean shownWarning = false;
     37    private boolean urlWithPatterns;
    3638
    3739    WMSGrabber(String baseURL, Bounds b, Projection proj,
    3840            double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) {
    3941        super(b, proj, pixelPerDegree, image, mv, layer);
    4042        this.baseURL = baseURL;
     43        /* URL containing placeholders? */
     44        urlWithPatterns = baseURL != null && baseURL.contains("{1}");
    4145    }
    4246
    4347    public void run() {
    public class WMSGrabber extends Grabber {  
    7377    protected URL getURL(double w, double s,double e,double n,
    7478            int wi, int ht) throws MalformedURLException {
    7579        String str = baseURL;
    76         if(!str.endsWith("?"))
    77             str += "&";
    78         str += "bbox="
    79             + latLonFormat.format(w) + ","
    80             + latLonFormat.format(s) + ","
    81             + latLonFormat.format(e) + ","
    82             + latLonFormat.format(n)
    83             + getProjection(baseURL, false)
    84             + "&width=" + wi + "&height=" + ht;
     80        String bbox = latLonFormat.format(w) + "," +
     81                      latLonFormat.format(s) + "," +
     82                      latLonFormat.format(e) + "," +
     83                      latLonFormat.format(n);
     84
     85        if (urlWithPatterns) {
     86            String proj = Main.proj.toCode();
     87            if(proj.equals("EPSG:3785")) // don't use mercator code
     88                proj = "EPSG:4326";
     89
     90            str = MessageFormat.format(str, proj, bbox, wi, ht);
     91        } else {
     92            if(!str.endsWith("?"))
     93                str += "&";
     94            str += "bbox="
     95                + bbox
     96                + getProjection(baseURL, false)
     97                + "&width=" + wi + "&height=" + ht;
     98        }
    8599        return new URL(str.replace(" ", "%20"));
    86100    }
    87101