From 0a7034f194fe04576f8055bb9251b17d4f2042c0 Mon Sep 17 00:00:00 2001 From: timskap Date: Fri, 15 Mar 2024 13:42:05 +0400 Subject: [PATCH 1/5] Add coordinates for hex --- .../contents.xcworkspacedata | 7 ++ Sources/SwiftH3/H3Index.swift | 65 +++++++++++++++++-- 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Sources/SwiftH3/H3Index.swift b/Sources/SwiftH3/H3Index.swift index a741bb5..ea66d1d 100644 --- a/Sources/SwiftH3/H3Index.swift +++ b/Sources/SwiftH3/H3Index.swift @@ -7,6 +7,8 @@ public struct H3Index { private var value: UInt64 + // MARK: - Initializers + /** Initializes using a 64-bit integer @@ -35,13 +37,64 @@ public struct H3Index { - Parameter string: The string representing the hex value of the int */ public init(string: String) { - var value: UInt64 = 0 - string.withCString { ptr in - value = stringToH3(ptr) + let value = string.withCString { cString in + stringToH3(cString) + } + guard value != 0 else { + fatalError("Invalid H3 index string: \(string)") } self.value = value } +} +extension H3Index { + + /// Returns the cell boundary in spherical coordinates for an H3 index. + /// + /// - Returns: The boundary of the H3 cell in spherical coordinates. + public func boundary() -> [H3Coordinate] { + var gb = GeoBoundary() + h3ToGeoBoundary(value, &gb) + + var coordinates: [H3Coordinate] = [] + + switch gb.numVerts { + case 10: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.9.lat), lon: radsToDegs(gb.verts.9.lon))) + fallthrough + case 9: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.8.lat), lon: radsToDegs(gb.verts.8.lon))) + fallthrough + case 8: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.7.lat), lon: radsToDegs(gb.verts.7.lon))) + fallthrough + case 7: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.6.lat), lon: radsToDegs(gb.verts.6.lon))) + fallthrough + case 6: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.5.lat), lon: radsToDegs(gb.verts.5.lon))) + fallthrough + case 5: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.4.lat), lon: radsToDegs(gb.verts.4.lon))) + fallthrough + case 4: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.3.lat), lon: radsToDegs(gb.verts.3.lon))) + fallthrough + case 3: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.2.lat), lon: radsToDegs(gb.verts.2.lon))) + fallthrough + case 2: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.1.lat), lon: radsToDegs(gb.verts.1.lon))) + fallthrough + case 1: + coordinates.append(H3Coordinate(lat: radsToDegs(gb.verts.0.lat), lon: radsToDegs(gb.verts.0.lon))) + default: + break + } + + return coordinates + } + } // MARK: Properties @@ -80,10 +133,8 @@ extension H3Index { */ public func kRingIndices(ringK: Int32) -> [H3Index] { var indices = [UInt64](repeating: 0, count: Int(maxKringSize(ringK))) - indices.withUnsafeMutableBufferPointer { ptr in - kRing(value, ringK, ptr.baseAddress) - } - return indices.map { H3Index($0) } + kRing(value, ringK, &indices) + return indices.compactMap { $0 == 0 ? nil : H3Index($0) } } } From 8f16aaa30e3036a21e08b8a14e94f6938b6ae377 Mon Sep 17 00:00:00 2001 From: timskap Date: Fri, 15 Mar 2024 13:43:51 +0400 Subject: [PATCH 2/5] Update README.md Change package url --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f508836..f477814 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This is a simple library to interface with [H3](https://github.com/uber/h3), a h ## Installation 1. Add this to your `Package.swift` (or add via Xcode 11): - * `.package(url: "https://github.com/bdotdub/SwiftH3.git", from: "0.1.0")` + * `.package(url: "https://github.com/timskap/SwiftH3.git", from: "0.1.0")` ## Examples From 573aac030108e44f107629e418b06e7a933753b8 Mon Sep 17 00:00:00 2001 From: timskap Date: Fri, 15 Mar 2024 13:54:21 +0400 Subject: [PATCH 3/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f477814..f508836 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This is a simple library to interface with [H3](https://github.com/uber/h3), a h ## Installation 1. Add this to your `Package.swift` (or add via Xcode 11): - * `.package(url: "https://github.com/timskap/SwiftH3.git", from: "0.1.0")` + * `.package(url: "https://github.com/bdotdub/SwiftH3.git", from: "0.1.0")` ## Examples From ebb84176e075a3e519ba8836a4cc56ccbd587ef0 Mon Sep 17 00:00:00 2001 From: Benny Wong Date: Fri, 14 Mar 2025 23:44:02 -0400 Subject: [PATCH 4/5] Delete .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata --- .../xcode/package.xcworkspace/contents.xcworkspacedata | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata b/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index 919434a..0000000 --- a/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,7 +0,0 @@ - - - - - From 0c46bc66fb937707b24f8b293a6a125c89df849a Mon Sep 17 00:00:00 2001 From: Timur Abdrakhimov Date: Fri, 23 May 2025 17:29:43 +0500 Subject: [PATCH 5/5] Change strdup --- Sources/SwiftH3/H3Index.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/SwiftH3/H3Index.swift b/Sources/SwiftH3/H3Index.swift index ea66d1d..0c4f055 100644 --- a/Sources/SwiftH3/H3Index.swift +++ b/Sources/SwiftH3/H3Index.swift @@ -200,9 +200,11 @@ extension H3Index: CustomStringConvertible { /// String description of the index public var description: String { - let cString = strdup("") - h3ToString(value, cString, 17) - return String(cString: cString!) + var buffer = [CChar](repeating: 0, count: 17) + buffer.withUnsafeMutableBufferPointer { ptr in + h3ToString(value, ptr.baseAddress, 17) + } + return String(cString: buffer) } }