Skip to content

Commit

Permalink
feat: coveringGeohash funciton added
Browse files Browse the repository at this point in the history
Adds funtion which given an envelope returns up to four geohashes
which cover the envelope.
  • Loading branch information
LinusWallin committed Mar 3, 2024
1 parent dbe750f commit e0b04d3
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/main/java/com/esri/core/geometry/Geohash.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,44 @@ 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[] {};

String[] geoHashes = new String[4];

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 (xmin == xmax && ymin == ymax) {
Point2D p = new Point2D(xmax, ymax);
return geoHash;
}
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;

if (deltaLon * deltaLat > 4){
return geoHashes;
} else {
for (int i = 0; i < deltaLon; i++){
for (int j = 0; j < deltaLat; j++){
Point2D p = new Point2D(gridMinLon + i * grid, gridMinLat + j * grid);
geoHashes[i*deltaLat] = toGeohash(p, 1);
}
}
}
return geoHashes;
}
}

0 comments on commit e0b04d3

Please sign in to comment.