Skip to content

Commit

Permalink
modify getBounds method
Browse files Browse the repository at this point in the history
  • Loading branch information
fummicc1 committed Nov 16, 2024
1 parent 566aa9b commit b95d735
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 49 deletions.
74 changes: 26 additions & 48 deletions Sources/GeoHashFramework/GeoHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/GeoHashFrameworkTests/GeoHashTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b95d735

Please sign in to comment.