Home

Awesome

Turf for Swift

📱iOS     🖥💻macOS     📺tvOS     ⌚️watchOS     <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/TuxFlat.svg" width="20" alt="Linux">     Documentation     Carthage compatible     CocoaPods     SPM compatible    

A spatial analysis library written in Swift for native iOS, macOS, tvOS, watchOS, visionOS, and Linux applications, ported from Turf.js.

Requirements

Turf requires Xcode 14.1 or above 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.

Installation

Releases are available for installation using any of the popular Swift dependency managers.

CocoaPods

To install Turf using CocoaPods:

  1. Specify the following dependency in your Podfile:
    pod 'Turf', '~> 2.8'
    
  2. Run pod repo update if you haven’t lately.
  3. Run pod install and open the resulting Xcode workspace.
  4. Add import Turf to any Swift file in your application target.

Carthage

To install Turf using Carthage:

  1. Add the following dependency to your Cartfile:
    github "mapbox/turf-swift" ~> 2.8
    
  2. Run carthage bootstrap.
  3. Follow the rest of Carthage’s integration instructions. Your application target’s Embedded Frameworks should include Turf.framework.
  4. Add import Turf to any Swift file in your application target.

Swift Package Manager

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: "2.8.0")

Then import Turf in any Swift file in your module.

Available functionality

This work-in-progress port of Turf.js contains the following functionality:

Turf.jsTurf for Swift
turf-along#alongLineString.coordinateFromStart(distance:)
turf-area#areaPolygon.area
turf-bearing#bearingCLLocationCoordinate2D.direction(to:)<br>LocationCoordinate2D.direction(to:) on Linux<br>RadianCoordinate2D.direction(to:)
turf-bezier-spline#bezierSplineLineString.bezier(resolution:sharpness:)
turf-boolean-point-in-polygon#booleanPointInPolygonPolygon.contains(_:ignoreBoundary:)
turf-center#centerPolygon.center
turf-center-of-mass#centerOfMassPolygon.centerOfMass
turf-centroid#centroidPolygon.centroid
turf-circle#circlePolygon(center:radius:vertices:)
turf-destination#destinationCLLocationCoordinate2D.coordinate(at:facing:)<br>LocationCoordinate2D.coordinate(at:facing:) on Linux<br>RadianCoordinate2D.coordinate(at:facing:)
turf-distance#distanceCLLocationCoordinate2D.distance(to:)<br>LocationCoordinate2D.distance(to:) on Linux<br>RadianCoordinate2D.distance(to:)
turf-helpers#polygonPolygon(_:)
turf-helpers#lineStringLineString(_:)
turf-helpers#degreesToRadiansCLLocationDegrees.toRadians()<br>LocationDegrees.toRadians() on Linux
turf-helpers#radiansToDegreesCLLocationDegrees.toDegrees()<br>LocationDegrees.toDegrees() on Linux
turf-helpers#convertLength<br>turf-helpers#convertAreaMeasurement.converted(to:)
turf-length#lengthLineString.distance(from:to:)
turf-line-intersect#lineIntersectLineString.intersections(with:)
turf-line-slice#lineSliceLineString.sliced(from:to:)
turf-line-slice-along#lineSliceAlongLineString.trimmed(from:to:)
turf-midpoint#midpointmid(_:_:)
turf-nearest-point-on-line#nearestPointOnLineLineString.closestCoordinate(to:)
turf-polygon-to-line#polygonToLineLineString(_:)<br>MultiLineString(_:)
turf-simplify#simplifyLineString.simplify(tolerance:highestQuality:)<br>LineString.simplified(tolerance:highestQuality:)
turf-polygon-smooth#polygonSmoothPolygon.smooth(iterations:)
—CLLocationDirection.difference(from:)<br>LocationDirection.difference(from:) on Linux
—CLLocationDirection.wrap(min:max:)<br>LocationDirection.wrap(min:max:) on Linux

GeoJSON

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

// 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 a known GeoJSON object.
let featureCollection = try JSONDecoder().decode(FeatureCollection.self, from: data)

// 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 json = String(data: data, encoding: .utf8)
print(json)

/*
{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [
      1,
      0
    ]
  }
}
*/

Well Known Text (WKT)

turf-swift contains a minimal WKT encoding/decoding support for geometries implementing WKTCodable protocol.

let wktString = "POINT(123.53 -12.12)"

// Decoding is done using an init method
let point = try? Point(wkt: wktString)
let geometry = try? Geometry(wkt: wktString)

print(point?.coordinates)

// ...

// Geometries then can be serialized using a property getter
let serializedWKTString = point?.wkt
print(serializedWKTString)