Skip to content

Commit

Permalink
added optionals to the SpatialAnalysis.swift file
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilson85 committed Apr 17, 2018
1 parent f7ca870 commit 6006a74
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
8 changes: 7 additions & 1 deletion GEOSwift/MapKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ open class MKShapesCollection : MKShape, MKOverlay {
MKShape in
return geometry.mapShape()
})
self.centroid = CLLocationCoordinate2DFromCoordinate(geometryCollection.centroid().coordinate)

if let coordinate = geometryCollection.centroid()?.coordinate {
self.centroid = CLLocationCoordinate2DFromCoordinate(coordinate)
} else {
self.centroid = CLLocationCoordinate2DFromCoordinate(CoordinateFromCLLocationCoordinate2D(CLLocationCoordinate2DMake(0, 0)))
}

self.shapes = shapes

if let envelope = geometryCollection.envelope() as? Polygon {
Expand Down
2 changes: 1 addition & 1 deletion GEOSwift/QuickLook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public extension Geometry {
region = MKCoordinateRegionMake(center,span)
} else {
if let envelope = self.envelope() as? Polygon {
let centroid = envelope.centroid()
guard let centroid = envelope.centroid() else { return nil }
let center = CLLocationCoordinate2DMake(centroid.coordinate.y, centroid.coordinate.x)
let exteriorRing = envelope.exteriorRing
let upperLeft = exteriorRing.points[2]
Expand Down
56 changes: 28 additions & 28 deletions GEOSwift/SpatialAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,57 @@ public extension Geometry {

/// - returns: A Polygon that represents all points whose distance from this geometry is less than or equal to the given width.
func buffer(width: Double) -> Geometry? {
guard let bufferGEOM = GEOSBuffer_r(GEOS_HANDLE, self.geometry, width, 0) else {
return nil
}
guard let bufferGEOM = GEOSBuffer_r(GEOS_HANDLE, self.geometry, width, 0) else { return nil }
return Geometry.create(bufferGEOM, destroyOnDeinit: true)
}

/// - returns: The smallest Polygon that contains all the points in the geometry.
func convexHull() -> Polygon {
let convexHullGEOM = GEOSConvexHull_r(GEOS_HANDLE, self.geometry)!
let convexHull = Geometry.create(convexHullGEOM, destroyOnDeinit: true) as! Polygon
func convexHull() -> Polygon? {
guard let convexHullGEOM = GEOSConvexHull_r(GEOS_HANDLE, self.geometry),
let convexHull = Geometry.create(convexHullGEOM, destroyOnDeinit: true) as? Polygon else { return nil }
return convexHull
}

/// - returns: a Geometry representing the points shared by this geometry and other.
func intersection(_ geometry: Geometry) -> Geometry {
let intersectionGEOM = GEOSIntersection_r(GEOS_HANDLE, self.geometry, geometry.geometry)!
let intersection = Geometry.create(intersectionGEOM, destroyOnDeinit: true)!
func intersection(_ geometry: Geometry) -> Geometry? {
guard let intersectionGEOM = GEOSIntersection_r(GEOS_HANDLE, self.geometry, geometry.geometry),
let intersection = Geometry.create(intersectionGEOM, destroyOnDeinit: true) else { return nil }
return intersection
}

/// - returns: A Geometry representing all the points in this geometry and the other.
func union(_ geometry: Geometry) -> Geometry {
let unionGEOM = GEOSUnion_r(GEOS_HANDLE, self.geometry, geometry.geometry)!
let union = Geometry.create(unionGEOM, destroyOnDeinit: true)!
func union(_ geometry: Geometry) -> Geometry? {
guard let unionGEOM = GEOSUnion_r(GEOS_HANDLE, self.geometry, geometry.geometry),
let union = Geometry.create(unionGEOM, destroyOnDeinit: true) else { return nil }
return union
}

/// - returns: A Geometry representing all the points in this geometry and the other.
func unaryUnion() -> Geometry {
let unionGEOM = GEOSUnaryUnion_r(GEOS_HANDLE, self.geometry)!
let union = Geometry.create(unionGEOM, destroyOnDeinit: true)!
func unaryUnion() -> Geometry? {
guard let unionGEOM = GEOSUnaryUnion_r(GEOS_HANDLE, self.geometry),
let union = Geometry.create(unionGEOM, destroyOnDeinit: true) else { return nil }

return union
}

/// - returns: A Geometry representing the points making up this geometry that do not make up other.
func difference(_ geometry: Geometry) -> Geometry {
let differenceGEOM = GEOSDifference_r(GEOS_HANDLE, self.geometry, geometry.geometry)!
let difference = Geometry.create(differenceGEOM, destroyOnDeinit: true)!
func difference(_ geometry: Geometry) -> Geometry? {
guard let differenceGEOM = GEOSDifference_r(GEOS_HANDLE, self.geometry, geometry.geometry),
let difference = Geometry.create(differenceGEOM, destroyOnDeinit: true) else { return nil }
return difference
}

/// - returns: The boundary as a newly allocated Geometry object.
func boundary() -> Geometry {
let boundaryGEOM = GEOSBoundary_r(GEOS_HANDLE, self.geometry)!
let boundary = Geometry.create(boundaryGEOM, destroyOnDeinit: true)!
func boundary() -> Geometry? {
guard let boundaryGEOM = GEOSBoundary_r(GEOS_HANDLE, self.geometry),
let boundary = Geometry.create(boundaryGEOM, destroyOnDeinit: true) else { return nil }
return boundary
}

/// - returns: A Waypoint representing the geometric center of the geometry. The point is not guaranteed to be on the interior of the geometry.
func centroid() -> Waypoint {
let centroidGEOM = GEOSGetCentroid_r(GEOS_HANDLE, self.geometry)!
let centroid = Geometry.create(centroidGEOM, destroyOnDeinit: true) as! Waypoint
func centroid() -> Waypoint? {
guard let centroidGEOM = GEOSGetCentroid_r(GEOS_HANDLE, self.geometry),
let centroid = Geometry.create(centroidGEOM, destroyOnDeinit: true) as? Waypoint else { return nil }
return centroid
}

Expand All @@ -76,9 +75,10 @@ public extension Geometry {
}

/// - returns: A POINT guaranteed to lie on the surface.
func pointOnSurface() -> Waypoint {
let pointOnSurfaceGEOM = GEOSPointOnSurface_r(GEOS_HANDLE, self.geometry)!
let pointOnSurface = Geometry.create(pointOnSurfaceGEOM, destroyOnDeinit: true) as! Waypoint
func pointOnSurface() -> Waypoint? {
guard let pointOnSurfaceGEOM = GEOSPointOnSurface_r(GEOS_HANDLE, self.geometry),
let pointOnSurface = Geometry.create(pointOnSurfaceGEOM, destroyOnDeinit: true) as? Waypoint else { return nil }

return pointOnSurface
}

Expand All @@ -105,7 +105,7 @@ public extension Geometry {

/// - returns: The DE-9IM intersection matrix (a string) representing the topological relationship between this geometry and the other.
func relationship(_ geometry: Geometry) -> String {
let CString = GEOSRelate_r(GEOS_HANDLE, self.geometry, geometry.geometry)!
guard let CString = GEOSRelate_r(GEOS_HANDLE, self.geometry, geometry.geometry) else { return "" }
return String(cString: CString)
}
}

0 comments on commit 6006a74

Please sign in to comment.