Changeset 18675 in josm


Ignore:
Timestamp:
2023-02-22T19:14:00+01:00 (14 months ago)
Author:
taylor.smock
Message:

Fix #22684: IAE: Parameter 'en1' must not be null in PowerLines.addWaterWaySegments (patch by gaben, modified)

PowerLines.addWaterWaySegments now only adds segments where all nodes have
position data.

This additionally fixes a similar issue caused by inspecting an incomplete
way in InspectPrimitiveDataText. In this case, the centroid is still
calculated, but does not account for how nodes without positional data
will affect the centroid.

Location:
trunk
Files:
4 edited

Legend:

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

    r18553 r18675  
    131131        for (int i = 0; i < w.getNodesCount() - 1; i++) {
    132132            final WaySegment es1 = new WaySegment(w, i);
    133             CrossingWays.getSegments(this.cellSegmentsWater, es1.getFirstNode(), es1.getSecondNode()).forEach(list -> list.add(es1));
     133            final Node first = es1.getFirstNode();
     134            final Node second = es1.getSecondNode();
     135
     136            if (first.isLatLonKnown() && second.isLatLonKnown()) {
     137                CrossingWays.getSegments(this.cellSegmentsWater, first, second).forEach(list -> list.add(es1));
     138            }
    134139        }
    135140    }
  • trunk/src/org/openstreetmap/josm/tools/Geometry.java

    r18590 r18675  
    939939     */
    940940    public static EastNorth getCentroid(List<? extends INode> nodes) {
    941         return getCentroidEN(nodes.stream().map(INode::getEastNorth).collect(Collectors.toList()));
     941        return getCentroidEN(nodes.stream().filter(INode::isLatLonKnown).map(INode::getEastNorth).collect(Collectors.toList()));
    942942    }
    943943
     
    955955        } else if (size == 2) {
    956956            return nodes.get(0).getCenter(nodes.get(1));
     957        } else if (size == 0) {
     958            return null;
    957959        }
    958960
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/PowerLinesTest.java

    r18553 r18675  
    22package org.openstreetmap.josm.data.validation.tests;
    33
     4import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
    45import static org.junit.jupiter.api.Assertions.assertFalse;
    56import static org.junit.jupiter.api.Assertions.assertTrue;
     
    78import org.junit.jupiter.api.BeforeEach;
    89import org.junit.jupiter.api.Test;
    9 import org.junit.jupiter.api.extension.RegisterExtension;
     10import org.openstreetmap.josm.TestUtils;
    1011import org.openstreetmap.josm.data.coor.LatLon;
    1112import org.openstreetmap.josm.data.osm.DataSet;
    1213import org.openstreetmap.josm.data.osm.Node;
     14import org.openstreetmap.josm.data.osm.Relation;
     15import org.openstreetmap.josm.data.osm.RelationMember;
    1316import org.openstreetmap.josm.data.osm.TagMap;
    1417import org.openstreetmap.josm.data.osm.Way;
    15 import org.openstreetmap.josm.testutils.JOSMTestRules;
    1618import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
     19import org.openstreetmap.josm.testutils.annotations.Projection;
    1720
    1821/**
     
    2124 */
    2225@BasicPreferences
     26@Projection
    2327class PowerLinesTest {
    2428    private PowerLines powerLines;
    2529    private DataSet ds;
    26 
    27     @RegisterExtension
    28     static JOSMTestRules josmTestRules = new JOSMTestRules().projection();
    2930
    3031    @BeforeEach
     
    119120        assertFalse(powerLines.getErrors().isEmpty());
    120121    }
     122
     123    /**
     124     * Ensure that incomplete relations don't cause problems
     125     */
     126    @Test
     127    void testNonRegression22684() {
     128        final Relation powerLine = TestUtils.newRelation("natural=water water=river",
     129                new RelationMember("", TestUtils.newWay("", new Node(), new Node())));
     130        assertDoesNotThrow(() -> this.powerLines.visit(powerLine));
     131    }
    121132}
  • trunk/test/unit/org/openstreetmap/josm/tools/GeometryTest.java

    r18590 r18675  
    22package org.openstreetmap.josm.tools;
    33
     4import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
    45import static org.junit.jupiter.api.Assertions.assertEquals;
    56import static org.junit.jupiter.api.Assertions.assertFalse;
    67import static org.junit.jupiter.api.Assertions.assertNotEquals;
    78import static org.junit.jupiter.api.Assertions.assertNotNull;
     9import static org.junit.jupiter.api.Assertions.assertNull;
    810import static org.junit.jupiter.api.Assertions.assertTrue;
    911
     
    1820import java.util.stream.Stream;
    1921
    20 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    2122import org.junit.jupiter.api.Test;
    22 import org.junit.jupiter.api.extension.RegisterExtension;
    2323import org.junit.jupiter.params.ParameterizedTest;
    2424import org.junit.jupiter.params.provider.Arguments;
     
    3939import org.openstreetmap.josm.data.projection.Projections;
    4040import org.openstreetmap.josm.io.OsmReader;
    41 import org.openstreetmap.josm.testutils.JOSMTestRules;
     41import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
    4242
    4343/**
    4444 * Unit tests of {@link Geometry} class.
    4545 */
     46@BasicPreferences
     47@org.openstreetmap.josm.testutils.annotations.Projection
    4648class GeometryTest {
    47     /**
    48      * Primitives need preferences and projection.
    49      */
    50     @RegisterExtension
    51     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    52     static JOSMTestRules test = new JOSMTestRules().preferences().projection();
    53 
    5449    /**
    5550     * Test of {@link Geometry#getLineLineIntersection} method.
     
    578573        assertEquals(angle, Math.toDegrees(original.bearing(actual)), 0.000_001);
    579574    }
     575
     576    /**
     577     * A non-regression test for an issue found during the investigation of #22684 (see comment:3 by GerdP)
     578     */
     579    @Test
     580    void testNonRegression22684() {
     581        final EastNorth centroid1 = assertDoesNotThrow(() -> Geometry.getCentroid(Collections.singletonList(new Node())));
     582        assertNull(centroid1);
     583        final EastNorth centroid2 = assertDoesNotThrow(() -> Geometry.getCentroid(Arrays.asList(new Node(LatLon.ZERO), new Node())));
     584        assertTrue(new EastNorth(0, 0).equalsEpsilon(centroid2, 1e-9));
     585        final EastNorth centroid3 = assertDoesNotThrow(
     586                () -> Geometry.getCentroid(Arrays.asList(new Node(LatLon.ZERO), new Node(), new Node(LatLon.ZERO))));
     587        assertTrue(new EastNorth(0, 0).equalsEpsilon(centroid3, 1e-9));
     588    }
    580589}
Note: See TracChangeset for help on using the changeset viewer.