A Swift language port of Turf.js.
Clone or download
Latest commit e5471bc Sep 25, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Sources/Turf Bump version Sep 26, 2018
Tests Bump version Sep 26, 2018
Turf.xcodeproj Added GeoJSON feature initializers Sep 25, 2018
.gitignore Initial commit Jul 11, 2017
.swift-version Upgrade to Swift 4 Nov 27, 2017
.travis.yml Bump Swift version on Linux May 14, 2018
LICENSE.md Various fixes for installing through CocoaPods Jul 13, 2017
Package.swift Linux support Jan 17, 2018
README.md Bump version Sep 26, 2018
Turf.podspec Bump version Sep 26, 2018

README.md

Turf for Swift

📱iOS     🖥💻macOS     📺tvOS     ⌚️watchOS     Linux
Carthage compatible     CocoaPods     SPM compatible    

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.

Requirements

Turf is written in Swift 4. It requires Xcode 9.x and supports the following minimum deployment targets:

  • iOS 8.0 and above
  • macOS 10.10.0 (Yosemite) and above
  • tvOS 9.0 and above
  • watchOS 2.0 and above

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

Although a stable release of this library is not yet available, prereleases 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', '~> 0.2'
  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" ~> 0.2
    
  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", .upToNextMinor(from: "0.2"))

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.js Turf-swift
turf-along LineString.coordinateFromStart(distance:)
turf-area Polygon.area
turf-boolean-point-in-polygon Polygon.contains(point:ignoreBoundary:)
turf-destination CLLocationCoordinate2D.coordinate(at:facing:)
RadianCoordinate2D.coordinate(at:facing:)
turf-distance CLLocationCoordinate2D.distance(to:)
RadianCoordinate2D.distance(to:)
turf-helpers#polygon Polygon(outerRing:innerRings:)
turf-helpers#lineString LineString(_:)
turf-helpers#degreesToRadians CLLocationDegrees.toRadians()
turf-helpers#radiansToDegrees CLLocationDegrees.toDegrees()
turf-helpers#convertLength
turf-helpers#convertArea
Measurement.converted(to:)
turf-length LineString.distance(from:to:)
turf-line-intersect Turf.intersection(_:_:)
turf-line-slice LineString.sliced(from:to:)
turf-line-slice-along LineString.trimmed(from:distance:)
turf-nearest-point-on-line LineString.closestCoordinate(to:)
CLLocationCoordinate2D.direction(to:)
RadianCoordinate2D.direction(to:)
CLLocationDirection.difference(from:)
CLLocationDirection.wrap(min:max:)

GeoJSON

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

// Decode unknown GeoJSON type
let geojson = try! GeoJSON.parse(data: data)

// Decode known GeoJSON type
let geojson = try! GeoJSON.parse(data: data, as: FeatureCollection.self)

// Initialize a PointFeature and encode as GeoJSON
let coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 1)
let point = Point(coordinate)
let pointFeature = PointFeature(point)
let data = try! JSONEncoder().encode(pointFeature)
let json = String(data: data, encoding: .utf8)
print(json)

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