Skip to content

Commit

Permalink
refactor : changing helper functions to private instead of public and…
Browse files Browse the repository at this point in the history
… removing their tests
  • Loading branch information
WenJJ2000 committed Mar 3, 2024
1 parent 62f7d5f commit 80aa932
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 71 deletions.
25 changes: 12 additions & 13 deletions src/main/java/com/esri/core/geometry/Geohash.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static String toGeohash(Point2D pt, int characterLength) {
* @return base32 string of the binStr in chunks of 5 binary digits
*/

public static String binaryToBase32(String binStr) {
private static String binaryToBase32(String binStr) {
StringBuilder base32Str = new StringBuilder();
for (int i = 0; i < binStr.length(); i += 5) {
String chunk = binStr.substring(i, i + 5);
Expand All @@ -142,7 +142,7 @@ public static String binaryToBase32(String binStr) {
* @return A binary string representation of the value with the given range and precision
*/

public static String convertToBinary(
private static String convertToBinary(
double value,
double[] r,
int precision
Expand Down Expand Up @@ -179,27 +179,26 @@ public static String containingGeohash(Envelope2D envelope) {
double deltaLon = 360;
double deltaLat = 180;

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

if(chars%2 == 0){
while (xmin == xmax && ymin == ymax && chars < 25) {
if (chars % 2 == 0) {
deltaLon = deltaLon / 8;
deltaLat = deltaLat / 4;
}else{
} 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);
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);
if (chars == 1) return "";

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

/**
Expand Down
94 changes: 36 additions & 58 deletions src/test/java/com/esri/core/geometry/TestGeohash.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,78 +72,56 @@ public void testGeohashToEnvelopeGoodDimensions2() {
assertEquals(latDiff, env.ymax - env.ymin, delta);
}

/**
* Check if BinaryToBase32 work as intended with a normal binary string
*/
@Test
public void testBinaryToBase32() {
String testStr = "011011111111011";
assertEquals("ezv", Geohash.binaryToBase32(testStr));
}

@Test
public void testConvertToBinary() {
double lat = 40.7128;
double lon = -74.0060;
String latStr = Geohash.convertToBinary(lat, new double[] { -90, 90 }, 10);
String lonStr = Geohash.convertToBinary(
lon,
new double[] { -180, 180 },
10
);

assertEquals("1011100111", latStr);
assertEquals("0100101101", lonStr);
}

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

int chrLen = 5;

String p1Hash = Geohash.toGeohash(p1, chrLen);
String p2Hash = Geohash.toGeohash(p2, chrLen);
String p3Hash = Geohash.toGeohash(p3, chrLen);
String p4Hash = Geohash.toGeohash(p4, chrLen);

assertEquals("gbsuv", p1Hash);
assertEquals("gk6ru", p2Hash);
assertEquals("sqdnk", p3Hash);
assertEquals("bb9su", p4Hash);
}

@Test
public void testToGeohashHasGoodPrecision() {
Point2D point = new Point2D(18.068581, 59.329323);
assertEquals(6, Geohash.toGeohash(point, 6).length());
}
@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 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));
}
}

0 comments on commit 80aa932

Please sign in to comment.