From e0b04d3959305a7bb5fb5ab22c05ce6f102283bb Mon Sep 17 00:00:00 2001 From: Linus Wallin Date: Sun, 3 Mar 2024 17:39:37 +0100 Subject: [PATCH] feat: coveringGeohash funciton added Adds funtion which given an envelope returns up to four geohashes which cover the envelope. --- .../java/com/esri/core/geometry/Geohash.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/esri/core/geometry/Geohash.java b/src/main/java/com/esri/core/geometry/Geohash.java index f9e95869..5e1b9c37 100644 --- a/src/main/java/com/esri/core/geometry/Geohash.java +++ b/src/main/java/com/esri/core/geometry/Geohash.java @@ -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; } }