Skip to content

Commit

Permalink
Updated GeoJSON example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 authored Sep 28, 2021
1 parent 3f3fb5b commit 8aaabff
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,24 @@ Turf.js | Turf-swift

## GeoJSON

turf-swift also contains an experimental GeoJSON encoder/decoder with support for Codable.
turf-swift also contains a GeoJSON encoder/decoder with support for Codable.

```swift
// Decode unknown GeoJSON type
let geojson = try! GeoJSON.parse(data)
// Decode an unknown GeoJSON object.
let geojson = try JSONDecoder().decode(GeoJSONObject.self, from: data)
guard case let .feature(feature) = geojson,
case let .point(point) = feature.geometry else {
return
}
// Decode known GeoJSON type
let geojson = try! GeoJSON.parse(FeatureCollection.self, from: data)
// Decode a known GeoJSON object.
let featureCollection = try JSONDecoder().decode(FeatureCollection.self, from: data)
// Initialize a PointFeature and encode as GeoJSON
// Initialize a Point feature and encode it as GeoJSON.
let coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 1)
let point = Point(coordinate)
let pointFeature = Feature(geometry: .point(point))
let data = try! JSONEncoder().encode(pointFeature)
let data = try JSONEncoder().encode(pointFeature)
let json = String(data: data, encoding: .utf8)
print(json)
Expand Down

0 comments on commit 8aaabff

Please sign in to comment.