Skip to content

Commit

Permalink
ShardBucketCollection (#184)
Browse files Browse the repository at this point in the history
* ShardBucketCollection

* PR comments addressed
  • Loading branch information
jklamer authored and matthieun committed Aug 6, 2018
1 parent 61447c0 commit 7f1e7a6
Show file tree
Hide file tree
Showing 6 changed files with 1,012 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/java/org/openstreetmap/atlas/geography/Rectangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,55 @@ public Location center()
return new Segment(this.lowerLeft, this.upperRight).middle();
}

/**
* Contract the rectangle in 4 directions as far as possible. If the distance to move the
* corners would invert the rectangle then the side(s) will collapse into length 0. The most
* that it can contract is to a single point in the middle.
*
* @param distance
* to contract the four corners
* @return new rectangle with contracted dimensions
*/
public Rectangle contract(final Distance distance)
{
final Location newLowerLeft = this.lowerLeft.shiftAlongGreatCircle(Heading.NORTH, distance)
.shiftAlongGreatCircle(Heading.EAST, distance);
final Location newUpperRight = this.upperRight
.shiftAlongGreatCircle(Heading.SOUTH, distance)
.shiftAlongGreatCircle(Heading.WEST, distance);
final boolean tooShortHeight = newLowerLeft.getLatitude()
.isGreaterThan(newUpperRight.getLatitude());
final boolean tooShortWidth = newLowerLeft.getLongitude()
.isGreaterThan(newUpperRight.getLongitude());
if (tooShortHeight && tooShortWidth)
{
return this.center().bounds();
}
else
{
final Location lowerRight = new Location(this.lowerLeft().getLatitude(),
this.upperRight().getLongitude());
if (tooShortHeight)
{
final Latitude sharedLatitude = lowerRight.midPoint(this.upperRight())
.getLatitude();
return forCorners(new Location(sharedLatitude, newLowerLeft.getLongitude()),
new Location(sharedLatitude, newUpperRight.getLongitude()));
}
else if (tooShortWidth)
{
final Longitude sharedLongitude = lowerRight.midPoint(this.lowerLeft())
.getLongitude();
return forCorners(new Location(newLowerLeft.getLatitude(), sharedLongitude),
new Location(newUpperRight.getLatitude(), sharedLongitude));
}
else
{
return forCorners(newLowerLeft, newUpperRight);
}
}
}

/**
* @param that
* The other {@link Rectangle} to combine
Expand Down
Loading

0 comments on commit 7f1e7a6

Please sign in to comment.