From 9aaaf4cd1222a16a5f89ba64a3ea1a6087dc81dd Mon Sep 17 00:00:00 2001 From: wenjj2000 Date: Mon, 4 Mar 2024 10:00:46 +0100 Subject: [PATCH] Squashed commit of the following: commit 279212f7e85e923793eaace978cbbcb666508096 Author: Linus Wallin Date: Mon Mar 4 09:46:08 2024 +0100 fix: solves bug where precision is allowed to be too high Limits the precision in containingGeohash function to 6 from 24 since the toGeohash function has been updated to only work for highest precision 6. commit 2aa650f094120e01c3aa9b2329698b3b88b8dfe0 Merge: 263f8e2 dcdb58d Author: Linus Wallin Date: Mon Mar 4 09:45:04 2024 +0100 Merge branch '213-geometry-to-geohash' of github.com:DD2480-Group-3/geometry-api-java into 54-coveringGeohash commit 263f8e2a10d3ee5cb4815548d30abe6b19be26d1 Author: Linus Wallin Date: Sun Mar 3 19:47:34 2024 +0100 refactor: removed missed empty line commit 72b84f42b3e101f9aadc0b336f602cbe30bdfe9a Author: Linus Wallin Date: Sun Mar 3 19:47:05 2024 +0100 refactor: removed empty line commit 6fef31241f5724a43805781e0062256056ad45b5 Author: Linus Wallin Date: Sun Mar 3 19:45:34 2024 +0100 refactor: removed empty lines commit 1287b2c1ea051537ffc11f29f200ea88dfa47e99 Author: Linus Wallin Date: Sun Mar 3 19:39:49 2024 +0100 test: changed envelope to encompass 4 different parts of the geo grid The envelope was previously assigned wrong max x and y values, which resulted in errors as the coveringGeohash function didn't return the expected amount of geo hashes. commit 69ca9c0ffbfba83d59d6f4528bb3230c596aac1c Author: Linus Wallin Date: Sun Mar 3 19:30:38 2024 +0100 fix: solves bug which resulted in wrong geohashes commit 2a8d13169998e8dda67b8c737623c09ccd01438b Author: Linus Wallin Date: Sun Mar 3 19:24:32 2024 +0100 test: updated tests to match the changes of the function in last commit commit bb45871770a7d31861a166d0d6a3ae52892b5866 Author: Linus Wallin Date: Sun Mar 3 19:24:02 2024 +0100 refactor: made the return string array dynamic commit 708542e0eb04197d2049e6e46dae3722f0f4c048 Author: Linus Wallin Date: Sun Mar 3 18:42:15 2024 +0100 refactor: removes indentation error caused by merge commit a216691f06191097ed230c771c15e739d8ee32d5 Merge: 4420765 62f7d5f Author: Linus Wallin Date: Sun Mar 3 18:41:36 2024 +0100 Merge branch '213-geometry-to-geohash' of github.com:DD2480-Group-3/geometry-api-java into 54-coveringGeohash commit 4420765466991ff31b9ffd31fa321740db3b8502 Author: Linus Wallin Date: Sun Mar 3 18:37:30 2024 +0100 test: added test cases for coveringGeohash function commit 2c319798289907fa33f165df62cf5b7dcc41f2b0 Author: Linus Wallin Date: Sun Mar 3 18:15:40 2024 +0100 refactor: removes unnecessary if statement Removes if statement which didn't change the outcome of the program. commit e0b04d3959305a7bb5fb5ab22c05ce6f102283bb Author: Linus Wallin Date: Sun Mar 3 17:39:37 2024 +0100 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 | 36 ++++++++++++- .../com/esri/core/geometry/TestGeohash.java | 53 +++++++++++++++++-- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/esri/core/geometry/Geohash.java b/src/main/java/com/esri/core/geometry/Geohash.java index 87070786..33987207 100644 --- a/src/main/java/com/esri/core/geometry/Geohash.java +++ b/src/main/java/com/esri/core/geometry/Geohash.java @@ -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; @@ -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; } } diff --git a/src/test/java/com/esri/core/geometry/TestGeohash.java b/src/test/java/com/esri/core/geometry/TestGeohash.java index 662e65df..ab09d327 100644 --- a/src/test/java/com/esri/core/geometry/TestGeohash.java +++ b/src/test/java/com/esri/core/geometry/TestGeohash.java @@ -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]); + } +} \ No newline at end of file