| 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.util.ArrayList;
|
|---|
| 7 | import java.util.Arrays;
|
|---|
| 8 | import java.util.Collections;
|
|---|
| 9 | import java.util.HashMap;
|
|---|
| 10 | import java.util.LinkedList;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 | import java.util.Map;
|
|---|
| 13 | import java.util.Map.Entry;
|
|---|
| 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.MainApplication;
|
|---|
| 25 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 26 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 27 | import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
|
|---|
| 28 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Adds a way to the current dataset. For instance, {@code /add_way?way=lat1,lon2;lat2,lon2}.
|
|---|
| 32 | */
|
|---|
| 33 | public class AddWayHandler extends RequestHandler {
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * The remote control command name used to add a way.
|
|---|
| 37 | */
|
|---|
| 38 | public static final String command = "add_way";
|
|---|
| 39 |
|
|---|
| 40 | private final List<LatLon> allCoordinates = new ArrayList<>();
|
|---|
| 41 |
|
|---|
| 42 | private Way way;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * The place to remeber already added nodes (they are reused if needed @since 5845
|
|---|
| 46 | */
|
|---|
| 47 | private Map<LatLon, Node> addedNodes;
|
|---|
| 48 |
|
|---|
| 49 | @Override
|
|---|
| 50 | public String[] getMandatoryParams() {
|
|---|
| 51 | return new String[]{"way"};
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | @Override
|
|---|
| 55 | public String[] getOptionalParams() {
|
|---|
| 56 | return new String[] {"addtags"};
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | @Override
|
|---|
| 60 | public String getUsage() {
|
|---|
| 61 | return "adds a way (given by a semicolon separated sequence of lat,lon pairs) to the current dataset";
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | @Override
|
|---|
| 65 | public String[] getUsageExamples() {
|
|---|
| 66 | return new String[] {
|
|---|
| 67 | // CHECKSTYLE.OFF: LineLength
|
|---|
| 68 | "/add_way?way=53.2,13.3;53.3,13.3;53.3,13.2",
|
|---|
| 69 | "/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"
|
|---|
| 70 | // CHECKSTYLE.ON: LineLength
|
|---|
| 71 | };
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | @Override
|
|---|
| 75 | protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException {
|
|---|
| 76 | GuiHelper.runInEDTAndWait(() -> way = addWay());
|
|---|
| 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 != null ? 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()+')', e);
|
|---|
| 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 (MainApplication.getLayerManager().getEditLayer() == null) {
|
|---|
| 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 coords in dataset or in already added nodes
|
|---|
| 120 | * @param ll coordinates
|
|---|
| 121 | * @param commands list of commands that will be modified if needed
|
|---|
| 122 | * @return node with almost the same coords
|
|---|
| 123 | * @since 5845
|
|---|
| 124 | */
|
|---|
| 125 | Node findOrCreateNode(LatLon ll, List<Command> commands) {
|
|---|
| 126 | Node nd = null;
|
|---|
| 127 |
|
|---|
| 128 | if (MainApplication.isDisplayingMapView()) {
|
|---|
| 129 | MapView mapView = MainApplication.getMap().mapView;
|
|---|
| 130 | nd = mapView.getNearestNode(mapView.getPoint(ll), OsmPrimitive::isUsable);
|
|---|
| 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 | MainApplication.getLayerManager().getEditDataSet().setSelected(way);
|
|---|
| 171 | if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
|
|---|
| 172 | AutoScaleAction.autoScale("selection");
|
|---|
| 173 | } else {
|
|---|
| 174 | MainApplication.getMap().mapView.repaint();
|
|---|
| 175 | }
|
|---|
| 176 | return way;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|