CocoaPods
Update your
Podfile
to include:use_frameworks! pod 'GEOSwift'
Run
$ pod install
Swift Package Manager
Update the top-level dependencies in your
Package.swift
to include:.package(url: "https://github.com/GEOSwift/GEOSwift.git", from: "10.1.0")
Update the target dependencies in your
Package.swift
to include"GEOSwift"
In certain cases, you may also need to explicitly include geos as a dependency. See issue #195 for details.
Geometry creation
// 1. From Well Known Text (WKT) representation
let point = try Point(wkt: "POINT(10 45)")
let polygon = try Geometry(wkt: "POLYGON((35 10, 45 45.5, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))")
// 2. From a Well Known Binary (WKB)
let wkb: NSData = geometryWKB()
let geometry2 = try Geometry(wkb: wkb)
// 3. From a GeoJSON file:
let decoder = JSONDecoder()
if let geoJSONURL = Bundle.main.url(forResource: "multipolygon", withExtension: "geojson"),
let data = try? Data(contentsOf: geoJSONURL),
let geoJSON = try? decoder.decode(GeoJSON.self, from: data),
case let .feature(feature) = geoJSON,
let italy = feature.geometry
{
italy
}
Topological operations
Let’s say we have two geometries:
GEOSwift let you perform a set of operations on these two geometries:
Predicates:
- equals: returns true if this geometric object is “spatially equal” to another geometry.
- disjoint: returns true if this geometric object is “spatially disjoint” from another geometry.
- intersects: returns true if this geometric object “spatially intersects” another geometry.
- touches: returns true if this geometric object “spatially touches” another geometry.
- crosses: returns true if this geometric object “spatially crosses’ another geometry.
- within: returns true if this geometric object is “spatially within” another geometry.
- contains: returns true if this geometric object “spatially contains” another geometry.
- overlaps: returns true if this geometric object “spatially overlaps” another geometry.
- relate: returns true if this geometric object is spatially related to another geometry by testing for intersections between the interior, boundary and exterior of the two geometric objects as specified by the values in the intersectionPatternMatrix.
Playground
Explore more, interactively, in the playground, which is available in the
GEOSwiftMapKit project. It can be
found inside GEOSwiftMapKit
workspace. Open the workspace in Xcode, build the
GEOSwiftMapKit
framework and open the playground file.
Maintainer
- Andrew Hershberger (@macdrevx)
Past Maintainers
- Virgilio Favero Neto (@vfn)
- Andrea Cremaschi (@andreacremaschi) (original author)