Skip to content

maparoni/GeoJSONKit-Turf

Repository files navigation

GeoJSONKit+Turf

Swift

This package provides various geospatial extensions for GeoJSONKit. It is a fork of turf-swift, which itself is a partial Swift-port of Turf.js.

Requirements

GeoJSONKitTurf requires Xcode 14.x and supports the following minimum deployment targets:

  • iOS 15 and above
  • macOS 12 and above
  • tvOS 15 and above
  • visionOS 1.0 and above
  • watchOS 8.0 and above

It's also compatible with Linux (and possibly other platforms), as long as you have Swift 5.7 (or above) installed.

Installation

Swift Package Manager

To install GeoJSONKitTurf using the Swift Package Manager, add the following package to the dependencies in your Package.swift file:

.package(name: "GeoJSONKitTurf", url: "https://github.com/maparoni/geojsonkit-turf", from: "0.3.0")

Then use:

import GeoJSONKitTurf

Available functionality

This is a partial port of Turf.js, which adds the following functionality to GeoJSONKit's GeoJSON:

Turf.js GeoJSONKit-Turf
turf-along GeoJSON.LineString.coordinateFromStart(distance:)
turf-area GeoJSON.Polygon.area
turf-bbox-clip GeoJSON.LineString.clipped(to:)
GeoJSON.Polygon.clipped(to:)
turf-bearing GeoJSON.Position.direction(to:)
RadianCoordinate2D.direction(to:)
turf-bezier-spline GeoJSON.LineString.bezier(resolution:sharpness:)
turf-boolean-point-in-polygon GeoJSON.Polygon.contains(_:)
turf-center GeoJSON.Geometry.center()
turf-center-of-mass GeoJSON.Geometry.centerOfMass()
turf-centroid GeoJSON.Geometry.centroid()
turf-circle GeoJSON.Polygon(center:radius:)
turf-convex GeoJSON.convexHull()
Collection<GeoJSON.Position>.convexHull()
turf-destination GeoJSON.Position.coordinate(at:facing:)
RadianCoordinate2D.coordinate(at:facing:)
turf-distance GeoJSON.Position.distance(to:)
RadianCoordinate2D.distance(to:)
turf-helpers#degreesToRadians GeoJSON.Degrees.toRadians()
turf-helpers#radiansToDegrees GeoJSON.DegreesRadians.toDegrees()
turf-helpers#convertLength
turf-helpers#convertArea
Measurement.converted(to:)
turf-length GeoJSON.LineString.distance(from:to:)
turf-line-chunk GeoJSON.LineString.chunked(length:)
GeoJSON.Polygon.LinearRing.chunked(length:)
turf-line-intersect GeoJSON.LineString.intersection(with:)
turf-line-slice GeoJSON.LineString.sliced(from:to:)
turf-line-slice-along GeoJSON.LineString.trimmed(from:distance:)
GeoJSON.LineString.trimmed(from:to:)
turf-midpoint mid(_:_:)
turf-nearest-point-on-line GeoJSON.LineString.closestCoordinate(to:)
turf-point-on-feature GeoJSON.Geometry.nearestPoint(to:)
GeoJSON.Polygon.nearestPoint(to:)
turf-polygon-smooth GeoJSON.Polygon.smooth(iterations:)
turf-union Not provided, but see ASPolygonKit
turf-simplify GeoJSON.simplify(options:)

Additionally, it adds the following features, which do not have a direct equivalent in turf.js:

  • GeoJSON.Direction.difference(from:)
  • GeoJSON.Direction.wrap(min:max:)
  • GeoJSON.LineString.frechetDistance(to:): Determines the Fréchet distance between two line strings, which is a measure of their similarity.
  • GeoJSON.GeometryObject(splittingWhenCrossingAntiMeridian:): Breaks up a LineString or Polygon into two when crossing the anti-meridian.

CLI

Comes with a mini-CLI geokitten with these commands:

  • geokitten simplify $input to simplify a GeoJSON

Installation

Via SPM

First clone or download the repository, then run this:

swift build -c release
sudo cp .build/release/geokitten /usr/local/bin/geokitten
Via Mint
mint install maparoni/GeoJSONKit-Turf@main

If you get a permissions error, check this Mint issue.