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

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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