Skip to content

Commit

Permalink
Merge branch '213-geometry-to-geohash' of github.com:DD2480-Group-3/g…
Browse files Browse the repository at this point in the history
…eometry-api-java into 54-coveringGeohash
  • Loading branch information
LinusWallin committed Mar 3, 2024
2 parents 4420765 + 62f7d5f commit a216691
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
38 changes: 35 additions & 3 deletions src/main/java/com/esri/core/geometry/Geohash.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public static String toGeohash(Point2D pt, int characterLength) {
);
}
int precision = characterLength * 5;
double lat = pt.x;
double lon = pt.y;
double lat = pt.y;
double lon = pt.x;

String latBitStr = Geohash.convertToBinary(
lat,
Expand Down Expand Up @@ -167,7 +167,39 @@ public static String convertToBinary(
* @return the geohash as a string
*/
public static String containingGeohash(Envelope2D envelope) {
return "";
double posMinX = envelope.xmin + 180;
double posMaxX = envelope.xmax + 180;
double posMinY = envelope.ymin + 90;
double posMaxY = envelope.ymax + 90;
int chars = 0;
double xmin = 0;
double xmax = 0;
double ymin = 0;
double ymax = 0;
double deltaLon = 360;
double deltaLat = 180;

while(xmin == xmax && ymin == ymax && chars < 25){

if(chars%2 == 0){
deltaLon = deltaLon / 8;
deltaLat = deltaLat / 4;
}else{
deltaLon = deltaLon / 4;
deltaLat = deltaLat / 8;
}

xmin = Math.floor(posMinX/deltaLon);
xmax = Math.floor(posMaxX/deltaLon);
ymin = Math.floor(posMinY/deltaLat);
ymax = Math.floor(posMaxY/deltaLat);

chars++;
}

if(chars == 1) return "";

return toGeohash(new Point2D(envelope.xmin, envelope.ymin), chars-1);
}

/**
Expand Down
40 changes: 37 additions & 3 deletions src/test/java/com/esri/core/geometry/TestGeohash.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public void testConvertToBinary() {

@Test
public void testToGeoHash() {
Point2D p1 = new Point2D(48.669, -4.329);
Point2D p2 = new Point2D(70.273, -30.382);
Point2D p3 = new Point2D(37.691, 14.276);
Point2D p1 = new Point2D(-4.329,48.669);
Point2D p2 = new Point2D(-30.382, 70.273);
Point2D p3 = new Point2D(14.276, 37.691);

int chrLen = 5;

Expand All @@ -112,6 +112,40 @@ public void testToGeoHash() {
assertEquals("gk6ru", p2Hash);
assertEquals("sqdnk", p3Hash);
}
@Test
public void testToGeohashHasGoodPrecision(){
Point2D point = new Point2D(18.068581, 59.329323);
assertEquals(6, Geohash.toGeohash(point, 6).length());
}

@Test
public void testToGeohash2(){
String expected = "u6sce";
Point2D point = new Point2D(18.068581, 59.329323);
String geoHash = Geohash.toGeohash(point, 5);

assertEquals(expected, geoHash);
}

@Test
public void testContainingGeohashWithHugeValues(){
Envelope2D envelope = new Envelope2D(-179, -89, 179, 89);
assertEquals("", Geohash.containingGeohash(envelope));
}

@Test
public void testContainingGeohash(){
Envelope2D envelope = new Envelope2D(-179, -89, -140, -50);
assertEquals("0", Geohash.containingGeohash(envelope));
}

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



@Test
public void testCoveringGeohashEmptyEnvelope() {
Expand Down

0 comments on commit a216691

Please sign in to comment.