From b95d735062ff75462bb4d267867ca84feae302a1 Mon Sep 17 00:00:00 2001 From: fummicc1 Date: Sun, 17 Nov 2024 01:05:32 +0900 Subject: [PATCH] modify getBounds method --- Sources/GeoHashFramework/GeoHash.swift | 74 +++++++------------ .../GeoHashFrameworkTests/GeoHashTests.swift | 2 +- 2 files changed, 27 insertions(+), 49 deletions(-) diff --git a/Sources/GeoHashFramework/GeoHash.swift b/Sources/GeoHashFramework/GeoHash.swift index fb53655..e92ca47 100644 --- a/Sources/GeoHashFramework/GeoHash.swift +++ b/Sources/GeoHashFramework/GeoHash.swift @@ -218,7 +218,7 @@ extension GeoHash { let latitudeBits = precision.rawValue / 2 let longitudeBits = (precision.rawValue + 1) / 2 - + let latitudeRange = 180.0 // 90 - (-90) let latitudeDelta = latitudeRange / pow(2.0, Double(latitudeBits)) @@ -251,58 +251,36 @@ extension GeoHash { public static func getBounds(with precision: GeoHashBitsPrecision) -> [[GeoHashCoordinate2D]] { let bound = getBound(with: precision) - var topLeft = bound[0] - - let latitudeDelta = topLeft.latitude - bound[3].latitude - let longitudeDelta = bound[1].longitude - topLeft.longitude + let latitudeDelta = bound[0].latitude - bound[3].latitude + let longitudeDelta = bound[1].longitude - bound[0].longitude var ret: [[GeoHashCoordinate2D]] = [] - - // scan by sliding window - while true { - // only contains top-left point - var row: [GeoHashCoordinate2D] = [] - - while topLeft.longitude + longitudeDelta <= 180.0 { - row.append(topLeft) - - topLeft = GeoHashCoordinate2D( - latitude: topLeft.latitude, - longitude: topLeft.longitude + longitudeDelta - ) + var currentLatitude = 90.0 // Start from the top + + // Scan from top to bottom + while currentLatitude > -90.0 { + var currentLongitude = -180.0 + + // Scan each row from left to right + while currentLongitude < 180.0 { + let rectangle = [ + GeoHashCoordinate2D(latitude: currentLatitude, longitude: currentLongitude), + GeoHashCoordinate2D( + latitude: currentLatitude, longitude: currentLongitude + longitudeDelta), + GeoHashCoordinate2D( + latitude: currentLatitude - latitudeDelta, + longitude: currentLongitude + longitudeDelta), + GeoHashCoordinate2D( + latitude: currentLatitude - latitudeDelta, longitude: currentLongitude), + ] + ret.append(rectangle) + + currentLongitude += longitudeDelta } - // Add current row to result - // Append all rectangle points - ret.append(contentsOf: - row.map { topLeft in - [ - topLeft, - GeoHashCoordinate2D( - latitude: topLeft.latitude, - longitude: topLeft.longitude + longitudeDelta - ), - GeoHashCoordinate2D( - latitude: topLeft.latitude - latitudeDelta, - longitude: topLeft.longitude + longitudeDelta - ), - GeoHashCoordinate2D( - latitude: topLeft.latitude - latitudeDelta, - longitude: topLeft.longitude - ), - ] - } - ) - - // Move to next row - topLeft = GeoHashCoordinate2D( - latitude: topLeft.latitude - latitudeDelta, - longitude: -180.0 - ) - if topLeft.latitude + latitudeDelta < -90.0 { - break - } + currentLatitude -= latitudeDelta } + return ret } diff --git a/Tests/GeoHashFrameworkTests/GeoHashTests.swift b/Tests/GeoHashFrameworkTests/GeoHashTests.swift index 9b396a4..a2037a0 100644 --- a/Tests/GeoHashFrameworkTests/GeoHashTests.swift +++ b/Tests/GeoHashFrameworkTests/GeoHashTests.swift @@ -54,7 +54,7 @@ struct GeoHashTests { } @Test - func getBounds() async throws { + func getBound() async throws { let actual = GeoHash.getBound(with: .exact(digits: 0)) let expected = [ GeoHashCoordinate2D(