001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io.remotecontrol.handler;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Point;
007import java.util.Collections;
008import java.util.Map;
009
010import org.openstreetmap.josm.actions.AutoScaleAction;
011import org.openstreetmap.josm.command.AddCommand;
012import org.openstreetmap.josm.data.coor.LatLon;
013import org.openstreetmap.josm.data.osm.DataSet;
014import org.openstreetmap.josm.data.osm.Node;
015import org.openstreetmap.josm.data.osm.OsmPrimitive;
016import org.openstreetmap.josm.gui.MainApplication;
017import org.openstreetmap.josm.gui.MapView;
018import org.openstreetmap.josm.gui.util.GuiHelper;
019import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
020import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
021import org.openstreetmap.josm.spi.preferences.Config;
022import org.openstreetmap.josm.tools.Logging;
023
024/**
025 * Handler for add_node request.
026 */
027public class AddNodeHandler extends RequestHandler {
028
029    /**
030     * The remote control command name used to add a node.
031     */
032    public static final String command = "add_node";
033
034    private double lat;
035    private double lon;
036
037    @Override
038    protected void handleRequest() {
039        GuiHelper.runInEDTAndWait(() -> addNode(args));
040    }
041
042    @Override
043    public String[] getMandatoryParams() {
044        return new String[] {"lat", "lon"};
045    }
046
047    @Override
048    public String[] getOptionalParams() {
049        return new String[] {"addtags"};
050    }
051
052    @Override
053    public String getUsage() {
054        return "adds a node (given by its latitude and longitude) to the current dataset";
055    }
056
057    @Override
058    public String[] getUsageExamples() {
059        return new String[] {
060            "/add_node?lat=11&lon=22",
061            "/add_node?lon=13.3&lat=53.2&addtags=natural=tree|name=%20%20%20==Great%20Oak=="
062        };
063    }
064
065    @Override
066    public String getPermissionMessage() {
067        return tr("Remote Control has been asked to create a new node.") +
068                "<br>" + tr("Coordinates: ") + args.get("lat") + ", " + args.get("lon");
069    }
070
071    @Override
072    public PermissionPrefWithDefault getPermissionPref() {
073        return PermissionPrefWithDefault.CREATE_OBJECTS;
074    }
075
076    /**
077     * Adds a node, implements the GET /add_node?lon=...&amp;lat=... request.
078     * @param args request arguments
079     */
080    private void addNode(Map<String, String> args) {
081
082        // Parse the arguments
083        Logging.info("Adding node at (" + lat + ", " + lon + ')');
084
085        // Create a new node
086        LatLon ll = new LatLon(lat, lon);
087
088        Node node = null;
089
090        if (MainApplication.isDisplayingMapView()) {
091            MapView mapView = MainApplication.getMap().mapView;
092            Point p = mapView.getPoint(ll);
093            node = mapView.getNearestNode(p, OsmPrimitive::isUsable);
094            if (node != null && node.getCoor().greatCircleDistance(ll) > Config.getPref().getDouble("remotecontrol.tolerance", 0.1)) {
095                node = null; // node is too far
096            }
097        }
098
099        DataSet ds = MainApplication.getLayerManager().getEditDataSet();
100        if (node == null) {
101            node = new Node(ll);
102            // Now execute the commands to add this node.
103            MainApplication.undoRedo.add(new AddCommand(ds, node));
104        }
105
106        ds.setSelected(node);
107        if (PermissionPrefWithDefault.CHANGE_VIEWPORT.isAllowed()) {
108            AutoScaleAction.autoScale("selection");
109        } else {
110            MainApplication.getMap().mapView.repaint();
111        }
112        // parse parameter addtags=tag1=value1|tag2=vlaue2
113        AddTagsDialog.addTags(args, sender, Collections.singleton(node));
114    }
115
116    @Override
117    protected void validateRequest() throws RequestHandlerBadRequestException {
118        try {
119            lat = Double.parseDouble(args != null ? args.get("lat") : "");
120            lon = Double.parseDouble(args != null ? args.get("lon") : "");
121        } catch (NumberFormatException e) {
122            throw new RequestHandlerBadRequestException("NumberFormatException ("+e.getMessage()+')', e);
123        }
124        if (MainApplication.getLayerManager().getEditLayer() == null) {
125             throw new RequestHandlerBadRequestException(tr("There is no layer opened to add node"));
126        }
127    }
128}