Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 279212f
Author: Linus Wallin <linuswallin@live.se>
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 2aa650f
Merge: 263f8e2 dcdb58d
Author: Linus Wallin <linuswallin@live.se>
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 263f8e2
Author: Linus Wallin <linuswallin@live.se>
Date:   Sun Mar 3 19:47:34 2024 +0100

    refactor: removed missed empty line

commit 72b84f4
Author: Linus Wallin <linuswallin@live.se>
Date:   Sun Mar 3 19:47:05 2024 +0100

    refactor: removed empty line

commit 6fef312
Author: Linus Wallin <linuswallin@live.se>
Date:   Sun Mar 3 19:45:34 2024 +0100

    refactor: removed empty lines

commit 1287b2c
Author: Linus Wallin <linuswallin@live.se>
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 69ca9c0
Author: Linus Wallin <linuswallin@live.se>
Date:   Sun Mar 3 19:30:38 2024 +0100

    fix: solves bug which resulted in wrong geohashes

commit 2a8d131
Author: Linus Wallin <linuswallin@live.se>
Date:   Sun Mar 3 19:24:32 2024 +0100

    test: updated tests to match the changes of the function in last commit

commit bb45871
Author: Linus Wallin <linuswallin@live.se>
Date:   Sun Mar 3 19:24:02 2024 +0100

    refactor: made the return string array dynamic

commit 708542e
Author: Linus Wallin <linuswallin@live.se>
Date:   Sun Mar 3 18:42:15 2024 +0100

    refactor: removes indentation error caused by merge

commit a216691
Merge: 4420765 62f7d5f
Author: Linus Wallin <linuswallin@live.se>
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 4420765
Author: Linus Wallin <linuswallin@live.se>
Date:   Sun Mar 3 18:37:30 2024 +0100

    test: added test cases for coveringGeohash function

commit 2c31979
Author: Linus Wallin <linuswallin@live.se>
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 e0b04d3
Author: Linus Wallin <linuswallin@live.se>
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.
  • Loading branch information
WenJJ2000 committed Mar 4, 2024
1 parent 9b64610 commit 9aaaf4c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
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]);
}
}

0 comments on commit 9aaaf4c

Please sign in to comment.