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

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

see #11390 - fix checkstyle violations

  • Property svn:eol-style set to native
File size: 5.0 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.Utils;
16
17/**
18 * Adds an imagery (WMS/TMS) layer. For instance, {@code /imagery?title=...&type=...&url=...}.
19 * @since 3715
20 */
21public 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.getLayerManager().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(() -> {
105 try {
106 Main.getLayerManager().addLayer(ImageryLayer.create(imgInfo));
107 } catch (IllegalArgumentException e) {
108 Main.error(e, false);
109 }
110 });
111 }
112
113 @Override
114 protected void validateRequest() throws RequestHandlerBadRequestException {
115 String url = args != null ? args.get("url") : null;
116 String type = args != null ? args.get("type") : null;
117 String cookies = args != null ? args.get("cookies") : null;
118 try {
119 ImageryLayer.create(new ImageryInfo(null, url, type, null, cookies));
120 } catch (IllegalArgumentException e) {
121 throw new RequestHandlerBadRequestException(e.getMessage(), e);
122 }
123 }
124
125 @Override
126 public String getUsage() {
127 return "adds an imagery layer (e.g. WMS, TMS)";
128 }
129
130 @Override
131 public String[] getUsageExamples() {
132 final String types = Utils.join("|", Utils.transform(Arrays.asList(ImageryInfo.ImageryType.values()),
133 ImageryType::getTypeString));
134 return new String[] {
135 "/imagery?title=osm&type=tms&url=https://a.tile.openstreetmap.org/%7Bzoom%7D/%7Bx%7D/%7By%7D.png",
136 "/imagery?title=landsat&type=wms&url=http://irs.gis-lab.info/?" +
137 "layers=landsat&SRS=%7Bproj%7D&WIDTH=%7Bwidth%7D&HEIGHT=%7Bheight%7D&BBOX=%7Bbbox%7D",
138 "/imagery?title=...&type={"+types+"}&url=....[&cookies=...][&min_zoom=...][&max_zoom=...]"
139 };
140 }
141}
Note: See TracBrowser for help on using the repository browser.