From c22774aad961728d160118aed68631364b954039 Mon Sep 17 00:00:00 2001 From: Vladimir Lemberg <62575608+vladlemberg@users.noreply.github.com> Date: Wed, 27 Jan 2021 10:16:24 -0800 Subject: [PATCH] add more common methods and unittest (#468) * add more common methods and unittest * rebase and fix merging conflicts --- .../atlas/checks/utility/CommonMethods.java | 32 +++++++++++ .../checks/utility/CommonMethodsTest.java | 23 ++++++++ .../checks/utility/CommonMethodsTestRule.java | 55 +++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/src/main/java/org/openstreetmap/atlas/checks/utility/CommonMethods.java b/src/main/java/org/openstreetmap/atlas/checks/utility/CommonMethods.java index c97151f65..37d44ac91 100644 --- a/src/main/java/org/openstreetmap/atlas/checks/utility/CommonMethods.java +++ b/src/main/java/org/openstreetmap/atlas/checks/utility/CommonMethods.java @@ -1,6 +1,7 @@ package org.openstreetmap.atlas.checks.utility; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import org.openstreetmap.atlas.geography.PolyLine; @@ -8,6 +9,7 @@ import org.openstreetmap.atlas.geography.atlas.items.Relation; import org.openstreetmap.atlas.geography.atlas.items.RelationMember; import org.openstreetmap.atlas.geography.atlas.walker.OsmWayWalker; +import org.openstreetmap.atlas.utilities.collections.Iterables; /** * Hold common Methods (should be used in more than one check) @@ -58,6 +60,36 @@ public static long getOSMRelationMemberSize(final Relation relation) }).distinct().count(); } + /** + * Check if given {@link Edge} is part of Closed Way. OSM wiki: + * https://wiki.openstreetmap.org/wiki/Item:Q4669 + * + * @param edge + * entity to check + * @return {@code true} if edge is part of closed way. + */ + public static boolean isClosedWay(final Edge edge) + { + final HashSet wayIds = new HashSet<>(); + Edge nextEdge = edge; + // Loop through outgoing edges with the same OSM id + while (nextEdge != null) + { + wayIds.add(nextEdge.getIdentifier()); + final List nextEdgeList = Iterables.stream(nextEdge.outEdges()) + .filter(Edge::isMainEdge) + .filter(outEdge -> outEdge.getOsmIdentifier() == edge.getOsmIdentifier()) + .collectToList(); + nextEdge = nextEdgeList.isEmpty() ? null : nextEdgeList.get(0); + // If original edge is found, the way is closed + if (nextEdge != null && wayIds.contains(nextEdge.getIdentifier())) + { + return true; + } + } + return false; + } + private CommonMethods() { // constructor diff --git a/src/test/java/org/openstreetmap/atlas/checks/utility/CommonMethodsTest.java b/src/test/java/org/openstreetmap/atlas/checks/utility/CommonMethodsTest.java index eaeda6397..3cbbe61bf 100644 --- a/src/test/java/org/openstreetmap/atlas/checks/utility/CommonMethodsTest.java +++ b/src/test/java/org/openstreetmap/atlas/checks/utility/CommonMethodsTest.java @@ -1,6 +1,8 @@ package org.openstreetmap.atlas.checks.utility; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Rule; import org.junit.Test; @@ -17,6 +19,12 @@ public class CommonMethodsTest @Rule public CommonMethodsTestRule setup = new CommonMethodsTestRule(); + @Test + public void testClosedWay() + { + assertTrue(CommonMethods.isClosedWay(this.setup.getClosedWay().edge(12000001))); + } + @Test public void testOneMemberRelationArtificialMember() { @@ -52,6 +60,21 @@ public void testOneMemberRelationSectioned() this.setup.getOneMemberRelationSectionedEdge().relation(123))); } + @Test + public void testOriginalWayGeometry() + { + final String origGeom = "LINESTRING (-71.7194204 18.4360044, -71.6970306 18.4360737, -71.7052283 18.4273807, -71.7194204 18.4360044)"; + assertEquals(origGeom, CommonMethods + .buildOriginalOsmWayGeometry(this.setup.getOriginalWayGeometry().edge(12000003)) + .toString()); + } + + @Test + public void testUnClosedWay() + { + assertFalse(CommonMethods.isClosedWay(this.setup.getUnClosedWay().edge(12000002))); + } + @Test public void testValidRelation() { diff --git a/src/test/java/org/openstreetmap/atlas/checks/utility/CommonMethodsTestRule.java b/src/test/java/org/openstreetmap/atlas/checks/utility/CommonMethodsTestRule.java index 7723abcc8..42c3d1525 100644 --- a/src/test/java/org/openstreetmap/atlas/checks/utility/CommonMethodsTestRule.java +++ b/src/test/java/org/openstreetmap/atlas/checks/utility/CommonMethodsTestRule.java @@ -106,6 +106,51 @@ public class CommonMethodsTestRule extends CoreTestRule @Member(id = "12000003", type = "edge", role = "") }) }) private Atlas oneMemberRelationSectionedEdge; + @TestAtlas( + // nodes + nodes = { @Node(id = "1", coordinates = @Loc(value = ONE)), + @Node(id = "2", coordinates = @Loc(value = TWO)), + @Node(id = "3", coordinates = @Loc(value = THREE)) }, + // edges + edges = { + @Edge(id = "12000001", coordinates = { @Loc(value = ONE), @Loc(value = TWO) }), + @Edge(id = "12000002", coordinates = { @Loc(value = TWO), + @Loc(value = THREE) }), + @Edge(id = "12000003", coordinates = { @Loc(value = THREE), + @Loc(value = ONE) }) }) + private Atlas closedWay; + + @TestAtlas( + // nodes + nodes = { @Node(id = "1", coordinates = @Loc(value = ONE)), + @Node(id = "2", coordinates = @Loc(value = TWO)), + @Node(id = "3", coordinates = @Loc(value = THREE)) }, + // edges + edges = { + @Edge(id = "12000001", coordinates = { @Loc(value = ONE), @Loc(value = TWO) }), + @Edge(id = "12000002", coordinates = { @Loc(value = TWO), + @Loc(value = THREE) }), }) + private Atlas unClosedWay; + + @TestAtlas( + // nodes + nodes = { @Node(id = "1", coordinates = @Loc(value = ONE)), + @Node(id = "2", coordinates = @Loc(value = TWO)), + @Node(id = "3", coordinates = @Loc(value = THREE)) }, + // edges + edges = { + @Edge(id = "12000001", coordinates = { @Loc(value = ONE), @Loc(value = TWO) }), + @Edge(id = "12000002", coordinates = { @Loc(value = TWO), + @Loc(value = THREE) }), + @Edge(id = "12000003", coordinates = { @Loc(value = THREE), + @Loc(value = ONE) }) }) + private Atlas originalWayGeometry; + + public Atlas getClosedWay() + { + return this.closedWay; + } + public Atlas getOneMemberRelationArtificialMember() { return this.oneMemberRelationArtificialMember; @@ -131,6 +176,16 @@ public Atlas getOneMemberRelationSectionedEdge() return this.oneMemberRelationSectionedEdge; } + public Atlas getOriginalWayGeometry() + { + return this.originalWayGeometry; + } + + public Atlas getUnClosedWay() + { + return this.unClosedWay; + } + public Atlas getValidRelation() { return this.validRelation;