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