1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.text.DecimalFormat;
|
---|
8 | import java.text.DecimalFormatSymbols;
|
---|
9 | import java.text.NumberFormat;
|
---|
10 | import java.util.Locale;
|
---|
11 | import java.util.Map;
|
---|
12 | import java.util.Set;
|
---|
13 | import java.util.TreeSet;
|
---|
14 | import java.util.concurrent.ConcurrentHashMap;
|
---|
15 | import java.util.regex.Matcher;
|
---|
16 | import java.util.regex.Pattern;
|
---|
17 |
|
---|
18 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
19 | import org.openstreetmap.gui.jmapviewer.TileXY;
|
---|
20 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
21 | import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource;
|
---|
22 | import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.data.Bounds;
|
---|
25 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
26 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
27 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
28 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
29 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
---|
30 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Tile Source handling WMS providers
|
---|
34 | *
|
---|
35 | * @author Wiktor Niesiobędzki
|
---|
36 | * @since 8526
|
---|
37 | */
|
---|
38 | public class TemplatedWMSTileSource extends TMSTileSource implements TemplatedTileSource {
|
---|
39 | private final Map<String, String> headers = new ConcurrentHashMap<>();
|
---|
40 | private final Set<String> serverProjections;
|
---|
41 | private EastNorth anchorPosition;
|
---|
42 | private int[] tileXMin;
|
---|
43 | private int[] tileYMin;
|
---|
44 | private int[] tileXMax;
|
---|
45 | private int[] tileYMax;
|
---|
46 | private double[] degreesPerTile;
|
---|
47 |
|
---|
48 | private static final Pattern PATTERN_HEADER = Pattern.compile("\\{header\\(([^,]+),([^}]+)\\)\\}");
|
---|
49 | private static final Pattern PATTERN_PROJ = Pattern.compile("\\{proj\\}");
|
---|
50 | private static final Pattern PATTERN_BBOX = Pattern.compile("\\{bbox\\}");
|
---|
51 | private static final Pattern PATTERN_W = Pattern.compile("\\{w\\}");
|
---|
52 | private static final Pattern PATTERN_S = Pattern.compile("\\{s\\}");
|
---|
53 | private static final Pattern PATTERN_E = Pattern.compile("\\{e\\}");
|
---|
54 | private static final Pattern PATTERN_N = Pattern.compile("\\{n\\}");
|
---|
55 | private static final Pattern PATTERN_WIDTH = Pattern.compile("\\{width\\}");
|
---|
56 | private static final Pattern PATTERN_HEIGHT = Pattern.compile("\\{height\\}");
|
---|
57 | private static final Pattern PATTERN_PARAM = Pattern.compile("\\{([^}]+)\\}");
|
---|
58 |
|
---|
59 | private static final NumberFormat latLonFormat = new DecimalFormat("###0.0000000", new DecimalFormatSymbols(Locale.US));
|
---|
60 |
|
---|
61 | private static final Pattern[] ALL_PATTERNS = {
|
---|
62 | PATTERN_HEADER, PATTERN_PROJ, PATTERN_BBOX, PATTERN_W, PATTERN_S, PATTERN_E, PATTERN_N, PATTERN_WIDTH, PATTERN_HEIGHT
|
---|
63 | };
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Constant taken from OGC WMTS Implementation Specification (http://www.opengeospatial.org/standards/wmts)
|
---|
67 | * From table E.4 - Definition of Well-known scale set GoogleMapsCompatibile
|
---|
68 | *
|
---|
69 | * As higher zoom levels have denominator divided by 2, we keep only zoom level 1 in the code
|
---|
70 | */
|
---|
71 | private static final float SCALE_DENOMINATOR_ZOOM_LEVEL_1 = 559082264.0287178f;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Creates a tile source based on imagery info
|
---|
75 | * @param info imagery info
|
---|
76 | */
|
---|
77 | public TemplatedWMSTileSource(ImageryInfo info) {
|
---|
78 | super(info);
|
---|
79 | this.serverProjections = new TreeSet<>(info.getServerProjections());
|
---|
80 | handleTemplate();
|
---|
81 | initProjection();
|
---|
82 | // FIXME: remove in September 2015, when ImageryPreferenceEntry.tileSize will be initialized to -1 instead to 256
|
---|
83 | // need to leave it as it is to keep compatibility between tested and latest JOSM versions
|
---|
84 | tileSize = WMSLayer.PROP_IMAGE_SIZE.get();
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Initializes class with current projection in JOSM. This call is needed every time projection changes.
|
---|
89 | */
|
---|
90 | public void initProjection() {
|
---|
91 | initProjection(Main.getProjection());
|
---|
92 | }
|
---|
93 |
|
---|
94 | private void initAnchorPosition(Projection proj) {
|
---|
95 | Bounds worldBounds = proj.getWorldBoundsLatLon();
|
---|
96 | EastNorth min = proj.latlon2eastNorth(worldBounds.getMin());
|
---|
97 | EastNorth max = proj.latlon2eastNorth(worldBounds.getMax());
|
---|
98 | this.anchorPosition = new EastNorth(min.east(), max.north());
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Initializes class with projection in JOSM. This call is needed every time projection changes.
|
---|
103 | * @param proj new projection that shall be used for computations
|
---|
104 | */
|
---|
105 | public void initProjection(Projection proj) {
|
---|
106 | initAnchorPosition(proj);
|
---|
107 | ProjectionBounds worldBounds = proj.getWorldBoundsBoxEastNorth();
|
---|
108 |
|
---|
109 | EastNorth topLeft = new EastNorth(worldBounds.getMin().east(), worldBounds.getMax().north());
|
---|
110 | EastNorth bottomRight = new EastNorth(worldBounds.getMax().east(), worldBounds.getMin().north());
|
---|
111 |
|
---|
112 | // use 256 as "tile size" to keep the scale in line with default tiles in Mercator projection
|
---|
113 | double crsScale = 256 * 0.28e-03 / proj.getMetersPerUnit();
|
---|
114 | tileXMin = new int[getMaxZoom() + 1];
|
---|
115 | tileYMin = new int[getMaxZoom() + 1];
|
---|
116 | tileXMax = new int[getMaxZoom() + 1];
|
---|
117 | tileYMax = new int[getMaxZoom() + 1];
|
---|
118 | degreesPerTile = new double[getMaxZoom() + 1];
|
---|
119 |
|
---|
120 | for (int zoom = 1; zoom <= getMaxZoom(); zoom++) {
|
---|
121 | // use well known scale set "GoogleCompatibile" from OGC WMTS spec to calculate number of tiles per zoom level
|
---|
122 | // this makes the zoom levels "glued" to standard TMS zoom levels
|
---|
123 | degreesPerTile[zoom] = (SCALE_DENOMINATOR_ZOOM_LEVEL_1 / Math.pow(2, zoom - 1)) * crsScale;
|
---|
124 | TileXY minTileIndex = eastNorthToTileXY(topLeft, zoom);
|
---|
125 | tileXMin[zoom] = minTileIndex.getXIndex();
|
---|
126 | tileYMin[zoom] = minTileIndex.getYIndex();
|
---|
127 | TileXY maxTileIndex = eastNorthToTileXY(bottomRight, zoom);
|
---|
128 | tileXMax[zoom] = maxTileIndex.getXIndex();
|
---|
129 | tileYMax[zoom] = maxTileIndex.getYIndex();
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public int getDefaultTileSize() {
|
---|
135 | return WMSLayer.PROP_IMAGE_SIZE.get();
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public String getTileUrl(int zoom, int tilex, int tiley) {
|
---|
140 | String myProjCode = Main.getProjection().toCode();
|
---|
141 |
|
---|
142 | EastNorth nw = getTileEastNorth(tilex, tiley, zoom);
|
---|
143 | EastNorth se = getTileEastNorth(tilex + 1, tiley + 1, zoom);
|
---|
144 |
|
---|
145 | double w = nw.getX();
|
---|
146 | double n = nw.getY();
|
---|
147 |
|
---|
148 | double s = se.getY();
|
---|
149 | double e = se.getX();
|
---|
150 |
|
---|
151 | if (!serverProjections.contains(myProjCode) && serverProjections.contains("EPSG:4326") && "EPSG:3857".equals(myProjCode)) {
|
---|
152 | LatLon swll = Main.getProjection().eastNorth2latlon(new EastNorth(w, s));
|
---|
153 | LatLon nell = Main.getProjection().eastNorth2latlon(new EastNorth(e, n));
|
---|
154 | myProjCode = "EPSG:4326";
|
---|
155 | s = swll.lat();
|
---|
156 | w = swll.lon();
|
---|
157 | n = nell.lat();
|
---|
158 | e = nell.lon();
|
---|
159 | }
|
---|
160 |
|
---|
161 | if ("EPSG:4326".equals(myProjCode) && !serverProjections.contains(myProjCode) && serverProjections.contains("CRS:84")) {
|
---|
162 | myProjCode = "CRS:84";
|
---|
163 | }
|
---|
164 |
|
---|
165 | // Bounding box coordinates have to be switched for WMS 1.3.0 EPSG:4326.
|
---|
166 | //
|
---|
167 | // Background:
|
---|
168 | //
|
---|
169 | // bbox=x_min,y_min,x_max,y_max
|
---|
170 | //
|
---|
171 | // SRS=... is WMS 1.1.1
|
---|
172 | // CRS=... is WMS 1.3.0
|
---|
173 | //
|
---|
174 | // The difference:
|
---|
175 | // For SRS x is east-west and y is north-south
|
---|
176 | // For CRS x and y are as specified by the EPSG
|
---|
177 | // E.g. [1] lists lat as first coordinate axis and lot as second, so it is switched for EPSG:4326.
|
---|
178 | // For most other EPSG code there seems to be no difference.
|
---|
179 | // CHECKSTYLE.OFF: LineLength
|
---|
180 | // [1] https://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
|
---|
181 | // CHECKSTYLE.ON: LineLength
|
---|
182 | boolean switchLatLon = false;
|
---|
183 | if (baseUrl.toLowerCase(Locale.US).contains("crs=epsg:4326")) {
|
---|
184 | switchLatLon = true;
|
---|
185 | } else if (baseUrl.toLowerCase(Locale.US).contains("crs=")) {
|
---|
186 | // assume WMS 1.3.0
|
---|
187 | switchLatLon = Main.getProjection().switchXY();
|
---|
188 | }
|
---|
189 | String bbox;
|
---|
190 | if (switchLatLon) {
|
---|
191 | bbox = String.format("%s,%s,%s,%s", latLonFormat.format(s), latLonFormat.format(w), latLonFormat.format(n), latLonFormat.format(e));
|
---|
192 | } else {
|
---|
193 | bbox = String.format("%s,%s,%s,%s", latLonFormat.format(w), latLonFormat.format(s), latLonFormat.format(e), latLonFormat.format(n));
|
---|
194 | }
|
---|
195 |
|
---|
196 | // Using StringBuffer and generic PATTERN_PARAM matcher gives 2x performance improvement over replaceAll
|
---|
197 | StringBuffer url = new StringBuffer(baseUrl.length());
|
---|
198 | Matcher matcher = PATTERN_PARAM.matcher(baseUrl);
|
---|
199 | while (matcher.find()) {
|
---|
200 | String replacement;
|
---|
201 | switch (matcher.group(1)) {
|
---|
202 | case "proj":
|
---|
203 | replacement = myProjCode;
|
---|
204 | break;
|
---|
205 | case "bbox":
|
---|
206 | replacement = bbox;
|
---|
207 | break;
|
---|
208 | case "w":
|
---|
209 | replacement = latLonFormat.format(w);
|
---|
210 | break;
|
---|
211 | case "s":
|
---|
212 | replacement = latLonFormat.format(s);
|
---|
213 | break;
|
---|
214 | case "e":
|
---|
215 | replacement = latLonFormat.format(e);
|
---|
216 | break;
|
---|
217 | case "n":
|
---|
218 | replacement = latLonFormat.format(n);
|
---|
219 | break;
|
---|
220 | case "width":
|
---|
221 | case "height":
|
---|
222 | replacement = String.valueOf(getTileSize());
|
---|
223 | break;
|
---|
224 | default:
|
---|
225 | replacement = '{' + matcher.group(1) + '}';
|
---|
226 | }
|
---|
227 | matcher.appendReplacement(url, replacement);
|
---|
228 | }
|
---|
229 | matcher.appendTail(url);
|
---|
230 | return url.toString().replace(" ", "%20");
|
---|
231 | }
|
---|
232 |
|
---|
233 | @Override
|
---|
234 | public String getTileId(int zoom, int tilex, int tiley) {
|
---|
235 | return getTileUrl(zoom, tilex, tiley);
|
---|
236 | }
|
---|
237 |
|
---|
238 | @Override
|
---|
239 | public ICoordinate tileXYToLatLon(Tile tile) {
|
---|
240 | return tileXYToLatLon(tile.getXtile(), tile.getYtile(), tile.getZoom());
|
---|
241 | }
|
---|
242 |
|
---|
243 | @Override
|
---|
244 | public ICoordinate tileXYToLatLon(TileXY xy, int zoom) {
|
---|
245 | return tileXYToLatLon(xy.getXIndex(), xy.getYIndex(), zoom);
|
---|
246 | }
|
---|
247 |
|
---|
248 | @Override
|
---|
249 | public ICoordinate tileXYToLatLon(int x, int y, int zoom) {
|
---|
250 | return Main.getProjection().eastNorth2latlon(getTileEastNorth(x, y, zoom)).toCoordinate();
|
---|
251 | }
|
---|
252 |
|
---|
253 | @Override
|
---|
254 | public TileXY latLonToTileXY(double lat, double lon, int zoom) {
|
---|
255 | Projection proj = Main.getProjection();
|
---|
256 | EastNorth enPoint = proj.latlon2eastNorth(new LatLon(lat, lon));
|
---|
257 | return eastNorthToTileXY(enPoint, zoom);
|
---|
258 | }
|
---|
259 |
|
---|
260 | private TileXY eastNorthToTileXY(EastNorth enPoint, int zoom) {
|
---|
261 | double scale = getDegreesPerTile(zoom);
|
---|
262 | return new TileXY(
|
---|
263 | (enPoint.east() - anchorPosition.east()) / scale,
|
---|
264 | (anchorPosition.north() - enPoint.north()) / scale
|
---|
265 | );
|
---|
266 | }
|
---|
267 |
|
---|
268 | @Override
|
---|
269 | public TileXY latLonToTileXY(ICoordinate point, int zoom) {
|
---|
270 | return latLonToTileXY(point.getLat(), point.getLon(), zoom);
|
---|
271 | }
|
---|
272 |
|
---|
273 | @Override
|
---|
274 | public int getTileXMax(int zoom) {
|
---|
275 | return tileXMax[zoom];
|
---|
276 | }
|
---|
277 |
|
---|
278 | @Override
|
---|
279 | public int getTileXMin(int zoom) {
|
---|
280 | return tileXMin[zoom];
|
---|
281 | }
|
---|
282 |
|
---|
283 | @Override
|
---|
284 | public int getTileYMax(int zoom) {
|
---|
285 | return tileYMax[zoom];
|
---|
286 | }
|
---|
287 |
|
---|
288 | @Override
|
---|
289 | public int getTileYMin(int zoom) {
|
---|
290 | return tileYMin[zoom];
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Override
|
---|
294 | public Point latLonToXY(double lat, double lon, int zoom) {
|
---|
295 | double scale = getDegreesPerTile(zoom) / getTileSize();
|
---|
296 | EastNorth point = Main.getProjection().latlon2eastNorth(new LatLon(lat, lon));
|
---|
297 | return new Point(
|
---|
298 | (int) Math.round((point.east() - anchorPosition.east()) / scale),
|
---|
299 | (int) Math.round((anchorPosition.north() - point.north()) / scale)
|
---|
300 | );
|
---|
301 | }
|
---|
302 |
|
---|
303 | @Override
|
---|
304 | public Point latLonToXY(ICoordinate point, int zoom) {
|
---|
305 | return latLonToXY(point.getLat(), point.getLon(), zoom);
|
---|
306 | }
|
---|
307 |
|
---|
308 | @Override
|
---|
309 | public ICoordinate xyToLatLon(Point point, int zoom) {
|
---|
310 | return xyToLatLon(point.x, point.y, zoom);
|
---|
311 | }
|
---|
312 |
|
---|
313 | @Override
|
---|
314 | public ICoordinate xyToLatLon(int x, int y, int zoom) {
|
---|
315 | double scale = getDegreesPerTile(zoom) / getTileSize();
|
---|
316 | Projection proj = Main.getProjection();
|
---|
317 | EastNorth ret = new EastNorth(
|
---|
318 | anchorPosition.east() + x * scale,
|
---|
319 | anchorPosition.north() - y * scale
|
---|
320 | );
|
---|
321 | return proj.eastNorth2latlon(ret).toCoordinate();
|
---|
322 | }
|
---|
323 |
|
---|
324 | @Override
|
---|
325 | public Map<String, String> getHeaders() {
|
---|
326 | return headers;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Checks if url is acceptable by this Tile Source
|
---|
331 | * @param url URL to check
|
---|
332 | */
|
---|
333 | public static void checkUrl(String url) {
|
---|
334 | CheckParameterUtil.ensureParameterNotNull(url, "url");
|
---|
335 | Matcher m = PATTERN_PARAM.matcher(url);
|
---|
336 | while (m.find()) {
|
---|
337 | boolean isSupportedPattern = false;
|
---|
338 | for (Pattern pattern : ALL_PATTERNS) {
|
---|
339 | if (pattern.matcher(m.group()).matches()) {
|
---|
340 | isSupportedPattern = true;
|
---|
341 | break;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | if (!isSupportedPattern) {
|
---|
345 | throw new IllegalArgumentException(
|
---|
346 | tr("{0} is not a valid WMS argument. Please check this server URL:\n{1}", m.group(), url));
|
---|
347 | }
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | private void handleTemplate() {
|
---|
352 | // Capturing group pattern on switch values
|
---|
353 | StringBuffer output = new StringBuffer();
|
---|
354 | Matcher matcher = PATTERN_HEADER.matcher(this.baseUrl);
|
---|
355 | while (matcher.find()) {
|
---|
356 | headers.put(matcher.group(1), matcher.group(2));
|
---|
357 | matcher.appendReplacement(output, "");
|
---|
358 | }
|
---|
359 | matcher.appendTail(output);
|
---|
360 | this.baseUrl = output.toString();
|
---|
361 | }
|
---|
362 |
|
---|
363 | protected EastNorth getTileEastNorth(int x, int y, int z) {
|
---|
364 | double scale = getDegreesPerTile(z);
|
---|
365 | return new EastNorth(
|
---|
366 | anchorPosition.east() + x * scale,
|
---|
367 | anchorPosition.north() - y * scale
|
---|
368 | );
|
---|
369 | }
|
---|
370 |
|
---|
371 | private double getDegreesPerTile(int zoom) {
|
---|
372 | return degreesPerTile[zoom];
|
---|
373 | }
|
---|
374 | }
|
---|