Turf

A Swift language port of Turf.js.

Showing:

Popularity

Downloads/wk

0

GitHub Stars

141

Maintenance

Last Commit

2mos ago

Contributors

28

Package

Dependencies

0

License

ISC

Categories

Readme

Turf for Swift

📱iOS     🖥💻macOS     📺tvOS     ⌚️watchOS     Linux     codecov 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.

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.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" ~> 1.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", from: "1.2.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-swift
turf-alongLineString.coordinateFromStart(distance:)
turf-areaPolygon.area
turf-bearingCLLocationCoordinate2D.direction(to:)
LocationCoordinate2D.direction(to:) on Linux
RadianCoordinate2D.direction(to:)
turf-bezier-splineLineString.bezier(resolution:sharpness:)
turf-boolean-point-in-polygonPolygon.contains(_:ignoreBoundary:)
turf-centerPolygon.center
turf-center-of-massPolygon.centerOfMass
turf-centroidPolygon.centroid
turf-circlePolygon(center:radius:vertices:)
turf-destinationCLLocationCoordinate2D.coordinate(at:facing:)
LocationCoordinate2D.coordinate(at:facing:) on Linux
RadianCoordinate2D.coordinate(at:facing:)
turf-distanceCLLocationCoordinate2D.distance(to:)
LocationCoordinate2D.distance(to:) on Linux
RadianCoordinate2D.distance(to:)
turf-helpers#polygonPolygon(_:)
turf-helpers#lineStringLineString(_:)
turf-helpers#degreesToRadiansCLLocationDegrees.toRadians()
LocationDegrees.toRadians() on Linux
turf-helpers#radiansToDegreesCLLocationDegrees.toDegrees()
LocationDegrees.toDegrees() on Linux
turf-helpers#convertLength
turf-helpers#convertArea
Measurement.converted(to:)
turf-lengthLineString.distance(from:to:)
turf-line-intersectintersection(_:_:)
turf-line-sliceLineString.sliced(from:to:)
turf-line-slice-alongLineString.trimmed(from:distance:)
turf-midpointmid(_:_:)
turf-nearest-point-on-lineLineString.closestCoordinate(to:)
turf-polygon-to-lineLineString(_:)
MultiLineString(_:)
FeatureCollection(_:)
turf-simplifyLineString.simplified(tolerance:highestQuality:)
turf-polygon-smoothPolygon.smooth(iterations:)
turf-simplifyPolygon.simplified(tolerance:highestQuality:)
CLLocationDirection.difference(from:)
LocationDirection.difference(from:) on Linux
CLLocationDirection.wrap(min:max:)
LocationDirection.wrap(min:max:) on Linux

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100