Skip to content

Fix wrong coverage around antimeridian #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions geo/src/main/java/com/github/davidmoten/geo/GeoHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,35 +633,42 @@ void add(long l) {
}
}

static CoverageLongs coverBoundingBoxLongs(double topLeftLat, final double topLeftLon,
final double bottomRightLat, final double bottomRightLon, final int length) {
static CoverageLongs coverBoundingBoxLongs(double topLeftLat, double topLeftLon,
double bottomRightLat, double bottomRightLon, final int length) {
Preconditions.checkArgument(topLeftLat >= bottomRightLat,
"topLeftLat must be >= bottomRighLat");
Preconditions.checkArgument(topLeftLon <= bottomRightLon,
"topLeftLon must be <= bottomRighLon");
Preconditions.checkArgument(length > 0, "length must be greater than zero");
final double actualWidthDegreesPerHash = widthDegrees(length);
final double actualHeightDegreesPerHash = heightDegrees(length);

LongSet hashes = new LongSet();

double diff = longitudeDiff(bottomRightLon, topLeftLon);
double maxLon = topLeftLon + diff;
double diff = bottomRightLon - topLeftLon;
if (diff < 0) {
// case where bottomRightLon cross the antimeridian
bottomRightLon += 360;
diff = bottomRightLon - topLeftLon;
} else if (diff > 360) {
// case where this bounding box displays more than one copy of the world
topLeftLon = -180;
bottomRightLon = 180;
diff = 360;
}

for (double lat = bottomRightLat; lat <= topLeftLat; lat += actualHeightDegreesPerHash) {
for (double lon = topLeftLon; lon <= maxLon; lon += actualWidthDegreesPerHash) {
hashes.add(encodeHashToLong(lat, lon, length));
for (double lon = topLeftLon; lon <= bottomRightLon; lon += actualWidthDegreesPerHash) {
hashes.add(encodeHashToLong(lat, to180(lon), length));
}
}
// ensure have the borders covered
for (double lat = bottomRightLat; lat <= topLeftLat; lat += actualHeightDegreesPerHash) {
hashes.add(encodeHashToLong(lat, maxLon, length));
hashes.add(encodeHashToLong(lat, to180(bottomRightLon), length));
}
for (double lon = topLeftLon; lon <= maxLon; lon += actualWidthDegreesPerHash) {
hashes.add(encodeHashToLong(topLeftLat, lon, length));
for (double lon = topLeftLon; lon <= bottomRightLon; lon += actualWidthDegreesPerHash) {
hashes.add(encodeHashToLong(topLeftLat, to180(lon), length));
}
// ensure that the topRight corner is covered
hashes.add(encodeHashToLong(topLeftLat, maxLon, length));
hashes.add(encodeHashToLong(topLeftLat, to180(bottomRightLon), length));

double areaDegrees = diff * (topLeftLat - bottomRightLat);
double coverageAreaDegrees = hashes.count * widthDegrees(length) * heightDegrees(length);
Expand Down
81 changes: 81 additions & 0 deletions geo/src/test/java/com/github/davidmoten/geo/CoverageTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.davidmoten.geo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

Expand Down Expand Up @@ -28,4 +29,84 @@ public void testCoverageOfAnAreaThatCantBeCoveredWithHashOfLengthOne() {
assertEquals(1, coverage.getHashLength());
assertEquals(Sets.newHashSet("q", "r"), coverage.getHashes());
}

/**
* test copied from https://github.com/davidmoten/geo/pull/25/files to validate bug fix
*/
@Test
public void testWideCoverage() {
double top = -1;
double left = -26;
double bottom = -2;
double right = 175;

Coverage coverage = GeoHash.coverBoundingBox(top, left, bottom, right, 1);
assertTrue(coverage.getHashes().contains("r"));
}

/**
* test copied from https://github.com/davidmoten/geo/issues/34 to validate bug fix
*/
@Test
public void testCoverageAllWorld() {
double topLeftLat = 90d;
double topLeftLon = -179d;
double bottomRightLat = -90d;
double bottomRightLon = 180d;
Coverage coverage = GeoHash.coverBoundingBox(topLeftLat, topLeftLon, bottomRightLat, bottomRightLon, 1);
assertEquals(Sets.newHashSet("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"),
coverage.getHashes());
}

@Test
public void testCoverageAllWorldLeaflet() {
double topLeftLat = 90;
double topLeftLon = -703;
double bottomRightLat = -90;
double bottomRightLon = 624;
Coverage coverage = GeoHash.coverBoundingBox(topLeftLat, topLeftLon, bottomRightLat, bottomRightLon, 1);
assertEquals(Sets.newHashSet("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"),
coverage.getHashes());
}

@Test
public void testCoverageAntimeridianGoogleMaps() {
double topLeftLat = 39;
double topLeftLon = 156;
double bottomRightLat = 3;
double bottomRightLon = -118;
Coverage coverage = GeoHash.coverBoundingBox(topLeftLat, topLeftLon, bottomRightLat, bottomRightLon, 1);
assertEquals(Sets.newHashSet("x", "8", "9"), coverage.getHashes());
}

@Test
public void testCoverageAntimeridianLeaflet() {
double topLeftLat = 39;
double topLeftLon = -204;
double bottomRightLat = 2;
double bottomRightLon = -121;
Coverage coverage = GeoHash.coverBoundingBox(topLeftLat, topLeftLon, bottomRightLat, bottomRightLon, 1);
assertEquals(Sets.newHashSet("x", "8", "9"), coverage.getHashes());
}

@Test
public void testCoverageAntimeridianLeaflet2() {
double topLeftLat = 44;
double topLeftLon = 110;
double bottomRightLat = 9;
double bottomRightLon = 194;
Coverage coverage = GeoHash.coverBoundingBox(topLeftLat, topLeftLon, bottomRightLat, bottomRightLon, 1);
assertEquals(Sets.newHashSet("w", "x", "8"), coverage.getHashes());
}

@Test
public void testCoverageMaxHashes() {
double topLeftLat = 50.1112;
double topLeftLon = -6.8167;
double bottomRightLat = 46.6997;
double bottomRightLon = 3.7301;
Coverage coverage = GeoHash.coverBoundingBoxMaxHashes(topLeftLat, topLeftLon, bottomRightLat, bottomRightLon, 64);
assertEquals(3, coverage.getHashLength());
assertEquals(24, coverage.getHashes().size());
}
}
4 changes: 0 additions & 4 deletions geo/src/test/java/com/github/davidmoten/geo/GeoHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,4 @@ public void testCoverBoundingBoxPreconditionLat() {
GeoHash.coverBoundingBox(0, 100, 10, 120);
}

@Test(expected=IllegalArgumentException.class)
public void testCoverBoundingBoxPreconditionLon() {
GeoHash.coverBoundingBox(10, 120, 0, 100);
}
}