Compatibility
- v1.2.0, v2.0.0-alpha.3, and main5.35.25.15.04.2
- v1.2.0, v2.0.0-alpha.3, and mainiOSmacOS(Intel)macOS(ARM)LinuxtvOSwatchOS
A Swift language port of Turf.js.
A spatial analysis library written in Swift for native iOS, macOS, tvOS, watchOS, and Linux applications, ported from Turf.js.
Turf for Swift is experimental and its public API is subject to change. Please use with care and open issues for any problems you see or missing features that should be added.
Turf requires Xcode 9.x and supports the following minimum deployment targets:
Alternatively, you can incorporate Turf into a command line tool without Xcode on any platform that Swift supports, including Linux.
If your project is written in Objective-C, you’ll need to write a compatibility layer between turf-swift and your Objective-C code. If your project is written in Objective-C++, you may be able to use spatial-algorithms as an alternative to Turf.
Although a stable release of this library is not yet available, prereleases are available for installation using any of the popular Swift dependency managers.
To install Turf using CocoaPods:
pod 'Turf', '~> 1.2'
pod repo update
if you haven’t lately.pod install
and open the resulting Xcode workspace.import Turf
to any Swift file in your application target.To install Turf using Carthage:
github "mapbox/turf-swift" ~> 1.2
carthage bootstrap
.import Turf
to any Swift file in your application target.To install Turf using the Swift Package Manager, add the following package to the dependencies
in your Package.swift file:
.package(url: "https://github.com/mapbox/turf-swift.git", from: "1.2.0")
Then import Turf
in any Swift file in your module.
This work-in-progress port of Turf.js contains the following functionality:
Turf.js | Turf-swift |
---|---|
turf-along | LineString.coordinateFromStart(distance:) |
turf-area | Polygon.area |
turf-bearing | CLLocationCoordinate2D.direction(to:) LocationCoordinate2D.direction(to:) on LinuxRadianCoordinate2D.direction(to:) |
turf-bezier-spline | LineString.bezier(resolution:sharpness:) |
turf-boolean-point-in-polygon | Polygon.contains(_:ignoreBoundary:) |
turf-circle | Polygon(center:radius:vertices:) |
turf-destination | CLLocationCoordinate2D.coordinate(at:facing:) LocationCoordinate2D.coordinate(at:facing:) on LinuxRadianCoordinate2D.coordinate(at:facing:) |
turf-distance | CLLocationCoordinate2D.distance(to:) LocationCoordinate2D.distance(to:) on LinuxRadianCoordinate2D.distance(to:) |
turf-helpers#polygon | Polygon(_:) |
turf-helpers#lineString | LineString(_:) |
turf-helpers#degreesToRadians | CLLocationDegrees.toRadians() LocationDegrees.toRadians() on Linux |
turf-helpers#radiansToDegrees | CLLocationDegrees.toDegrees() LocationDegrees.toDegrees() on Linux |
turf-helpers#convertLengthturf-helpers#convertArea | Measurement.converted(to:) |
turf-length | LineString.distance(from:to:) |
turf-line-intersect | intersection(_:_:) |
turf-line-slice | LineString.sliced(from:to:) |
turf-line-slice-along | LineString.trimmed(from:distance:) |
turf-midpoint | mid(_:_:) |
turf-nearest-point-on-line | LineString.closestCoordinate(to:) |
turf-polygon-to-line | LineString(_:) MultiLineString(_:) FeatureCollection(_:) |
turf-simplify | LineString.simplified(tolerance:highestQuality:) |
— | CLLocationDirection.difference(from:) LocationDirection.difference(from:) on Linux |
— | CLLocationDirection.wrap(min:max:) LocationDirection.wrap(min:max:) on Linux |
turf-swift also contains an experimental GeoJSON encoder/decoder with support for Codable.
// Decode unknown GeoJSON type
let geojson = try! GeoJSON.parse(data)
// Decode known GeoJSON type
let geojson = try! GeoJSON.parse(FeatureCollection.self, from: data)
// Initialize a PointFeature and encode 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 json = String(data: data, encoding: .utf8)
print(json)
/*
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
1,
0
]
}
}
*/