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

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

SonarQube - fix minor issues

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