Swiftpack.co - Package - mapbox/turf-swift
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

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 requires Xcode 9.x and supports the following minimum deployment targets:

  • iOS 10.0 and above
  • macOS 10.12 (Sierra) and above
  • tvOS 10.0 and above
  • watchOS 3.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', '~> 1.1'
    
  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" ~> 1.1
    
  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: "1.1.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.js Turf-swift
turf-along LineString.coordinateFromStart(distance:)
turf-area Polygon.area
turf-bearing CLLocationCoordinate2D.direction(to:)
RadianCoordinate2D.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:)
RadianCoordinate2D.coordinate(at:facing:)
turf-distance CLLocationCoordinate2D.distance(to:)
RadianCoordinate2D.distance(to:)
turf-helpers#polygon Polygon(_:)
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 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:)
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)

// 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
    ]
  }
}
*/

Github

link
Stars: 116

Releases

v2.0.0-alpha.1 - 2021-01-19T20:06:21

This release is a wrapper that will serve v1.1.0 of Turf-Swift as a binary. This consumption is needed so that Swift Package Manager can use Turf Swift as a dynamic dependency.

v1.2.0-rc.1 - 2021-01-14T23:28:58

Changes since v1.2.0-beta.1:

  • Application extensions can now link to Turf. (#126)

v1.2.0-beta.1 - 2020-12-12T00:08:59

Changes since v1.1.0:

  • Added the Polygon(center:radius:vertices:) initializer for creating a regular polygon that approximates a circle. (#123)
  • Added the LineString.simplified(tolerance:highestQuality:) and LineString.simplify(tolerance:highestQuality:) methods that apply Ramer–Douglas–Peucker simplification. (#124)

v1.1.0 - 2020-11-06T02:39:53

Changes since v1.0.0:

  • Added CLLocationCoordinate2D.latitude and CLLocationCoordinate2D.longitude properties and a CLLocationCoordinate2D(latitude:longitude:) initializer on Linux for consistency with Core Location on Apple platforms. (#120)

v1.0.0 - 2020-09-24T22:17:02

Changes since v0.5.0:

  • Turf now requires iOS 10.0 or above, macOS 10.12.0 or above, tvOS 10.0 or above, or watchOS 3.0 or above to run. (#112)
  • Renamed Turf.intersection(_:_:) to intersection(_:_:), working around a compiler error. (#115)
  • Renamed GeoJSON.decodedFeatueCollection to GeoJSON.decodedFeatureCollection. (#111)

v0.5.0 - 2020-06-11T15:12:06

Changes since v0.4.0:

GeoJSON Representations

  • Exposed all geometries structs coordinates editable (#105)
  • Added convenience initializers for Polygon and MultiPolygon geometries, as well as MultiPolygon.contains(_:ignoreBoundary:) method (#106)

Bugfixes

  • Fixed BoundingBox to store properties in accordance to GeoJSON RFC7946 (#104)
  • Fixed LineString.closesCoordinate(to:) returned IndexedCoordinate to have correct distance value (#107)

v0.4.0 - 2020-06-05T08:09:08

Changes since v0.3.0:

Packaging

  • Migrated to Swift 5. (#83)

GeoJSON representation

  • Replaced the Geometry struct with an enumeration by the same name that has an associated value for each geometry type. (#93)
  • Removed the FeatureVariant enumeration and the series of geometry-specific feature structs such as PointFeature and LineStringFeature in favor of a single Feature struct. For example, instead of creating a PointFeature instance, create a Feature instance and set its Feature.geometry property to a Point instance. (#93)
  • Added the GeometryCollection struct to represent GeoJSON geometry collections. (#93)
  • Removed the JSONType protocol and AnyJSONType struct in favor of making [String: Any?] conform to the Codable protocol. (#95)
  • This library no longer provides Codable conformance for CLLocationCoordinate2D structs, since the appropriate precision may vary from one use case to another. (#96)
  • Removed Polyline as a type alias for LineString. (#93)
  • Added initializers for converting a MultiPolygon to a GeometryCollection, a Ring to a LineString, and a Polygon to a MultiLineString. (#99)

Spatial operations

  • Added the LineString.bezier(resolution:sharpness:) method for calculating a Bézier spline. (#73)
  • Added the mid(_:_:) function for calculating the point midway between two coordinates, taking the curvature of the Earth into account. (#75)
  • Fixed an issue where LineString.sliced(from:to:) duplicated the last coordinate if to is omitted or equal to the end of the line string. (#79)
  • Fixed an issue where Polygon.contains(_:ignoreBoundary:) returned false for coordinates on a boundary coinciding with the bounding box. (#90)
  • Fixed an issue where the Polygon.contains(_:ignoreBoundary:) misinterpreted the ignoreBoundary argument to result in the opposite behavior. (#90)

v0.3.0 - 2018-12-20T01:19:54

Changes since v0.2.1:

  • Fixed an issue where the index returned by LineString.closestCoordinate(to:) was rounded up instead of down. This in turn fixes unstable return values of LineString.sliced(from:to:) and LineString.distance(from:to:). (#74)

v0.2.1 - 2018-09-26T11:35:50

  • Exposed GeoJSON geometries and feature initializers. #57 #69
  • Ported the turf-boolean-point-in-polygon function and added a BoundingBox. #64 #66
  • Added support for codable null values #63
  • GeoJSONObject.properties is now optionally coded #63

v0.2.0 - 2018-06-21T21:24:39

  • Adds support for decoding GeoJSON. #45
  • Adds support for Xcode 10. #53

v0.1.1 - 2018-04-05T14:58:16

  • Removed Swift 4.1 warning. #48

v0.1.0 - 2018-01-23T19:57:18

Packaging

  • Added support for installing this library using Swift Package Manager. (#30)
  • Added support for deploying on iOS 8.x after installing the library using Carthage. (#32)
  • Added support for deploying on macOS 10.10.x after installing the library using CocoaPods. (#32)
  • Added support for watchOS and tvOS. (#32)
  • Added support for Linux. (#30)

Other changes

  • Renamed CLLocationDirection.differenceBetween(_:) to CLLocationDirection.difference(from:). (#39)
  • Added a Polygon struct that can calculate its own area. (#21)

v0.0.4 - 2017-10-30T15:48:01

  • Exposes toDegrees() and toRadians() methods.

v0.0.3 - 2017-09-29T09:24:26

  • Expose haversine distance calculation method

v0.0.2 - 2017-09-25T19:48:29

  • Polyline related functionality is now encapsulated in a Polyline struct
  • Turf-swift now builds on macOS 💻 🖥 🎉

v0.0.1 - 2017-08-09T22:07:05