source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/ImageryHandler.java@ 12630

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

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

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