Skip to content
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

54 covering geohash #55

Merged
merged 1 commit into from
Mar 4, 2024
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
36 changes: 34 additions & 2 deletions src/main/java/com/esri/core/geometry/Geohash.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static String containingGeohash(Envelope2D envelope) {
double deltaLon = 360;
double deltaLat = 180;

while (xmin == xmax && ymin == ymax && chars < 25) {
while (xmin == xmax && ymin == ymax && chars < 7) {
if (chars % 2 == 0) {
deltaLon = deltaLon / 8;
deltaLat = deltaLat / 4;
Expand Down Expand Up @@ -215,6 +215,38 @@ public static String containingGeohash(Envelope2D envelope) {
* @return up to four geohashes that completely cover given envelope
*/
public static String[] coveringGeohash(Envelope2D envelope) {
return new String[] {};
double xmin = envelope.xmin;
double ymin = envelope.ymin;
double xmax = envelope.xmax;
double ymax = envelope.ymax;

if (NumberUtils.isNaN(xmax)) {
return new String[] {""};
}
String[] geoHash = {containingGeohash(envelope)};
if (geoHash[0] != ""){
return geoHash;
}

int grid = 45;
int gridMaxLon = (int)Math.floor(xmax/grid);
int gridMinLon = (int)Math.floor(xmin/grid);
int gridMaxLat = (int)Math.floor(ymax/grid);
int gridMinLat = (int)Math.floor(ymin/grid);
int deltaLon = gridMaxLon - gridMinLon + 1;
int deltaLat = gridMaxLat - gridMinLat + 1;
String[] geoHashes = new String[deltaLon * deltaLat];

if (deltaLon * deltaLat > 4){
return new String[] {""};
} else {
for (int i = 0; i < deltaLon; i++){
for (int j = 0; j < deltaLat; j++){
Point2D p = new Point2D(xmin + i * grid, ymin + j * grid);
geoHashes[i*deltaLat + j] = toGeohash(p, 1);
}
}
}
return geoHashes;
}
}
53 changes: 50 additions & 3 deletions src/test/java/com/esri/core/geometry/TestGeohash.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,54 @@ public void testContainingGeohash() {

@Test
public void testContainingGeohash2() {
Envelope2D envelope = new Envelope2D(18.078, 59.3564, 18.1, 59.3344);
assertEquals("u6sce", Geohash.containingGeohash(envelope));
Envelope2D envelope = new Envelope2D(18.078, 59.3564, 18.1, 59.3344);
assertEquals("u6sce", Geohash.containingGeohash(envelope));
}
}

@Test
public void testCoveringGeohashEmptyEnvelope() {
Envelope2D emptyEnv = new Envelope2D();
String [] coverage = Geohash.coveringGeohash(emptyEnv);
}

@Test
public void testCoveringGeohashOneGeohash() {
Envelope2D env = new Envelope2D(-180, -90, -149, -49);
String [] coverage = Geohash.coveringGeohash(env);
assertEquals("0", coverage[0]);
}

@Test
public void testCoveringGeohashPoint() {
Envelope2D env = new Envelope2D(180,90,180,90);
String [] coverage = Geohash.coveringGeohash(env);
assertEquals("zzzzzz", coverage[0]);
}

@Test
public void testCoveringGeohashTwoGeohashes() {
Envelope2D env = new Envelope2D(-180, -90, -180, -35);
String [] coverage = Geohash.coveringGeohash(env);
assertEquals("0", coverage[0]);
assertEquals("2", coverage[1]);
}

@Test
public void testCoveringGeohashThreeGeohashes() {
Envelope2D env = new Envelope2D(-180, -90, -180, 5);
String [] coverage = Geohash.coveringGeohash(env);
assertEquals("0", coverage[0]);
assertEquals("2", coverage[1]);
assertEquals("8", coverage[2]);
}

@Test
public void testCoveringGeohashFourGeohashes() {
Envelope2D env = new Envelope2D(-180, -90, -130, -40);
String [] coverage = Geohash.coveringGeohash(env);
assertEquals("0", coverage[0]);
assertEquals("2", coverage[1]);
assertEquals("1", coverage[2]);
assertEquals("3", coverage[3]);
}
}