1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
16 | import org.openstreetmap.josm.command.AddCommand;
|
---|
17 | import org.openstreetmap.josm.command.Command;
|
---|
18 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
24 | import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
|
---|
25 | import 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 | */
|
---|
30 | public 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<LatLon>();
|
---|
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[] getUsageExamples() {
|
---|
56 | return new String[] {
|
---|
57 | "/add_way?way=53.2,13.3;53.3,13.3;53.3,13.2",
|
---|
58 | "/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"
|
---|
59 | };
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException {
|
---|
64 | GuiHelper.runInEDTAndWait(new Runnable() {
|
---|
65 | @Override public void run() {
|
---|
66 | addWay();
|
---|
67 | }
|
---|
68 | });
|
---|
69 | // parse parameter addtags=tag1=value1|tag2=value2
|
---|
70 | AddTagsDialog.addTags(args, sender);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public String getPermissionMessage() {
|
---|
75 | return tr("Remote Control has been asked to create a new way.");
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public PermissionPrefWithDefault getPermissionPref() {
|
---|
80 | return PermissionPrefWithDefault.CREATE_OBJECTS;
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | protected void validateRequest() throws RequestHandlerBadRequestException {
|
---|
85 | allCoordinates.clear();
|
---|
86 | for (String coordinatesString : args.get("way").split(";\\s*")) {
|
---|
87 | String[] coordinates = coordinatesString.split(",\\s*", 2);
|
---|
88 | if (coordinates.length < 2) {
|
---|
89 | throw new RequestHandlerBadRequestException(
|
---|
90 | tr("Invalid coordinates: {0}", Arrays.toString(coordinates)));
|
---|
91 | }
|
---|
92 | try {
|
---|
93 | double lat = Double.parseDouble(coordinates[0]);
|
---|
94 | double lon = Double.parseDouble(coordinates[1]);
|
---|
95 | allCoordinates.add(new LatLon(lat, lon));
|
---|
96 | } catch (NumberFormatException e) {
|
---|
97 | throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+")");
|
---|
98 | }
|
---|
99 | }
|
---|
100 | if (allCoordinates.isEmpty()) {
|
---|
101 | throw new RequestHandlerBadRequestException(tr("Empty ways"));
|
---|
102 | } else if (allCoordinates.size() == 1) {
|
---|
103 | throw new RequestHandlerBadRequestException(tr("One node ways"));
|
---|
104 | }
|
---|
105 | if (!Main.main.hasEditLayer()) {
|
---|
106 | throw new RequestHandlerBadRequestException(tr("There is no layer opened to add way"));
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Find the node with almost the same ccords in dataset or in already added nodes
|
---|
112 | * @since 5845
|
---|
113 | **/
|
---|
114 | Node findOrCreateNode(LatLon ll, List<Command> commands) {
|
---|
115 | Node nd = null;
|
---|
116 |
|
---|
117 | if (Main.isDisplayingMapView()) {
|
---|
118 | Point p = Main.map.mapView.getPoint(ll);
|
---|
119 | nd = Main.map.mapView.getNearestNode(p, OsmPrimitive.isUsablePredicate);
|
---|
120 | if (nd!=null && nd.getCoor().greatCircleDistance(ll) > Main.pref.getDouble("remote.tolerance", 0.1)) {
|
---|
121 | nd = null; // node is too far
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | Node prev = null;
|
---|
126 | for (LatLon lOld: addedNodes.keySet()) {
|
---|
127 | if (lOld.greatCircleDistance(ll) < Main.pref.getDouble("remotecontrol.tolerance", 0.1)) {
|
---|
128 | prev = addedNodes.get(lOld);
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (prev!=null) {
|
---|
134 | nd = prev;
|
---|
135 | } else if (nd==null) {
|
---|
136 | nd = new Node(ll);
|
---|
137 | // Now execute the commands to add this node.
|
---|
138 | commands.add(new AddCommand(nd));
|
---|
139 | addedNodes.put(ll, nd);
|
---|
140 | }
|
---|
141 | return nd;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * This function creates the way with given coordinates of nodes
|
---|
146 | */
|
---|
147 | private void addWay() {
|
---|
148 | addedNodes = new HashMap<LatLon, Node>();
|
---|
149 | Way way = new Way();
|
---|
150 | List<Command> commands = new LinkedList<Command>();
|
---|
151 | for (LatLon ll : allCoordinates) {
|
---|
152 | Node node = findOrCreateNode(ll, commands);
|
---|
153 | way.addNode(node);
|
---|
154 | }
|
---|
155 | allCoordinates.clear();
|
---|
156 | commands.add(new AddCommand(way));
|
---|
157 | Main.main.undoRedo.add(new SequenceCommand(tr("Add way"), commands));
|
---|
158 | Main.main.getCurrentDataSet().setSelected(way);
|
---|
159 | if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
|
---|
160 | AutoScaleAction.autoScale("selection");
|
---|
161 | } else {
|
---|
162 | Main.map.mapView.repaint();
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|