Skip to content

Commit

Permalink
Polylines without duplicate consecutive shape points (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieun authored and MikeGost committed Jan 12, 2018
1 parent 3fa7f26 commit 1c9c2c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/org/openstreetmap/atlas/geography/PolyLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,37 @@ public String toWkt()
return new WktPolyLineConverter().convert(this);
}

/**
* @return This {@link PolyLine} without duplicate consecutive shape points. Non-consecutive
* shape points will remain unchanged.
*/
public PolyLine withoutDuplicateConsecutiveShapePoints()
{
final List<Location> shapePoints = new ArrayList<>();
boolean hasDuplicates = false;

final Iterator<Location> locationIterator = this.iterator();
// PolyLines are only valid if at least one point exists, so it is safe to call next() once.
Location previousLocation = locationIterator.next();
shapePoints.add(previousLocation);

while (locationIterator.hasNext())
{
final Location currentLocation = locationIterator.next();

if (!currentLocation.equals(previousLocation))
{
shapePoints.add(currentLocation);
}
else
{
hasDuplicates = true;
}
previousLocation = currentLocation;
}
return hasDuplicates ? new PolyLine(shapePoints) : this;
}

protected final List<Location> getPoints()
{
return this.points;
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/openstreetmap/atlas/geography/PolyLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,25 @@ public void testToString()
multipleLocationPolyLine.toString());
}

@Test
public void testWithoutDuplicateConsecutiveShapePoints()
{
final PolyLine polyLine1 = PolyLine.wkt(
"LINESTRING (-75.616326 40.194564, -75.616330 40.194570, -75.616330 40.194570, -75.616340 40.194580)");
final PolyLine polyLine2 = PolyLine.wkt(
"LINESTRING (-75.616326 40.194564, -75.616330 40.194570, -75.616340 40.194580)");
final PolyLine polyLine3 = PolyLine.wkt(
"LINESTRING (-75.616326 40.194564, -75.616330 40.194570, -75.616340 40.194580, -75.616330 40.194570)");
final PolyLine polyLine4 = PolyLine.wkt(
"LINESTRING (-75.616330 40.194570, -75.616330 40.194570, -75.616330 40.194570, -75.616340 40.194580, -75.616340 40.194590)");
final PolyLine polyLine5 = PolyLine.wkt(
"LINESTRING (-75.616330 40.194570, -75.616340 40.194580, -75.616340 40.194590)");
Assert.assertEquals(polyLine2.toWkt(),
polyLine1.withoutDuplicateConsecutiveShapePoints().toWkt());
Assert.assertEquals(polyLine3.toWkt(),
polyLine3.withoutDuplicateConsecutiveShapePoints().toWkt());
Assert.assertEquals(polyLine5.toWkt(),
polyLine4.withoutDuplicateConsecutiveShapePoints().toWkt());
}

}

0 comments on commit 1c9c2c4

Please sign in to comment.