1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Arrays;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.data.imagery.ImageryInfo;
|
---|
10 | import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
|
---|
11 | import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
|
---|
12 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
13 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
14 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
15 | import org.openstreetmap.josm.tools.Utils;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Adds an imagery (WMS/TMS) layer. For instance, {@code /imagery?title=...&type=...&url=...}.
|
---|
19 | * @since 3715
|
---|
20 | */
|
---|
21 | public class ImageryHandler extends RequestHandler.RawURLParseRequestHandler {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The remote control command name used to add an imagery layer.
|
---|
25 | */
|
---|
26 | public static final String command = "imagery";
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public String getPermissionMessage() {
|
---|
30 | return tr("Remote Control has been asked to load an imagery layer from the following URL:")
|
---|
31 | + "<br>" + args.get("url");
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public String[] getMandatoryParams() {
|
---|
36 | return new String[]{"url"};
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public String[] getOptionalParams() {
|
---|
41 | return new String[] {"title", "type", "cookies", "min_zoom", "max_zoom"};
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public PermissionPrefWithDefault getPermissionPref() {
|
---|
46 | return PermissionPrefWithDefault.LOAD_IMAGERY;
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected static ImageryInfo findBingEntry() {
|
---|
50 | for (ImageryInfo i : ImageryLayerInfo.instance.getDefaultLayers()) {
|
---|
51 | if (ImageryType.BING.equals(i.getImageryType())) {
|
---|
52 | return i;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | return null;
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected ImageryInfo buildImageryInfo() {
|
---|
59 | String url = args.get("url");
|
---|
60 | String title = args.get("title");
|
---|
61 | String type = args.get("type");
|
---|
62 | final ImageryInfo bing = ImageryType.BING.getTypeString().equals(type) ? findBingEntry() : null;
|
---|
63 | if ((title == null || title.isEmpty()) && bing != null) {
|
---|
64 | title = bing.getName();
|
---|
65 | }
|
---|
66 | if (title == null || title.isEmpty()) {
|
---|
67 | title = tr("Remote imagery");
|
---|
68 | }
|
---|
69 | String cookies = args.get("cookies");
|
---|
70 | final ImageryInfo imgInfo = new ImageryInfo(title, url, type, null, cookies);
|
---|
71 | if (bing != null) {
|
---|
72 | imgInfo.setIcon(bing.getIcon());
|
---|
73 | }
|
---|
74 | String minZoom = args.get("min_zoom");
|
---|
75 | if (minZoom != null && !minZoom.isEmpty()) {
|
---|
76 | try {
|
---|
77 | imgInfo.setDefaultMinZoom(Integer.parseInt(minZoom));
|
---|
78 | } catch (NumberFormatException e) {
|
---|
79 | Main.error(e);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | String maxZoom = args.get("max_zoom");
|
---|
83 | if (maxZoom != null && !maxZoom.isEmpty()) {
|
---|
84 | try {
|
---|
85 | imgInfo.setDefaultMaxZoom(Integer.parseInt(maxZoom));
|
---|
86 | } catch (NumberFormatException e) {
|
---|
87 | Main.error(e);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return imgInfo;
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | protected void handleRequest() throws RequestHandlerErrorException {
|
---|
95 | final ImageryInfo imgInfo = buildImageryInfo();
|
---|
96 | if (Main.isDisplayingMapView()) {
|
---|
97 | for (ImageryLayer layer : Main.map.mapView.getLayersOfType(ImageryLayer.class)) {
|
---|
98 | if (layer.getInfo().equals(imgInfo)) {
|
---|
99 | Main.info("Imagery layer already exists: "+imgInfo);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | GuiHelper.runInEDT(new Runnable() {
|
---|
105 | @Override
|
---|
106 | public void run() {
|
---|
107 | try {
|
---|
108 | Main.main.addLayer(ImageryLayer.create(imgInfo));
|
---|
109 | } catch (IllegalArgumentException e) {
|
---|
110 | Main.error(e, false);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | });
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
---|
118 | String url = args != null ? args.get("url") : null;
|
---|
119 | String type = args != null ? args.get("type") : null;
|
---|
120 | String cookies = args != null ? args.get("cookies") : null;
|
---|
121 | try {
|
---|
122 | ImageryLayer.create(new ImageryInfo(null, url, type, null, cookies));
|
---|
123 | } catch (IllegalArgumentException e) {
|
---|
124 | throw new RequestHandlerBadRequestException(e.getMessage(), e);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public String getUsage() {
|
---|
130 | return "adds an imagery layer (e.g. WMS, TMS)";
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public String[] getUsageExamples() {
|
---|
135 | final String types = Utils.join("|", Utils.transform(Arrays.asList(ImageryInfo.ImageryType.values()),
|
---|
136 | new Utils.Function<ImageryInfo.ImageryType, String>() {
|
---|
137 | @Override
|
---|
138 | public String apply(ImageryInfo.ImageryType x) {
|
---|
139 | return x.getTypeString();
|
---|
140 | }
|
---|
141 | }));
|
---|
142 | return new String[] {
|
---|
143 | "/imagery?title=osm&type=tms&url=https://a.tile.openstreetmap.org/%7Bzoom%7D/%7Bx%7D/%7By%7D.png",
|
---|
144 | "/imagery?title=landsat&type=wms&url=http://irs.gis-lab.info/?" +
|
---|
145 | "layers=landsat&SRS=%7Bproj%7D&WIDTH=%7Bwidth%7D&HEIGHT=%7Bheight%7D&BBOX=%7Bbbox%7D",
|
---|
146 | "/imagery?title=...&type={"+types+"}&url=....[&cookies=...][&min_zoom=...][&max_zoom=...]"
|
---|
147 | };
|
---|
148 | }
|
---|
149 | }
|
---|