1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Map;
|
---|
7 | import java.util.concurrent.ConcurrentHashMap;
|
---|
8 | import java.util.regex.Matcher;
|
---|
9 | import java.util.regex.Pattern;
|
---|
10 | import java.util.stream.Collectors;
|
---|
11 |
|
---|
12 | import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource;
|
---|
13 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
14 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
15 | import org.openstreetmap.josm.gui.layer.WMSLayer;
|
---|
16 | import org.openstreetmap.josm.io.imagery.WMSImagery;
|
---|
17 | import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException;
|
---|
18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Class representing ImageryType.WMS_ENDPOINT tile source.
|
---|
22 | * It differs from standard WMS tile source that this tile source fetches GetCapabilities from server and
|
---|
23 | * uses most of the parameters from there
|
---|
24 | *
|
---|
25 | * @author Wiktor Niesiobedzki
|
---|
26 | * @since 13733
|
---|
27 | */
|
---|
28 | public class WMSEndpointTileSource extends AbstractWMSTileSource implements TemplatedTileSource {
|
---|
29 |
|
---|
30 | private final WMSImagery wmsi;
|
---|
31 | private final List<DefaultLayer> layers;
|
---|
32 | private final String urlPattern;
|
---|
33 | private static final Pattern PATTERN_PARAM = Pattern.compile("\\{([^}]+)\\}");
|
---|
34 | private final Map<String, String> headers = new ConcurrentHashMap<>();
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Create WMSEndpointTileSource tile source
|
---|
38 | * @param info WMS_ENDPOINT ImageryInfo
|
---|
39 | * @param tileProjection server projection that should be used by this tile source
|
---|
40 | */
|
---|
41 | public WMSEndpointTileSource(ImageryInfo info, Projection tileProjection) {
|
---|
42 | super(info, tileProjection);
|
---|
43 | CheckParameterUtil.ensureThat(info.getImageryType() == ImageryType.WMS_ENDPOINT, "imageryType");
|
---|
44 | try {
|
---|
45 | wmsi = new WMSImagery(info.getUrl(), info.getCustomHttpHeaders());
|
---|
46 | } catch (IOException | WMSGetCapabilitiesException e) {
|
---|
47 | throw new IllegalArgumentException(e);
|
---|
48 | }
|
---|
49 | layers = info.getDefaultLayers();
|
---|
50 | initProjection();
|
---|
51 | urlPattern = wmsi.buildGetMapUrl(layers, info.isTransparent());
|
---|
52 | this.headers.putAll(info.getCustomHttpHeaders());
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public int getDefaultTileSize() {
|
---|
57 | return WMSLayer.PROP_IMAGE_SIZE.get();
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public String getTileUrl(int zoom, int tilex, int tiley) {
|
---|
62 | // Using StringBuffer and generic PATTERN_PARAM matcher gives 2x performance improvement over replaceAll
|
---|
63 | StringBuffer url = new StringBuffer(urlPattern.length());
|
---|
64 | Matcher matcher = PATTERN_PARAM.matcher(urlPattern);
|
---|
65 | while (matcher.find()) {
|
---|
66 | String replacement;
|
---|
67 | switch (matcher.group(1)) {
|
---|
68 | case "proj":
|
---|
69 | replacement = getServerCRS();
|
---|
70 | break;
|
---|
71 | case "bbox":
|
---|
72 | replacement = getBbox(zoom, tilex, tiley, !wmsi.belowWMS130() && getTileProjection().switchXY());
|
---|
73 | break;
|
---|
74 | case "width":
|
---|
75 | case "height":
|
---|
76 | replacement = String.valueOf(getTileSize());
|
---|
77 | break;
|
---|
78 | default:
|
---|
79 | replacement = '{' + matcher.group(1) + '}';
|
---|
80 | }
|
---|
81 | matcher.appendReplacement(url, replacement);
|
---|
82 | }
|
---|
83 | matcher.appendTail(url);
|
---|
84 | return url.toString();
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Returns list of EPSG codes that current layer selection supports.
|
---|
89 | * @return list of EPSG codes that current layer selection supports (this may differ from layer to layer)
|
---|
90 | */
|
---|
91 | public List<String> getServerProjections() {
|
---|
92 | return wmsi.getLayers(layers).stream().flatMap(x -> x.getCrs().stream()).distinct().collect(Collectors.toList());
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public Map<String, String> getHeaders() {
|
---|
97 | return headers;
|
---|
98 | }
|
---|
99 | }
|
---|