sfera
sfera models RFC 7946 GeoJSON as Swift value types. The rules of the format live in the types:
- Coordinates are validated the moment a
Positionis created. - A line string needs two positions, and a ring three vertices. The compiler checks both.
- Rings close themselves, and polygons follow the right-hand rule automatically.
- Large integer identifiers and properties survive a round trip exactly.
Every type is Codable, Sendable and Hashable. sfera has no third-party dependencies and uses only the Swift standard library.
Quick start Link to heading
import Foundation
import sfera
let rome = try Position(latitude: 41.9028, longitude: 12.4964)
let city = Feature(id: "rome", geometry: .point(rome), properties: ["name": "Rome"])
let data = try JSONEncoder().encode(FeatureCollection([city]))
// {"type":"FeatureCollection","features":[{"type":"Feature","id":"rome","geometry":{"type":"Point","coordinates":[12.4964,41.9028]},"properties":{"name":"Rome"}}]}
let document = try JSONDecoder().decode(GeoJSON.self, from: data)
Installation Link to heading
sfera requires Swift 6.4. It runs on iOS, macOS, watchOS and tvOS 26, on Linux, and on WebAssembly (WASI). Add it with Swift Package Manager:
dependencies: [
.package(url: "https://github.com/csanfilippo/sfera.git", from: "0.1.0")
]
Learn more Link to heading
- The full guide, covering geometries, features, decoding rules and output, is on GitHub