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

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

fix many checkstyle violations

  • 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.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.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.get("url");
119 String type = args.get("type");
120 String cookies = args.get("cookies");
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[] {"/imagery?title=osm&type=tms&url=https://a.tile.openstreetmap.org/%7Bzoom%7D/%7Bx%7D/%7By%7D.png",
143 "/imagery?title=landsat&type=wms&url=http://irs.gis-lab.info/?layers=landsat&SRS=%7Bproj%7D&WIDTH=%7Bwidth%7D&HEIGHT=%7Bheight%7D&BBOX=%7Bbbox%7D",
144 "/imagery?title=...&type={"+types+"}&url=....[&cookies=...][&min_zoom=...][&max_zoom=...]"};
145 }
146}
Note: See TracBrowser for help on using the repository browser.