source: josm/trunk/src/org/openstreetmap/josm/io/imagery/WMSGrabber.java@ 6248

Last change on this file since 6248 was 6248, checked in by Don-vip, 11 years ago

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.imagery;
3
4import java.awt.image.BufferedImage;
5import java.io.BufferedReader;
6import java.io.ByteArrayInputStream;
7import java.io.ByteArrayOutputStream;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.net.HttpURLConnection;
12import java.net.MalformedURLException;
13import java.net.URL;
14import java.net.URLConnection;
15import java.text.DecimalFormat;
16import java.text.DecimalFormatSymbols;
17import java.text.NumberFormat;
18import java.util.HashMap;
19import java.util.Locale;
20import java.util.Map;
21import java.util.Map.Entry;
22import java.util.regex.Matcher;
23import java.util.regex.Pattern;
24
25import javax.imageio.ImageIO;
26
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.data.coor.EastNorth;
29import org.openstreetmap.josm.data.coor.LatLon;
30import org.openstreetmap.josm.data.imagery.GeorefImage.State;
31import org.openstreetmap.josm.data.imagery.ImageryInfo;
32import org.openstreetmap.josm.gui.MapView;
33import org.openstreetmap.josm.gui.layer.WMSLayer;
34import org.openstreetmap.josm.io.OsmTransferException;
35import org.openstreetmap.josm.io.ProgressInputStream;
36import org.openstreetmap.josm.tools.Utils;
37
38
39public class WMSGrabber extends Grabber {
40
41 protected String baseURL;
42 private ImageryInfo info;
43 private Map<String, String> props = new HashMap<String, String>();
44
45 public WMSGrabber(MapView mv, WMSLayer layer, boolean localOnly) {
46 super(mv, layer, localOnly);
47 this.info = layer.getInfo();
48 this.baseURL = info.getUrl();
49 if(layer.getInfo().getCookies() != null && !layer.getInfo().getCookies().isEmpty()) {
50 props.put("Cookie", layer.getInfo().getCookies());
51 }
52 Pattern pattern = Pattern.compile("\\{header\\(([^,]+),([^}]+)\\)\\}");
53 StringBuffer output = new StringBuffer();
54 Matcher matcher = pattern.matcher(this.baseURL);
55 while (matcher.find()) {
56 props.put(matcher.group(1),matcher.group(2));
57 matcher.appendReplacement(output, "");
58 }
59 matcher.appendTail(output);
60 this.baseURL = output.toString();
61 }
62
63 @Override
64 void fetch(WMSRequest request, int attempt) throws Exception{
65 URL url = null;
66 try {
67 url = getURL(
68 b.minEast, b.minNorth,
69 b.maxEast, b.maxNorth,
70 width(), height());
71 request.finish(State.IMAGE, grab(request, url, attempt));
72
73 } catch(Exception e) {
74 e.printStackTrace();
75 throw new Exception(e.getMessage() + "\nImage couldn't be fetched: " + (url != null ? url.toString() : ""));
76 }
77 }
78
79 public static final NumberFormat latLonFormat = new DecimalFormat("###0.0000000",
80 new DecimalFormatSymbols(Locale.US));
81
82 protected URL getURL(double w, double s,double e,double n,
83 int wi, int ht) throws MalformedURLException {
84 String myProj = Main.getProjection().toCode();
85 if (!info.getServerProjections().contains(myProj) && "EPSG:3857".equals(Main.getProjection().toCode())) {
86 LatLon sw = Main.getProjection().eastNorth2latlon(new EastNorth(w, s));
87 LatLon ne = Main.getProjection().eastNorth2latlon(new EastNorth(e, n));
88 myProj = "EPSG:4326";
89 s = sw.lat();
90 w = sw.lon();
91 n = ne.lat();
92 e = ne.lon();
93 }
94 if (myProj.equals("EPSG:4326") && !info.getServerProjections().contains(myProj) && info.getServerProjections().contains("CRS:84")) {
95 myProj = "CRS:84";
96 }
97
98 // Bounding box coordinates have to be switched for WMS 1.3.0 EPSG:4326.
99 //
100 // Background:
101 //
102 // bbox=x_min,y_min,x_max,y_max
103 //
104 // SRS=... is WMS 1.1.1
105 // CRS=... is WMS 1.3.0
106 //
107 // The difference:
108 // For SRS x is east-west and y is north-south
109 // For CRS x and y are as specified by the EPSG
110 // E.g. [1] lists lat as first coordinate axis and lot as second, so it is switched for EPSG:4326.
111 // For most other EPSG code there seems to be no difference.
112 // [1] http://www.epsg-registry.org/report.htm?type=selection&entity=urn:ogc:def:crs:EPSG::4326&reportDetail=short&style=urn:uuid:report-style:default-with-code&style_name=OGP%20Default%20With%20Code&title=EPSG:4326
113 boolean switchLatLon = false;
114 if (baseURL.toLowerCase().contains("crs=epsg:4326")) {
115 switchLatLon = true;
116 } else if (baseURL.toLowerCase().contains("crs=") && myProj.equals("EPSG:4326")) {
117 switchLatLon = true;
118 }
119 String bbox;
120 if (switchLatLon) {
121 bbox = String.format("%s,%s,%s,%s", latLonFormat.format(s), latLonFormat.format(w), latLonFormat.format(n), latLonFormat.format(e));
122 } else {
123 bbox = String.format("%s,%s,%s,%s", latLonFormat.format(w), latLonFormat.format(s), latLonFormat.format(e), latLonFormat.format(n));
124 }
125 return new URL(baseURL.replaceAll("\\{proj(\\([^})]+\\))?\\}", myProj)
126 .replaceAll("\\{bbox\\}", bbox)
127 .replaceAll("\\{w\\}", latLonFormat.format(w))
128 .replaceAll("\\{s\\}", latLonFormat.format(s))
129 .replaceAll("\\{e\\}", latLonFormat.format(e))
130 .replaceAll("\\{n\\}", latLonFormat.format(n))
131 .replaceAll("\\{width\\}", String.valueOf(wi))
132 .replaceAll("\\{height\\}", String.valueOf(ht))
133 .replace(" ", "%20"));
134 }
135
136 @Override
137 public boolean loadFromCache(WMSRequest request) {
138 BufferedImage cached = layer.cache.getExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth);
139
140 if (cached != null) {
141 request.finish(State.IMAGE, cached);
142 return true;
143 } else if (request.isAllowPartialCacheMatch()) {
144 BufferedImage partialMatch = layer.cache.getPartialMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth);
145 if (partialMatch != null) {
146 request.finish(State.PARTLY_IN_CACHE, partialMatch);
147 return true;
148 }
149 }
150
151 if((!request.isReal() && !layer.hasAutoDownload())){
152 request.finish(State.NOT_IN_CACHE, null);
153 return true;
154 }
155
156 return false;
157 }
158
159 protected BufferedImage grab(WMSRequest request, URL url, int attempt) throws IOException, OsmTransferException {
160 Main.info("Grabbing WMS " + (attempt > 1? "(attempt " + attempt + ") ":"") + url);
161
162 HttpURLConnection conn = Utils.openHttpConnection(url);
163 for(Entry<String, String> e : props.entrySet()) {
164 conn.setRequestProperty(e.getKey(), e.getValue());
165 }
166 conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15) * 1000);
167 conn.setReadTimeout(Main.pref.getInteger("socket.timeout.read", 30) * 1000);
168
169 String contentType = conn.getHeaderField("Content-Type");
170 if( conn.getResponseCode() != 200
171 || contentType != null && !contentType.startsWith("image") )
172 throw new IOException(readException(conn));
173
174 ByteArrayOutputStream baos = new ByteArrayOutputStream();
175 InputStream is = new ProgressInputStream(conn, null);
176 try {
177 Utils.copyStream(is, baos);
178 } finally {
179 Utils.close(is);
180 }
181
182 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
183 BufferedImage img = layer.normalizeImage(ImageIO.read(bais));
184 bais.reset();
185 layer.cache.saveToCache(layer.isOverlapEnabled()?img:null, bais, Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth);
186 return img;
187 }
188
189 protected String readException(URLConnection conn) throws IOException {
190 StringBuilder exception = new StringBuilder();
191 InputStream in = conn.getInputStream();
192 BufferedReader br = new BufferedReader(new InputStreamReader(in));
193 try {
194 String line = null;
195 while( (line = br.readLine()) != null) {
196 // filter non-ASCII characters and control characters
197 exception.append(line.replaceAll("[^\\p{Print}]", ""));
198 exception.append('\n');
199 }
200 return exception.toString();
201 } finally {
202 Utils.close(br);
203 }
204 }
205}
Note: See TracBrowser for help on using the repository browser.