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.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Map.Entry;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
16 | import org.openstreetmap.josm.actions.AutoScaleAction.AutoScaleMode;
|
---|
17 | import org.openstreetmap.josm.command.AddCommand;
|
---|
18 | import org.openstreetmap.josm.command.Command;
|
---|
19 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
20 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
21 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
22 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
23 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmDataManager;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
27 | import org.openstreetmap.josm.data.osm.Way;
|
---|
28 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
29 | import org.openstreetmap.josm.gui.MapView;
|
---|
30 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
31 | import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
|
---|
32 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
33 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Adds a way to the current dataset. For instance, {@code /add_way?way=lat1,lon2;lat2,lon2}.
|
---|
37 | */
|
---|
38 | public class AddWayHandler extends RequestHandler {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * The remote control command name used to add a way.
|
---|
42 | */
|
---|
43 | public static final String command = "add_way";
|
---|
44 |
|
---|
45 | private final List<LatLon> allCoordinates = new ArrayList<>();
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * The place to remember already added nodes (they are reused if needed @since 5845
|
---|
49 | */
|
---|
50 | private Map<LatLon, Node> addedNodes;
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public String[] getMandatoryParams() {
|
---|
54 | return new String[]{"way"};
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public String[] getOptionalParams() {
|
---|
59 | return new String[] {"addtags"};
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public String getUsage() {
|
---|
64 | return "adds a way (given by a semicolon separated sequence of lat,lon pairs) to the current dataset";
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public String[] getUsageExamples() {
|
---|
69 | return new String[] {
|
---|
70 | // CHECKSTYLE.OFF: LineLength
|
---|
71 | "/add_way?way=53.2,13.3;53.3,13.3;53.3,13.2",
|
---|
72 | "/add_way?&addtags=building=yes&way=45.437213,-2.810792;45.437988,-2.455983;45.224080,-2.455036;45.223302,-2.809845;45.437213,-2.810792"
|
---|
73 | // CHECKSTYLE.ON: LineLength
|
---|
74 | };
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException {
|
---|
79 | GuiHelper.runInEDT(this::addWay);
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public String getPermissionMessage() {
|
---|
84 | return tr("Remote Control has been asked to create a new way.");
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public PermissionPrefWithDefault getPermissionPref() {
|
---|
89 | return PermissionPrefWithDefault.CREATE_OBJECTS;
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
---|
94 | allCoordinates.clear();
|
---|
95 | for (String coordinatesString : splitArg("way", SPLITTER_SEMIC)) {
|
---|
96 | String[] coordinates = coordinatesString.split(",\\s*", 2);
|
---|
97 | if (coordinates.length < 2) {
|
---|
98 | throw new RequestHandlerBadRequestException(
|
---|
99 | tr("Invalid coordinates: {0}", Arrays.toString(coordinates)));
|
---|
100 | }
|
---|
101 | try {
|
---|
102 | double lat = Double.parseDouble(coordinates[0]);
|
---|
103 | double lon = Double.parseDouble(coordinates[1]);
|
---|
104 | allCoordinates.add(new LatLon(lat, lon));
|
---|
105 | } catch (NumberFormatException e) {
|
---|
106 | throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+')', e);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | if (allCoordinates.isEmpty()) {
|
---|
110 | throw new RequestHandlerBadRequestException(tr("Empty ways"));
|
---|
111 | } else if (allCoordinates.size() == 1) {
|
---|
112 | throw new RequestHandlerBadRequestException(tr("One node ways"));
|
---|
113 | }
|
---|
114 | if (MainApplication.getLayerManager().getEditLayer() == null) {
|
---|
115 | throw new RequestHandlerBadRequestException(tr("There is no layer opened to add way"));
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Find the node with almost the same coords in dataset or in already added nodes
|
---|
121 | * @param ll coordinates
|
---|
122 | * @param commands list of commands that will be modified if needed
|
---|
123 | * @return node with almost the same coords
|
---|
124 | * @since 5845
|
---|
125 | */
|
---|
126 | Node findOrCreateNode(LatLon ll, List<Command> commands) {
|
---|
127 | Node nd = null;
|
---|
128 |
|
---|
129 | if (MainApplication.isDisplayingMapView()) {
|
---|
130 | MapView mapView = MainApplication.getMap().mapView;
|
---|
131 | nd = mapView.getNearestNode(mapView.getPoint(ll), OsmPrimitive::isUsable);
|
---|
132 | if (nd != null && nd.greatCircleDistance(ll) > Config.getPref().getDouble("remote.tolerance", 0.1)) {
|
---|
133 | nd = null; // node is too far
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | Node prev = null;
|
---|
138 | for (Entry<LatLon, Node> entry : addedNodes.entrySet()) {
|
---|
139 | ILatLon lOld = entry.getKey();
|
---|
140 | if (lOld.greatCircleDistance(ll) < Config.getPref().getDouble("remotecontrol.tolerance", 0.1)) {
|
---|
141 | prev = entry.getValue();
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (prev != null) {
|
---|
147 | nd = prev;
|
---|
148 | } else if (nd == null) {
|
---|
149 | nd = new Node(ll);
|
---|
150 | // Now execute the commands to add this node.
|
---|
151 | commands.add(new AddCommand(OsmDataManager.getInstance().getEditDataSet(), nd));
|
---|
152 | addedNodes.put(ll, nd);
|
---|
153 | }
|
---|
154 | return nd;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * This function creates the way with given coordinates of nodes
|
---|
159 | */
|
---|
160 | private void addWay() {
|
---|
161 | addedNodes = new HashMap<>();
|
---|
162 | Way way = new Way();
|
---|
163 | List<Command> commands = new LinkedList<>();
|
---|
164 | for (LatLon ll : allCoordinates) {
|
---|
165 | Node node = findOrCreateNode(ll, commands);
|
---|
166 | way.addNode(node);
|
---|
167 | }
|
---|
168 | allCoordinates.clear();
|
---|
169 | DataSet ds = MainApplication.getLayerManager().getEditDataSet();
|
---|
170 | commands.add(new AddCommand(ds, way));
|
---|
171 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Add way"), commands));
|
---|
172 | ds.setSelected(way);
|
---|
173 | if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
|
---|
174 | AutoScaleAction.autoScale(AutoScaleMode.SELECTION);
|
---|
175 | } else {
|
---|
176 | MainApplication.getMap().mapView.repaint();
|
---|
177 | }
|
---|
178 | // parse parameter addtags=tag1=value1|tag2=value2
|
---|
179 | AddTagsDialog.addTags(args, sender, Collections.singleton(way));
|
---|
180 | }
|
---|
181 | }
|
---|