Skip to content

Commit

Permalink
add more common methods and unittest (osmlab#468)
Browse files Browse the repository at this point in the history
* add more common methods and unittest

* rebase and fix merging conflicts
  • Loading branch information
vladlemberg authored Jan 27, 2021
1 parent 62507d7 commit c22774a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.openstreetmap.atlas.checks.utility;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.openstreetmap.atlas.geography.PolyLine;
import org.openstreetmap.atlas.geography.atlas.items.Edge;
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)
Expand Down Expand Up @@ -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<Long> wayIds = new HashSet<>();
Edge nextEdge = edge;
// Loop through outgoing edges with the same OSM id
while (nextEdge != null)
{
wayIds.add(nextEdge.getIdentifier());
final List<Edge> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit c22774a

Please sign in to comment.