source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java@ 12472

Last change on this file since 12472 was 10716, checked in by simon04, 8 years ago

see #11390, see #12890 - Deprecate predicates in OsmPrimitive class

  • Property svn:eol-style set to native
File size: 3.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.Point;
7import java.util.Collections;
8import java.util.Map;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.actions.AutoScaleAction;
12import org.openstreetmap.josm.command.AddCommand;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
16import org.openstreetmap.josm.gui.util.GuiHelper;
17import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
18import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
19
20/**
21 * Handler for add_node request.
22 */
23public class AddNodeHandler extends RequestHandler {
24
25 /**
26 * The remote control command name used to add a node.
27 */
28 public static final String command = "add_node";
29
30 private double lat;
31 private double lon;
32
33 @Override
34 protected void handleRequest() {
35 GuiHelper.runInEDTAndWait(() -> addNode(args));
36 }
37
38 @Override
39 public String[] getMandatoryParams() {
40 return new String[] {"lat", "lon"};
41 }
42
43 @Override
44 public String[] getOptionalParams() {
45 return new String[] {"addtags"};
46 }
47
48 @Override
49 public String getUsage() {
50 return "adds a node (given by its latitude and longitude) to the current dataset";
51 }
52
53 @Override
54 public String[] getUsageExamples() {
55 return new String[] {
56 "/add_node?lat=11&lon=22",
57 "/add_node?lon=13.3&lat=53.2&addtags=natural=tree|name=%20%20%20==Great%20Oak=="
58 };
59 }
60
61 @Override
62 public String getPermissionMessage() {
63 return tr("Remote Control has been asked to create a new node.") +
64 "<br>" + tr("Coordinates: ") + args.get("lat") + ", " + args.get("lon");
65 }
66
67 @Override
68 public PermissionPrefWithDefault getPermissionPref() {
69 return PermissionPrefWithDefault.CREATE_OBJECTS;
70 }
71
72 /**
73 * Adds a node, implements the GET /add_node?lon=...&amp;lat=... request.
74 * @param args request arguments
75 */
76 private void addNode(Map<String, String> args) {
77
78 // Parse the arguments
79 Main.info("Adding node at (" + lat + ", " + lon + ')');
80
81 // Create a new node
82 LatLon ll = new LatLon(lat, lon);
83
84 Node node = null;
85
86 if (Main.isDisplayingMapView()) {
87 Point p = Main.map.mapView.getPoint(ll);
88 node = Main.map.mapView.getNearestNode(p, OsmPrimitive::isUsable);
89 if (node != null && node.getCoor().greatCircleDistance(ll) > Main.pref.getDouble("remotecontrol.tolerance", 0.1)) {
90 node = null; // node is too far
91 }
92 }
93
94 if (node == null) {
95 node = new Node(ll);
96 // Now execute the commands to add this node.
97 Main.main.undoRedo.add(new AddCommand(node));
98 }
99
100 Main.getLayerManager().getEditDataSet().setSelected(node);
101 if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
102 AutoScaleAction.autoScale("selection");
103 } else {
104 Main.map.mapView.repaint();
105 }
106 // parse parameter addtags=tag1=value1|tag2=vlaue2
107 AddTagsDialog.addTags(args, sender, Collections.singleton(node));
108 }
109
110 @Override
111 protected void validateRequest() throws RequestHandlerBadRequestException {
112 try {
113 lat = Double.parseDouble(args != null ? args.get("lat") : "");
114 lon = Double.parseDouble(args != null ? args.get("lon") : "");
115 } catch (NumberFormatException e) {
116 throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+')', e);
117 }
118 if (Main.getLayerManager().getEditLayer() == null) {
119 throw new RequestHandlerBadRequestException(tr("There is no layer opened to add node"));
120 }
121 }
122}
Note: See TracBrowser for help on using the repository browser.