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