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

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

Rework console output:

  • new log level "error"
  • Replace nearly all calls to system.out and system.err to Main.(error|warn|info|debug)
  • Remove some unnecessary debug output
  • Some messages are modified (removal of "Info", "Warning", "Error" from the message itself -> notable i18n impact but limited to console error messages not seen by the majority of users, so that's ok)
File size: 11.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.awt.geom.Area;
7import java.awt.geom.Rectangle2D;
8import java.util.HashSet;
9import java.util.Set;
10import java.util.concurrent.Future;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.AutoScaleAction;
14import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
15import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
16import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
17import org.openstreetmap.josm.data.Bounds;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.osm.BBox;
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.gui.util.GuiHelper;
27import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
28import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
29import org.openstreetmap.josm.tools.Utils;
30
31/**
32 * Handler for load_and_zoom request.
33 */
34public class LoadAndZoomHandler extends RequestHandler
35{
36 /**
37 * The remote control command name used to load data and zoom.
38 */
39 public static final String command = "load_and_zoom";
40
41 /**
42 * The remote control command name used to zoom.
43 */
44 public static final String command2 = "zoom";
45
46 // Mandatory arguments
47 private double minlat;
48 private double maxlat;
49 private double minlon;
50 private double maxlon;
51
52 // Optional argument 'select'
53 private final Set<Long> ways = new HashSet<Long>();
54 private final Set<Long> nodes = new HashSet<Long>();
55 private final Set<Long> relations = new HashSet<Long>();
56
57 @Override
58 public String getPermissionMessage()
59 {
60 String msg = tr("Remote Control has been asked to load data from the API.") +
61 "<br>" + tr("Bounding box: ") + new BBox(minlon, minlat, maxlon, maxlat).toStringCSV(", ");
62 if (args.containsKey("select") && ways.size()+nodes.size()+relations.size() > 0) {
63 msg += "<br>" + tr("Sel.: Rel.:{0} / Ways:{1} / Nodes:{2}", relations.size(), ways.size(), nodes.size());
64 }
65 return msg;
66 }
67
68 @Override
69 public String[] getMandatoryParams()
70 {
71 return new String[] { "bottom", "top", "left", "right" };
72 }
73
74 @Override
75 public String[] getOptionalParams()
76 {
77 return new String[] {"new_layer", "addtags", "select", "zoom_mode"};
78 }
79
80 @Override
81 public String[] getUsageExamples() {
82 if (command.equals(myCommand)) {
83 return new String[] {
84 "/load_and_zoom?addtags=wikipedia:de=Wei%C3%9Fe_Gasse|maxspeed=5&select=way23071688,way23076176,way23076177,&left=13.740&right=13.741&top=51.05&bottom=51.049",
85 "/load_and_zoom?left=8.19&right=8.20&top=48.605&bottom=48.590&select=node413602999&new_layer=true"};
86 } else {
87 return new String[] {
88 "/zoom?left=8.19&right=8.20&top=48.605&bottom=48.590&select=node413602999"};
89 }
90 }
91
92 @Override
93 protected void handleRequest() throws RequestHandlerErrorException
94 {
95 DownloadTask osmTask = new DownloadOsmTask();
96 try {
97 boolean newLayer = isLoadInNewLayer();
98
99 if (command.equals(myCommand)) {
100 if (!PermissionPrefWithDefault.LOAD_DATA.isAllowed()) {
101 Main.info("RemoteControl: download forbidden by preferences");
102 } else {
103 Area toDownload = null;
104 if (!newLayer) {
105 // find out whether some data has already been downloaded
106 Area present = null;
107 DataSet ds = Main.main.getCurrentDataSet();
108 if (ds != null) {
109 present = ds.getDataSourceArea();
110 }
111 if (present != null && !present.isEmpty()) {
112 toDownload = new Area(new Rectangle2D.Double(minlon,minlat,maxlon-minlon,maxlat-minlat));
113 toDownload.subtract(present);
114 if (!toDownload.isEmpty()) {
115 // the result might not be a rectangle (L shaped etc)
116 Rectangle2D downloadBounds = toDownload.getBounds2D();
117 minlat = downloadBounds.getMinY();
118 minlon = downloadBounds.getMinX();
119 maxlat = downloadBounds.getMaxY();
120 maxlon = downloadBounds.getMaxX();
121 }
122 }
123 }
124 if (toDownload != null && toDownload.isEmpty()) {
125 Main.info("RemoteControl: no download necessary");
126 } else {
127 Future<?> future = osmTask.download(newLayer, new Bounds(minlat,minlon,maxlat,maxlon), null /* let the task manage the progress monitor */);
128 Main.worker.submit(new PostDownloadHandler(osmTask, future));
129 }
130 }
131 }
132 } catch (Exception ex) {
133 Main.warn("RemoteControl: Error parsing load_and_zoom remote control request:");
134 ex.printStackTrace();
135 throw new RequestHandlerErrorException();
136 }
137
138 /**
139 * deselect objects if parameter addtags given
140 */
141 if (args.containsKey("addtags")) {
142 GuiHelper.executeByMainWorkerInEDT(new Runnable() {
143 @Override
144 public void run() {
145 DataSet ds = Main.main.getCurrentDataSet();
146 if(ds == null) // e.g. download failed
147 return;
148 ds.clearSelection();
149 }
150 });
151 }
152
153 final Bounds bbox = new Bounds(minlat, minlon, maxlat, maxlon);
154 if (args.containsKey("select") && PermissionPrefWithDefault.CHANGE_SELECTION.isAllowed()) {
155 // select objects after downloading, zoom to selection.
156 GuiHelper.executeByMainWorkerInEDT(new Runnable() {
157 @Override
158 public void run() {
159 HashSet<OsmPrimitive> newSel = new HashSet<OsmPrimitive>();
160 DataSet ds = Main.main.getCurrentDataSet();
161 if(ds == null) // e.g. download failed
162 return;
163 for (Way w : ds.getWays()) {
164 if (ways.contains(w.getId())) {
165 newSel.add(w);
166 }
167 }
168 ways.clear();
169 for (Node n : ds.getNodes()) {
170 if (nodes.contains(n.getId())) {
171 newSel.add(n);
172 }
173 }
174 nodes.clear();
175 for (Relation r : ds.getRelations()) {
176 if (relations.contains(r.getId())) {
177 newSel.add(r);
178 }
179 }
180 relations.clear();
181 ds.setSelected(newSel);
182 if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
183 // zoom_mode=(download|selection), defaults to selection
184 if (!"download".equals(args.get("zoom_mode")) && !newSel.isEmpty()) {
185 AutoScaleAction.autoScale("selection");
186 } else {
187 zoom(bbox);
188 }
189 }
190 if (Main.isDisplayingMapView() && Main.map.relationListDialog != null) {
191 Main.map.relationListDialog.selectRelations(null); // unselect all relations to fix #7342
192 Main.map.relationListDialog.dataChanged(null);
193 Main.map.relationListDialog.selectRelations(Utils.filteredCollection(newSel, Relation.class));
194 }
195 }
196 });
197 } else if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
198 // after downloading, zoom to downloaded area.
199 zoom(bbox);
200 }
201
202 AddTagsDialog.addTags(args, sender);
203 }
204
205 protected void zoom(final Bounds bounds) {
206 // make sure this isn't called unless there *is* a MapView
207 if (Main.isDisplayingMapView()) {
208 GuiHelper.executeByMainWorkerInEDT(new Runnable() {
209 @Override
210 public void run() {
211 BoundingXYVisitor bbox = new BoundingXYVisitor();
212 bbox.visit(bounds);
213 Main.map.mapView.recalculateCenterScale(bbox);
214 }
215 });
216 }
217 }
218
219 @Override
220 public PermissionPrefWithDefault getPermissionPref() {
221 return null;
222 }
223
224 @Override
225 protected void validateRequest() throws RequestHandlerBadRequestException {
226 // Process mandatory arguments
227 minlat = 0;
228 maxlat = 0;
229 minlon = 0;
230 maxlon = 0;
231 try {
232 minlat = LatLon.roundToOsmPrecision(Double.parseDouble(args.get("bottom")));
233 maxlat = LatLon.roundToOsmPrecision(Double.parseDouble(args.get("top")));
234 minlon = LatLon.roundToOsmPrecision(Double.parseDouble(args.get("left")));
235 maxlon = LatLon.roundToOsmPrecision(Double.parseDouble(args.get("right")));
236 } catch (NumberFormatException e) {
237 throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+")");
238 }
239
240 // Process optional argument 'select'
241 if (args.containsKey("select")) {
242 ways.clear();
243 nodes.clear();
244 relations.clear();
245 for (String item : args.get("select").split(",")) {
246 try {
247 if (item.startsWith("way")) {
248 ways.add(Long.parseLong(item.substring(3)));
249 } else if (item.startsWith("node")) {
250 nodes.add(Long.parseLong(item.substring(4)));
251 } else if (item.startsWith("relation")) {
252 relations.add(Long.parseLong(item.substring(8)));
253 } else if (item.startsWith("rel")) {
254 relations.add(Long.parseLong(item.substring(3)));
255 } else {
256 Main.warn("RemoteControl: invalid selection '"+item+"' ignored");
257 }
258 } catch (NumberFormatException e) {
259 Main.warn("RemoteControl: invalid selection '"+item+"' ignored");
260 }
261 }
262 }
263 }
264}
Note: See TracBrowser for help on using the repository browser.