From 80aa9326d9926285e13b544c7d9ebd6730e01012 Mon Sep 17 00:00:00 2001 From: wenjj2000 Date: Mon, 4 Mar 2024 00:33:36 +0100 Subject: [PATCH] refactor : changing helper functions to private instead of public and removing their tests --- .../java/com/esri/core/geometry/Geohash.java | 25 +++-- .../com/esri/core/geometry/TestGeohash.java | 94 +++++++------------ 2 files changed, 48 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/esri/core/geometry/Geohash.java b/src/main/java/com/esri/core/geometry/Geohash.java index 739fda5e..6c0001bd 100644 --- a/src/main/java/com/esri/core/geometry/Geohash.java +++ b/src/main/java/com/esri/core/geometry/Geohash.java @@ -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); @@ -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 @@ -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); } /** diff --git a/src/test/java/com/esri/core/geometry/TestGeohash.java b/src/test/java/com/esri/core/geometry/TestGeohash.java index 0788de1b..13ddd866 100644 --- a/src/test/java/com/esri/core/geometry/TestGeohash.java +++ b/src/test/java/com/esri/core/geometry/TestGeohash.java @@ -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)); + } }