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;
|
| 13 | 13 | import java.net.URLConnection; |
| 14 | 14 | import java.text.DecimalFormat; |
| 15 | 15 | import java.text.DecimalFormatSymbols; |
| | 16 | import java.text.MessageFormat; |
| 16 | 17 | import java.text.NumberFormat; |
| 17 | 18 | import java.util.Locale; |
| 18 | 19 | import java.util.concurrent.TimeUnit; |
| … |
… |
public class WMSGrabber extends Grabber {
|
| 33 | 34 | protected String baseURL; |
| 34 | 35 | protected Cache cache = new wmsplugin.Cache(); |
| 35 | 36 | private static Boolean shownWarning = false; |
| | 37 | private boolean urlWithPatterns; |
| 36 | 38 | |
| 37 | 39 | WMSGrabber(String baseURL, Bounds b, Projection proj, |
| 38 | 40 | double pixelPerDegree, GeorefImage image, MapView mv, WMSLayer layer) { |
| 39 | 41 | super(b, proj, pixelPerDegree, image, mv, layer); |
| 40 | 42 | this.baseURL = baseURL; |
| | 43 | /* URL containing placeholders? */ |
| | 44 | urlWithPatterns = baseURL != null && baseURL.contains("{1}"); |
| 41 | 45 | } |
| 42 | 46 | |
| 43 | 47 | public void run() { |
| … |
… |
public class WMSGrabber extends Grabber {
|
| 73 | 77 | protected URL getURL(double w, double s,double e,double n, |
| 74 | 78 | int wi, int ht) throws MalformedURLException { |
| 75 | 79 | 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 | } |
| 85 | 99 | return new URL(str.replace(" ", "%20")); |
| 86 | 100 | } |
| 87 | 101 | |