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

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

see #8465 - use diamond operator where applicable

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