source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java@ 4795

Last change on this file since 4795 was 4795, checked in by simon04, 12 years ago

fix #5796 - remotecontrol: load_and_zoom/zoom: select relations in the relation toggle dialog

  • Property svn:eol-style set to native
File size: 9.9 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.awt.geom.Area;
7import java.awt.geom.Rectangle2D;
8import java.io.UnsupportedEncodingException;
9import java.net.URLDecoder;
10import java.util.HashSet;
11import java.util.concurrent.Future;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.AutoScaleAction;
15import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
16import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
17import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Relation;
24import org.openstreetmap.josm.data.osm.Way;
25import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
26import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
27import org.openstreetmap.josm.tools.Utils;
28
29/**
30 * Handler for load_and_zoom request.
31 */
32public class LoadAndZoomHandler extends RequestHandler
33{
34 public static final String command = "load_and_zoom";
35 public static final String command2 = "zoom";
36
37 public static final String loadDataPermissionKey = "remotecontrol.permission.load-data";
38 public static final boolean loadDataPermissionDefault = true;
39 public static final String changeSelectionPermissionKey = "remotecontrol.permission.change-selection";
40 public static final boolean changeSelectionPermissionDefault = true;
41 public static final String changeViewportPermissionKey = "remotecontrol.permission.change-viewport";
42 public static final boolean changeViewportPermissionDefault = true;
43
44 @Override
45 public String getPermissionMessage()
46 {
47 return tr("Remote Control has been asked to load data from the API.") +
48 "<br>" + tr("Request details: {0}", request);
49 }
50
51 @Override
52 protected String[] getMandatoryParams()
53 {
54 return new String[] { "bottom", "top", "left", "right" };
55 }
56
57 @Override
58 protected void handleRequest() throws RequestHandlerErrorException
59 {
60 DownloadTask osmTask = new DownloadOsmTask();
61 double minlat = 0;
62 double maxlat = 0;
63 double minlon = 0;
64 double maxlon = 0;
65 try {
66 minlat = Double.parseDouble(args.get("bottom"));
67 maxlat = Double.parseDouble(args.get("top"));
68 minlon = Double.parseDouble(args.get("left"));
69 maxlon = Double.parseDouble(args.get("right"));
70 boolean newLayer = Boolean.parseBoolean(args.get("new_layer"));
71
72 if(command.equals(myCommand))
73 {
74 if (!Main.pref.getBoolean(loadDataPermissionKey, loadDataPermissionDefault))
75 {
76 System.out.println("RemoteControl: download forbidden by preferences");
77 }
78 else
79 {
80
81 // find out whether some data has already been downloaded
82 Area present = null;
83 Area toDownload = null;
84 DataSet ds = Main.main.getCurrentDataSet();
85 if (ds != null) {
86 present = ds.getDataSourceArea();
87 }
88 if (present != null && !present.isEmpty()) {
89 toDownload = new Area(new Rectangle2D.Double(minlon,minlat,maxlon-minlon,maxlat-minlat));
90 toDownload.subtract(present);
91 if (!toDownload.isEmpty())
92 {
93 // the result might not be a rectangle (L shaped etc)
94 Rectangle2D downloadBounds = toDownload.getBounds2D();
95 minlat = downloadBounds.getMinY();
96 minlon = downloadBounds.getMinX();
97 maxlat = downloadBounds.getMaxY();
98 maxlon = downloadBounds.getMaxX();
99 }
100 }
101 if((toDownload != null) && toDownload.isEmpty())
102 {
103 System.out.println("RemoteControl: no download necessary");
104 }
105 else
106 {
107 Future<?> future = osmTask.download(newLayer, new Bounds(minlat,minlon,maxlat,maxlon), null /* let the task manage the progress monitor */);
108 Main.worker.submit(new PostDownloadHandler(osmTask, future));
109 }
110 }
111 }
112 } catch (Exception ex) {
113 System.out.println("RemoteControl: Error parsing load_and_zoom remote control request:");
114 ex.printStackTrace();
115 throw new RequestHandlerErrorException();
116 }
117
118 /**
119 * deselect objects if parameter addtags given
120 */
121 if (args.containsKey("addtags")) {
122 Main.worker.execute(new Runnable() {
123 public void run() {
124 DataSet ds = Main.main.getCurrentDataSet();
125 if(ds == null) // e.g. download failed
126 return;
127 ds.clearSelection();
128 }
129 });
130 }
131
132 if (args.containsKey("select") && Main.pref.getBoolean(changeSelectionPermissionKey, changeSelectionPermissionDefault)) {
133 // select objects after downloading, zoom to selection.
134 final String selection = args.get("select");
135 Main.worker.execute(new Runnable() {
136 public void run() {
137 HashSet<Long> ways = new HashSet<Long>();
138 HashSet<Long> nodes = new HashSet<Long>();
139 HashSet<Long> relations = new HashSet<Long>();
140 HashSet<OsmPrimitive> newSel = new HashSet<OsmPrimitive>();
141 for (String item : selection.split(",")) {
142 if (item.startsWith("way")) {
143 ways.add(Long.parseLong(item.substring(3)));
144 } else if (item.startsWith("node")) {
145 nodes.add(Long.parseLong(item.substring(4)));
146 } else if (item.startsWith("relation")) {
147 relations.add(Long.parseLong(item.substring(8)));
148 } else if (item.startsWith("rel")) {
149 relations.add(Long.parseLong(item.substring(3)));
150 } else {
151 System.out.println("RemoteControl: invalid selection '"+item+"' ignored");
152 }
153 }
154 DataSet ds = Main.main.getCurrentDataSet();
155 if(ds == null) // e.g. download failed
156 return;
157 for (Way w : ds.getWays()) {
158 if (ways.contains(w.getId())) {
159 newSel.add(w);
160 }
161 }
162 for (Node n : ds.getNodes()) {
163 if (nodes.contains(n.getId())) {
164 newSel.add(n);
165 }
166 }
167 for (Relation r : ds.getRelations()) {
168 if (relations.contains(r.getId())) {
169 newSel.add(r);
170 }
171 }
172 ds.setSelected(newSel);
173 if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) {
174 new AutoScaleAction("selection").actionPerformed(null);
175 }
176 if (Main.map != null && Main.map.relationListDialog != null) {
177 Main.map.relationListDialog.dataChanged(null);
178 Main.map.relationListDialog.selectRelations(Utils.filteredCollection(newSel, Relation.class));
179 }
180 }
181 });
182 } else if (Main.pref.getBoolean(changeViewportPermissionKey, changeViewportPermissionDefault)) {
183 // after downloading, zoom to downloaded area.
184 zoom(minlat, maxlat, minlon, maxlon);
185 }
186
187 /*
188 * parse addtags parameters
189 * Example URL (part):
190 * addtags=wikipedia:de%3DResidenzschloss Dresden|name:en%3DDresden Castle
191 */
192 if (args.containsKey("addtags")) {
193 Main.worker.execute(new Runnable() {
194 public void run() {
195 String[] tags = null;
196 try {
197 tags = URLDecoder.decode(args.get("addtags"), "UTF-8").split("\\|");
198 } catch (UnsupportedEncodingException e) {
199 throw new RuntimeException();
200 }
201 String[][] keyValue = new String[tags.length][2];
202 for (int i = 0; i<tags.length; i++) {
203 keyValue[i] = tags[i].split("=");
204
205 keyValue[i][0] = keyValue[i][0];
206 keyValue[i][1] = keyValue[i][1];
207 }
208
209 new AddTagsDialog(keyValue);
210 }
211 });
212 }
213
214 }
215
216 protected void zoom(double minlat, double maxlat, double minlon, double maxlon) {
217 final Bounds bounds = new Bounds(new LatLon(minlat, minlon),
218 new LatLon(maxlat, maxlon));
219
220 // make sure this isn't called unless there *is* a MapView
221 //
222 if (Main.map != null && Main.map.mapView != null) {
223 Main.worker.execute(new Runnable() {
224 public void run() {
225 BoundingXYVisitor bbox = new BoundingXYVisitor();
226 bbox.visit(bounds);
227 Main.map.mapView.recalculateCenterScale(bbox);
228 }
229 });
230 }
231 }
232}
Note: See TracBrowser for help on using the repository browser.