Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kiliankoe/GeoJSON
🌍 Swift types for working with GeoJSON data
https://github.com/kiliankoe/GeoJSON
geojson
Last synced: about 2 months ago
JSON representation
🌍 Swift types for working with GeoJSON data
- Host: GitHub
- URL: https://github.com/kiliankoe/GeoJSON
- Owner: kiliankoe
- License: mit
- Created: 2020-07-08T19:49:47.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-12-03T16:33:00.000Z (10 months ago)
- Last Synced: 2024-07-22T08:42:04.754Z (2 months ago)
- Topics: geojson
- Language: Swift
- Homepage: https://swiftpackageindex.com/kiliankoe/GeoJSON/documentation
- Size: 107 KB
- Stars: 43
- Watchers: 5
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🌍 GeoJSON
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fkiliankoe%2FGeoJSON%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/kiliankoe/GeoJSON)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fkiliankoe%2FGeoJSON%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/kiliankoe/GeoJSON)This is a Swift package for working with [GeoJSON](https://geojson.org) data. It contains necessary types conforming to `Codable` for easy de-/encoding of data.
The implementation of this package aims to follow the specification defined in [RFC 7946](https://tools.ietf.org/html/rfc7946).
## Usage
You can give this package a quick spin by cloning the repository and running `swift run --repl` inside it.
### Encoding
Create a `FeatureCollection` or just a single `Feature` to create some GeoJSON data.
```swift
let features = FeatureCollection(features: [
Feature(
geometry: .point(Point(longitude: 125.6, latitude: 10.1)),
properties: [
"name": "Dinagat Island"
]
)
])let json = try JSONEncoder().encode(features)
```### Decoding
It works the same way in the other direction. Use a standard `JSONDecoder` to decode GeoJSON data into a fitting type.
```swift
let features = try JSONDecoder().decode(FeatureCollection.self, from: json)
```If you don't know if the data to decode is a `Feature` or `FeatureCollection`, decode a value of type `GeoJSONDocument`, which handles
both.```swift
let document = try JSONDecoder().decode(GeoJSONDocument.self, from: json)
switch document {
case .feature(let feature):
// ...
case .featureCollection(let featureCollection):
// ...
}
```## Installation
This package is available via Swift Package Manager, add its clone URL to your project to get started.