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

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

see #15229 - deprecate Main.main - new class OsmDataManager

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