一个用 Swift 编写的空间分析库,用于原生 iOS、macOS、tvOS、watchOS、visionOS 和 Linux 应用程序,从 Turf.js 移植而来。
Turf 需要 Xcode 14.1 或更高版本,并支持以下最低部署目标
或者,您可以将 Turf 合并到任何支持Swift的平台(包括 Linux)上的无 Xcode 命令行工具中。
如果您的项目是用 Objective-C 编写的,您需要在 turf-swift 和您的 Objective-C 代码之间编写一个兼容层。 如果您的项目是用 Objective-C++ 编写的,您可以使用 spatial-algorithms 作为 Turf 的替代方案。
可以使用任何流行的 Swift 依赖项管理器安装版本。
要使用 CocoaPods 安装 Turf
pod 'Turf', '~> 4.0'
pod repo update。pod install 并打开生成的 Xcode 工作区。import Turf 添加到应用程序目标中的任何 Swift 文件。要使用 Carthage 安装 Turf
github "mapbox/turf-swift" ~> 4.0
carthage bootstrap。import Turf 添加到应用程序目标中的任何 Swift 文件。要使用 Swift Package Manager 安装 Turf,请将以下包添加到您的 Package.swift 文件中的 dependencies
.package(url: "https://github.com/mapbox/turf-swift.git", from: "4.0.0")
然后在您模块中的任何 Swift 文件中 import Turf。
这个正在进行的 Turf.js 移植包含以下功能
| Turf.js | Turf for Swift |
|---|---|
| turf-along#along | LineString.coordinateFromStart(distance:) |
| turf-area#area | Polygon.area |
| turf-bearing#bearing | CLLocationCoordinate2D.direction(to:)LocationCoordinate2D.direction(to:) on LinuxRadianCoordinate2D.direction(to:) |
| turf-bezier-spline#bezierSpline | LineString.bezier(resolution:sharpness:) |
| turf-boolean-point-in-polygon#booleanPointInPolygon | Polygon.contains(_:ignoreBoundary:) |
| turf-center#center | Polygon.center |
| turf-center-of-mass#centerOfMass | Polygon.centerOfMass |
| turf-centroid#centroid | Polygon.centroid |
| turf-circle#circle | Polygon(center:radius:vertices:) |
| turf-destination#destination | CLLocationCoordinate2D.coordinate(at:facing:)LocationCoordinate2D.coordinate(at:facing:) on LinuxRadianCoordinate2D.coordinate(at:facing:) |
| turf-distance#distance | CLLocationCoordinate2D.distance(to:)LocationCoordinate2D.distance(to:) on LinuxRadianCoordinate2D.distance(to:) |
| turf-helpers#polygon | Polygon(_:) |
| turf-helpers#lineString | LineString(_:) |
| turf-helpers#degreesToRadians | CLLocationDegrees.toRadians()LocationDegrees.toRadians() on Linux |
| turf-helpers#radiansToDegrees | CLLocationDegrees.toDegrees()LocationDegrees.toDegrees() on Linux |
| turf-helpers#convertLength turf-helpers#convertArea |
Measurement.converted(to:) |
| turf-length#length | LineString.distance(from:to:) |
| turf-line-intersect#lineIntersect | LineString.intersections(with:) |
| turf-line-slice#lineSlice | LineString.sliced(from:to:) |
| turf-line-slice-along#lineSliceAlong | LineString.trimmed(from:to:) |
| turf-midpoint#midpoint | mid(_:_:) |
| turf-nearest-point-on-line#nearestPointOnLine | LineString.closestCoordinate(to:) |
| turf-polygon-to-line#polygonToLine | LineString(_:)MultiLineString(_:) |
| turf-simplify#simplify | LineString.simplify(tolerance:highestQuality:)LineString.simplified(tolerance:highestQuality:) |
| turf-polygon-smooth#polygonSmooth | Polygon.smooth(iterations:) |
| — | CLLocationDirection.difference(from:)LocationDirection.difference(from:) on Linux |
| — | CLLocationDirection.wrap(min:max:)LocationDirection.wrap(min:max:) on Linux |
turf-swift 还包含一个 GeoJSON 编码器/解码器,支持 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
]
}
}
*/
turf-swift 包含对实现 WKTCodable 协议的几何体的最小 WKT 编码/解码支持。
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)