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