Skip to content

Commit

Permalink
Responses to code review / pull request:
Browse files Browse the repository at this point in the history
Renamed several methods to use identifier `coordinate` instead of `point`.
Made `BoundingBox` a `Codable` `struct`.
Removed several instances of an explict usage of `self`.
  • Loading branch information
Sandy Chapman authored and frederoni committed Sep 18, 2018
1 parent d679d07 commit 1ee3259
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
28 changes: 18 additions & 10 deletions Sources/Turf/BoundingBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@ import Foundation
import CoreLocation
#endif

public class BoundingBox {
public struct BoundingBox: Codable {

public init?(points: [CLLocationCoordinate2D]?) {
guard points?.count ?? 0 > 0 else {
public init?(from coordinates: [CLLocationCoordinate2D]?) {
guard coordinates?.count ?? 0 > 0 else {
return nil
}
(minLat, maxLat, minLon, maxLon) = points!
let (minLat, maxLat, minLon, maxLon) = coordinates!
.reduce((minLat: 0, maxLat: 0, minLon: 0, maxLon: 0)) { (result, coordinate) -> (minLat: Double, maxLat: Double, minLon: Double, maxLon: Double) in
let minLat = min(coordinate.latitude, result.0)
let maxLat = max(coordinate.latitude, result.1)
let minLon = min(coordinate.longitude, result.2)
let maxLon = max(coordinate.longitude, result.3)
return (minLat: minLat, maxLat: maxLat, minLon: minLon, maxLon: maxLon)
}
northWest = CLLocationCoordinate2D(latitude: maxLat, longitude: minLon)
southEast = CLLocationCoordinate2D(latitude: minLat, longitude: maxLon)
}

public func contains(point: CLLocationCoordinate2D) -> Bool {
return minLat < point.latitude && maxLat > point.latitude && minLon < point.longitude && maxLon > point.longitude
public init(_ northWest: CLLocationCoordinate2D, _ southEast: CLLocationCoordinate2D) {
self.northWest = northWest
self.southEast = southEast
}

public func contains(_ coordinate: CLLocationCoordinate2D) -> Bool {
return southEast.latitude < coordinate.latitude
&& northWest.latitude > coordinate.latitude
&& northWest.longitude < coordinate.longitude
&& southEast.longitude > coordinate.longitude
}

// MARK: - Private

let minLat: Double
let maxLat: Double
let minLon: Double
let maxLon: Double
let northWest: CLLocationCoordinate2D
let southEast: CLLocationCoordinate2D
}
14 changes: 7 additions & 7 deletions Sources/Turf/Polygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ extension Polygon {
extension Polygon {

/**
* Determines if the given point falls within the polygon and outside of its interior rings.
* The optional parameter `ignoreBoundary` will result in the method returning true if the given point
* Determines if the given coordinate falls within the polygon and outside of its interior rings.
* The optional parameter `ignoreBoundary` will result in the method returning true if the given coordinate
* lies on the boundary line of the polygon or its interior rings.
*
* Ported from: https://github.com/Turfjs/turf/blob/e53677b0931da9e38bb947da448ee7404adc369d/packages/turf-boolean-point-in-polygon/index.ts#L31-L75
*/
public func contains(point: CLLocationCoordinate2D, ignoreBoundary: Bool = false) -> Bool {
let bbox = BoundingBox(points: self.coordinates.first)
guard bbox?.contains(point: point) ?? false else {
public func contains(_ coordinate: CLLocationCoordinate2D, ignoreBoundary: Bool = false) -> Bool {
let bbox = BoundingBox(from: coordinates.first)
guard bbox?.contains(coordinate) ?? false else {
return false
}
guard self.outerRing.contains(point: point, ignoreBoundary: ignoreBoundary) else {
guard outerRing.contains(coordinate, ignoreBoundary: ignoreBoundary) else {
return false
}
if let innerRings = innerRings {
for ring in innerRings {
if ring.contains(point: point, ignoreBoundary: ignoreBoundary) {
if ring.contains(coordinate, ignoreBoundary: ignoreBoundary) {
return false
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/Turf/Ring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension Ring {
*
* Ported from: https://github.com/Turfjs/turf/blob/e53677b0931da9e38bb947da448ee7404adc369d/packages/turf-boolean-point-in-polygon/index.ts#L77-L108
*/
public func contains(point: CLLocationCoordinate2D, ignoreBoundary: Bool = false) -> Bool {
public func contains(_ coordinate: CLLocationCoordinate2D, ignoreBoundary: Bool = false) -> Bool {
var ring: ArraySlice<CLLocationCoordinate2D>!
var isInside = false
if coordinates.first == coordinates.last {
Expand All @@ -78,13 +78,13 @@ extension Ring {
let yi = ring[i].latitude
let xj = ring[j].longitude
let yj = ring[j].latitude
let onBoundary = (point.latitude * (xi - xj) + yi * (xj - point.longitude) + yj * (point.longitude - xi) == 0) &&
((xi - point.longitude) * (xj - point.longitude) <= 0) && ((yi - point.latitude) * (yj - point.latitude) <= 0)
let onBoundary = (coordinate.latitude * (xi - xj) + yi * (xj - coordinate.longitude) + yj * (coordinate.longitude - xi) == 0) &&
((xi - coordinate.longitude) * (xj - coordinate.longitude) <= 0) && ((yi - coordinate.latitude) * (yj - coordinate.latitude) <= 0)
if onBoundary {
return !ignoreBoundary
}
let intersect = ((yi > point.latitude) != (yj > point.latitude)) &&
(point.longitude < (xj - xi) * (point.latitude - yi) / (yj - yi) + xi);
let intersect = ((yi > coordinate.latitude) != (yj > coordinate.latitude)) &&
(coordinate.longitude < (xj - xi) * (coordinate.latitude - yi) / (yj - yi) + xi);
if (intersect) {
isInside = !isInside;
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/TurfTests/PolygonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PolygonTests: XCTestCase {
CLLocationCoordinate2D(latitude: 41, longitude: -72),
CLLocationCoordinate2D(latitude: 41, longitude: -81),
]])
XCTAssertTrue(polygon.contains(point: coordinate))
XCTAssertTrue(polygon.contains(coordinate))
}

func testPolygonDoesNotContain() {
Expand All @@ -50,7 +50,7 @@ class PolygonTests: XCTestCase {
CLLocationCoordinate2D(latitude: 41, longitude: -42),
CLLocationCoordinate2D(latitude: 41, longitude: -51),
]])
XCTAssertFalse(polygon.contains(point: coordinate))
XCTAssertFalse(polygon.contains(coordinate))
}

func testPolygonDoesNotContainWithHole() {
Expand All @@ -71,6 +71,6 @@ class PolygonTests: XCTestCase {
CLLocationCoordinate2D(latitude: 43, longitude: -76),
],
])
XCTAssertFalse(polygon.contains(point: coordinate))
XCTAssertFalse(polygon.contains(coordinate))
}
}

0 comments on commit 1ee3259

Please sign in to comment.