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

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

fix remaining checkstyle issues

  • Property svn:eol-style set to native
File size: 6.1 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.awt.Point;
7import java.util.ArrayList;
8import java.util.Arrays;
9import java.util.Collections;
10import java.util.HashMap;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Map;
14import java.util.Map.Entry;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.actions.AutoScaleAction;
18import org.openstreetmap.josm.command.AddCommand;
19import org.openstreetmap.josm.command.Command;
20import org.openstreetmap.josm.command.SequenceCommand;
21import org.openstreetmap.josm.data.coor.LatLon;
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.util.GuiHelper;
26import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
27import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
28
29/**
30 * Adds a way to the current dataset. For instance, {@code /add_way?way=lat1,lon2;lat2,lon2}.
31 */
32public class AddWayHandler extends RequestHandler {
33
34 /**
35 * The remote control command name used to add a way.
36 */
37 public static final String command = "add_way";
38
39 private final List<LatLon> allCoordinates = new ArrayList<>();
40
41 private Way way;
42
43 /**
44 * The place to remeber already added nodes (they are reused if needed @since 5845
45 */
46 private Map<LatLon, Node> addedNodes;
47
48 @Override
49 public String[] getMandatoryParams() {
50 return new String[]{"way"};
51 }
52
53 @Override
54 public String[] getOptionalParams() {
55 return new String[] {"addtags"};
56 }
57
58 @Override
59 public String getUsage() {
60 return "adds a way (given by a semicolon separated sequence of lat,lon pairs) to the current dataset";
61 }
62
63 @Override
64 public String[] getUsageExamples() {
65 return new String[] {
66 // CHECKSTYLE.OFF: LineLength
67 "/add_way?way=53.2,13.3;53.3,13.3;53.3,13.2",
68 "/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"
69 // CHECKSTYLE.ON: LineLength
70 };
71 }
72
73 @Override
74 protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException {
75 GuiHelper.runInEDTAndWait(new Runnable() {
76 @Override public void run() {
77 way = addWay();
78 }
79 });
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 : args.get("way").split(";\\s*")) {
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 (!Main.main.hasEditLayer()) {
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 ccords in dataset or in already added nodes
123 * @since 5845
124 */
125 Node findOrCreateNode(LatLon ll, List<Command> commands) {
126 Node nd = null;
127
128 if (Main.isDisplayingMapView()) {
129 Point p = Main.map.mapView.getPoint(ll);
130 nd = Main.map.mapView.getNearestNode(p, OsmPrimitive.isUsablePredicate);
131 if (nd != null && nd.getCoor().greatCircleDistance(ll) > Main.pref.getDouble("remote.tolerance", 0.1)) {
132 nd = null; // node is too far
133 }
134 }
135
136 Node prev = null;
137 for (Entry<LatLon, Node> entry : addedNodes.entrySet()) {
138 LatLon lOld = entry.getKey();
139 if (lOld.greatCircleDistance(ll) < Main.pref.getDouble("remotecontrol.tolerance", 0.1)) {
140 prev = entry.getValue();
141 break;
142 }
143 }
144
145 if (prev != null) {
146 nd = prev;
147 } else if (nd == null) {
148 nd = new Node(ll);
149 // Now execute the commands to add this node.
150 commands.add(new AddCommand(nd));
151 addedNodes.put(ll, nd);
152 }
153 return nd;
154 }
155
156 /*
157 * This function creates the way with given coordinates of nodes
158 */
159 private Way addWay() {
160 addedNodes = new HashMap<>();
161 Way way = new Way();
162 List<Command> commands = new LinkedList<>();
163 for (LatLon ll : allCoordinates) {
164 Node node = findOrCreateNode(ll, commands);
165 way.addNode(node);
166 }
167 allCoordinates.clear();
168 commands.add(new AddCommand(way));
169 Main.main.undoRedo.add(new SequenceCommand(tr("Add way"), commands));
170 Main.main.getCurrentDataSet().setSelected(way);
171 if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
172 AutoScaleAction.autoScale("selection");
173 } else {
174 Main.map.mapView.repaint();
175 }
176 return way;
177 }
178}
Note: See TracBrowser for help on using the repository browser.