Changeset 19619 in josm


Ignore:
Timestamp:
2026-08-29T10:42:43+02:00 (72 minutes ago)
Author:
GerdP
Message:

Fix #24851: Power line checks won't report existing non-power nodes

  • add the related parent objects to the error message so that they are not considered as unrelated when a partial validation is done on upload
  • unit test by gaben (thanks!)
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/PowerLines.java

    r18871 r19619  
    1212import java.util.HashMap;
    1313import java.util.HashSet;
     14import java.util.LinkedHashSet;
    1415import java.util.List;
    1516import java.util.Map;
     17import java.util.Map.Entry;
    1618import java.util.Set;
     19import java.util.stream.Collectors;
    1720
    1821import org.openstreetmap.josm.data.coor.ILatLon;
     
    7679    private double hillyCompensation;
    7780    private double hillyThreshold;
    78     private final Set<Node> badConnections = new HashSet<>();
    79     private final Set<Node> missingTags = new HashSet<>();
     81    private final Map<Node, Set<OsmPrimitive>> badConnections = new HashMap<>();
     82    private final Map<Node, Set<OsmPrimitive>> missingTags = new HashMap<>();
    8083    private final Set<Way> wrongLineType = new HashSet<>();
    8184    private final Set<WaySegment> missingNodes = new HashSet<>();
     
    99102    @Override
    100103    public void visit(Node n) {
    101         boolean nodeInLineOrCable = false;
    102         boolean connectedToUnrelated = false;
    103         for (Way parent : n.getParentWays()) {
    104             if (parent.hasTag(POWER, "line", MINOR_LINE, "cable"))
    105                 nodeInLineOrCable = true;
    106             else if (!isRelatedToPower(parent))
    107                 connectedToUnrelated = true;
    108         }
    109         if (nodeInLineOrCable && connectedToUnrelated)
    110             badConnections.add(n);
     104        if (!n.isConnectionNode() || n.referrers(Way.class).noneMatch(w -> isPowerLineOrCable(w)))
     105            return;
     106
     107        List<Way> unrelatedParents = n.referrers(Way.class).filter(w -> !isPowerLineOrCable(w) && !isRelatedToPower(w))
     108                .collect(Collectors.toList());
     109        if (!unrelatedParents.isEmpty()) {
     110            Set<OsmPrimitive> set = badConnections.computeIfAbsent(n, k -> new HashSet<>());
     111            set.addAll(unrelatedParents);
     112        }
    111113    }
    112114
     
    163165        }
    164166        // Then return the errors
    165         for (Node n : missingTags) {
     167        for (Entry<Node, Set<OsmPrimitive>> entry : missingTags.entrySet()) {
     168            Node n = entry.getKey();
    166169            if (!isInPowerStation(n)) {
    167170                errors.add(TestError.builder(this, Severity.WARNING, POWER_SUPPORT)
    168171                        // the "missing tag" grouping can become broken if the MapCSS message get reworded
    169172                        .message(tr("missing tag"), tr("node without power=*"))
    170                         .primitives(n)
     173                        .primitives(getAllPrimitives(entry))
     174                        .highlight(n)
    171175                        .build());
    172176            }
    173177        }
    174178
    175         for (Node n : badConnections) {
     179        for (Entry<Node, Set<OsmPrimitive>> entry : badConnections.entrySet()) {
    176180            errors.add(TestError.builder(this, Severity.WARNING, POWER_CONNECTION)
    177181                    .message(tr("Node connects a power line or cable with an object "
    178182                            + "which is not related to the power infrastructure"))
    179                     .primitives(n)
     183                    .primitives(getAllPrimitives(entry))
     184                    .highlight(entry.getKey())
    180185                    .build());
    181186        }
     
    222227
    223228        super.endTest();
     229    }
     230
     231    /**
     232     * Combine the node and the related objects.
     233     * @param entry a map entry with a node and related objects
     234     * @return set containing the node and the related objects
     235     */
     236    private Collection<? extends OsmPrimitive> getAllPrimitives(Entry<Node, Set<OsmPrimitive>> entry) {
     237        Set<OsmPrimitive> primitives = new LinkedHashSet<>();
     238        primitives.add(entry.getKey());
     239        primitives.addAll(entry.getValue());
     240        return primitives;
    224241    }
    225242
     
    254271            /// handle missing power line support tags (e.g. tower)
    255272            if (!isPowerTower(n) && !isPowerInfrastructure(n) && IN_DOWNLOADED_AREA.test(n)
    256                     && (!w.isFirstLastNode(n) || !isPowerStation(n)))
    257                 missingTags.add(n);
     273                    && (!w.isFirstLastNode(n) || !isPowerStation(n))) {
     274                Set<OsmPrimitive> set = missingTags.computeIfAbsent(n, k -> new HashSet<>());
     275                set.add(w);
     276            }
    258277
    259278            /// handle missing nodes
     
    670689
    671690    /**
     691     * Determines if the specified way denotes a power line or cable.
     692     * @param w The way to be tested
     693     * @return {@code true} if power key is set and equal to line,minor_line or cable
     694     */
     695    protected static boolean isPowerLineOrCable(Way w) {
     696        return isPowerIn(w, Arrays.asList("line", MINOR_LINE, "cable"));
     697    }
     698
     699    /**
    672700     * Determines if the specified primitive denotes a power station.
    673701     * @param p The primitive to be tested
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/PowerLinesTest.java

    r19519 r19619  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.data.validation.tests;
    3 
    4 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
    5 import static org.junit.jupiter.api.Assertions.assertFalse;
    6 import static org.junit.jupiter.api.Assertions.assertTrue;
    7 
    8 import java.util.ArrayList;
    93
    104import org.junit.jupiter.api.BeforeEach;
     
    1812import org.openstreetmap.josm.data.osm.TagMap;
    1913import org.openstreetmap.josm.data.osm.Way;
     14import org.openstreetmap.josm.data.validation.TestError;
    2015import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    2116import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    2217import org.openstreetmap.josm.testutils.annotations.Projection;
     18
     19import java.util.ArrayList;
     20
     21import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
     22import static org.junit.jupiter.api.Assertions.assertFalse;
     23import static org.junit.jupiter.api.Assertions.assertTrue;
    2324
    2425/**
     
    160161        assertTrue(this.powerLines.getErrors().isEmpty());
    161162    }
     163
     164    /**
     165     * Test for ticket #24851.
     166     * Simulates connecting a power line to an existing highway node without power tags.
     167     * Validates that the resulting error contains both the Node AND the Way, so it
     168     * doesn't get filtered out during partial validation on upload.
     169     */
     170    @Test
     171    void testTicket24851_ReportExistingNonPowerNodes() {
     172        Node sharedNode = new Node(new LatLon(0, 0)); // no power tag attached
     173
     174        // unrelated highway way
     175        Way highway = TestUtils.newWay("highway=unclassified",
     176                sharedNode, new Node(new LatLon(0.1, 0)));
     177
     178        // power line way
     179        Way powerline = TestUtils.newWay("power=line",
     180                sharedNode, new Node(new LatLon(0, 0.1)));
     181
     182        // second node has a valid tag
     183        powerline.getNode(1).put("power", "tower");
     184
     185        ds.addPrimitiveRecursive(highway);
     186        ds.addPrimitiveRecursive(powerline);
     187
     188        powerLines.startTest(NullProgressMonitor.INSTANCE);
     189        for (Way w : ds.getWays()) {
     190            powerLines.visit(w);
     191        }
     192        for (Node n : ds.getNodes()) {
     193            powerLines.visit(n);
     194        }
     195        powerLines.endTest();
     196
     197        assertFalse(powerLines.getErrors().isEmpty(), "Errors should be generated for the missing tag and bad connection");
     198
     199        boolean foundSupportError = false;
     200        boolean foundConnectionError = false;
     201
     202        for (TestError error : powerLines.getErrors()) {
     203            // verify POWER_SUPPORT behavior (missing tag)
     204            if (error.getCode() == PowerLines.POWER_SUPPORT && error.getPrimitives().contains(sharedNode)) {
     205                foundSupportError = true;
     206                assertTrue(error.getPrimitives().contains(powerline),
     207                        "MUST contain the parent powerline way. This prevents JOSM from discarding the error " +
     208                                "during partial validation if the node itself was unmodified.");
     209            }
     210            // verify POWER_CONNECTION behavior (bad connection)
     211            if (error.getCode() == PowerLines.POWER_CONNECTION && error.getPrimitives().contains(sharedNode)) {
     212                foundConnectionError = true;
     213                assertTrue(error.getPrimitives().contains(highway),
     214                        "MUST contain the unrelated parent way. This prevents JOSM from discarding the error" +
     215                                "during partial validation if the node itself was unmodified.");
     216            }
     217        }
     218
     219        assertTrue(foundSupportError, "Should report missing power tag on shared node");
     220        assertTrue(foundConnectionError, "Should report bad connection on shared node");
     221    }
    162222}
Note: See TracChangeset for help on using the changeset viewer.