Changeset 35617 in osm


Ignore:
Timestamp:
2020-10-25T12:22:46+01:00 (4 years ago)
Author:
GerdP
Message:

fix #8396: Replace geometry: save node ids

  • "Upgrade a node to a way or multipolygon": try first to find closest new node
  • fix some sonarlint and javadoc issues
File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/replacegeometry/ReplaceGeometryUtils.java

    r35597 r35617  
    1616import java.util.Map;
    1717import java.util.Set;
     18import java.util.stream.Collectors;
    1819
    1920import javax.swing.JOptionPane;
     
    155156            } else if (referenceObject instanceof Relation) {
    156157                for (RelationMember member : ((Relation) referenceObject).getMembers()) {
    157                     if ((member.getRole().equals("outer") || member.getRole().equals("inner"))
     158                    if (("outer".equals(member.getRole()) || "inner".equals(member.getRole()))
    158159                            && member.isWay()) {
    159160                        // TODO: could consider more nodes, such as nodes that are members of other ways,
     
    165166                assert false;
    166167            }
    167             nodeToReplace = findNearestNode(subjectNode, nodePool);
     168            // see #8396: first try to find nearest new node
     169            nodeToReplace = findNearestNode(subjectNode, nodePool.stream().filter(Node::isNew).collect(Collectors.toList()));
     170            if (nodeToReplace == null) {
     171                nodeToReplace = findNearestNode(subjectNode, nodePool);
     172            }
    168173        }
    169174
     
    251256     * @param subjectWay way to modify
    252257     * @param referenceWay way to remove
    253      * @return
     258     * @return Command to replace geometry or null if user cancelled
    254259     */
    255260    public static ReplaceGeometryCommand buildReplaceWayCommand(Way subjectWay, Way referenceWay) {
     
    291296        int gLen = geometryPool.size();
    292297        int nLen = nodePool.size();
    293         int N = Math.max(gLen, nLen);
     298        int maxDim = Math.max(gLen, nLen);
    294299        boolean useRobust = Config.getPref().getBoolean("utilsplugin2.replace-geometry.robustAssignment", true)
    295                 && N <= Config.getPref().getInt("utilsplugin2.replace-geometry.robustAssignment.max-size", 300);
     300                && maxDim <= Config.getPref().getInt("utilsplugin2.replace-geometry.robustAssignment.max-size", 300);
    296301
    297302        // Find new nodes that are closest to the old ones, remove matching old ones from the pool
     
    300305        if (gLen > 0 && nLen > 0) {
    301306            if (useRobust) {  // use robust, but slower assignment
    302                 double[][] cost = new double[N][N];
    303                 for (int i = 0; i < N; i++) {
    304                     for (int j = 0; j < N; j++) {
     307                double[][] cost = new double[maxDim][maxDim];
     308                for (int i = 0; i < maxDim; i++) {
     309                    for (int j = 0; j < maxDim; j++) {
    305310                        cost[i][j] = Double.MAX_VALUE;
    306311                    }
     
    321326                try {
    322327                    assignment = new AssignmentProblem(cost);
    323                     for (int i = 0; i < N; i++) {
     328                    for (int i = 0; i < maxDim; i++) {
    324329                        int nIdx = i;
    325330                        int gIdx = assignment.sol(i);
     
    369374
    370375        // Move old nodes to new positions
    371         for (Node node : nodeAssoc.keySet()) {
    372             commands.add(new MoveCommand(nodeAssoc.get(node), node.getCoor()));
     376        for (Map.Entry<Node, Node> entry : nodeAssoc.entrySet()) {
     377            commands.add(new MoveCommand(entry.getValue(), entry.getKey().getCoor()));
    373378        }
    374379
     
    394399     * @return list of distinct nodes that are not tagged and not used anywhere except in the way
    395400     */
    396     protected static List<Node> getUnimportantNodes(Way way) {
     401    static List<Node> getUnimportantNodes(Way way) {
    397402        List<Node> nodePool = new LinkedList<>();
    398403        for (Node n : way.getNodes()) {
     
    410415     * role membership), and thus cannot be safely modified.
    411416     */
    412     protected static boolean hasImportantNode(Way geometry, Way way) {
     417    static boolean hasImportantNode(Way geometry, Way way) {
    413418        for (Node n : way.getNodes()) {
    414419            // if original and replacement way share a node, it's safe to replace
     
    429434    }
    430435
    431     protected static boolean hasInterestingKey(OsmPrimitive object) {
    432         for (String key : object.getKeys().keySet()) {
    433             if (!OsmPrimitive.isUninterestingKey(key)) {
    434                 return true;
    435             }
    436         }
    437         return false;
    438     }
    439 
    440     protected static boolean isInArea(Node node, Area area) {
     436    static boolean hasInterestingKey(OsmPrimitive object) {
     437        return object.getKeys().keySet().stream().anyMatch(k -> !OsmPrimitive.isUninterestingKey(k));
     438    }
     439
     440    static boolean isInArea(Node node, Area area) {
    441441        LatLon ll = node.getCoor();
    442         if (node.isNewOrUndeleted() || area == null || ll == null || area.contains(ll.getX(), ll.getY())) {
    443             return true;
    444         }
    445         return false;
    446     }
    447 
    448     protected static boolean isInArea(Way way, Area area) {
     442        return node.isNewOrUndeleted() || area == null || ll == null || area.contains(ll.getX(), ll.getY());
     443    }
     444
     445    static boolean isInArea(Way way, Area area) {
    449446        if (area == null) {
    450447            return true;
     
    469466     * @throws UserCancelException If the user cancelled a dialog.
    470467     */
    471     protected static List<Command> getTagConflictResolutionCommands(OsmPrimitive source, OsmPrimitive target) throws UserCancelException {
     468    static List<Command> getTagConflictResolutionCommands(OsmPrimitive source, OsmPrimitive target) throws UserCancelException {
    472469        Collection<OsmPrimitive> primitives = Arrays.asList(source, target);
    473470        // launch a conflict resolution dialog, if necessary
     
    480477     * @return null if there is no such node.
    481478     */
    482     protected static Node findNearestNode(Node node, Collection<Node> nodes) {
     479    static Node findNearestNode(Node node, Collection<Node> nodes) {
    483480        if (nodes.contains(node))
    484481            return node;
Note: See TracChangeset for help on using the changeset viewer.