source: josm/trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java@ 14221

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

see #16706 - use documented enum for AutoScaleMode instead of undocumented string constants

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