diff --git a/Turf.xcodeproj/project.pbxproj b/Turf.xcodeproj/project.pbxproj index d0e5695c..6a7d74ad 100644 --- a/Turf.xcodeproj/project.pbxproj +++ b/Turf.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 052521D81FC47A1300DD266A /* polygon.geojson in Resources */ = {isa = PBXBuildFile; fileRef = 052521D71FC47A0300DD266A /* polygon.geojson */; }; + 052521D91FC47A1900DD266A /* polygon.geojson in Resources */ = {isa = PBXBuildFile; fileRef = 052521D71FC47A0300DD266A /* polygon.geojson */; }; 353E9B101F3E093A007CFA23 /* Turf.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 353E9B071F3E093A007CFA23 /* Turf.framework */; }; 353E9B1E1F3E09D3007CFA23 /* CoreLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356D24531F17948C003BBB9D /* CoreLocation.swift */; }; 353E9B1F1F3E09D8007CFA23 /* Turf.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35650B0A1F150DDB00B5C158 /* Turf.swift */; }; @@ -41,6 +43,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 052521D71FC47A0300DD266A /* polygon.geojson */ = {isa = PBXFileReference; lastKnownFileType = text; path = polygon.geojson; sourceTree = ""; }; 353E9B071F3E093A007CFA23 /* Turf.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Turf.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 353E9B0F1F3E093A007CFA23 /* TurfMacTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TurfMacTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 35650AF01F150DC500B5C158 /* Turf.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Turf.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -135,6 +138,7 @@ isa = PBXGroup; children = ( 356D24581F179B72003BBB9D /* dc-line.geojson */, + 052521D71FC47A0300DD266A /* polygon.geojson */, ); path = Fixtures; sourceTree = ""; @@ -291,6 +295,7 @@ buildActionMask = 2147483647; files = ( 35CB7F6F1F798A51008A18C8 /* dc-line.geojson in Resources */, + 052521D91FC47A1900DD266A /* polygon.geojson in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -306,6 +311,7 @@ buildActionMask = 2147483647; files = ( 356D24591F179B72003BBB9D /* dc-line.geojson in Resources */, + 052521D81FC47A1300DD266A /* polygon.geojson in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Turf/Turf.swift b/Turf/Turf.swift index b4f72d83..e1666fc8 100644 --- a/Turf/Turf.swift +++ b/Turf/Turf.swift @@ -4,7 +4,10 @@ public typealias LocationRadians = Double public typealias RadianDistance = Double public typealias RadianDirection = Double -let metersPerRadian = 6_373_000.0 + +let metersPerRadian: CLLocationDistance = 6_373_000.0 +// WGS84 equatorial radius as specified by the International Union of Geodesy and Geophysics +let equatorialRadius: CLLocationDistance = 6_378_137 /** A `RadianCoordinate2D` is a coordinate represented in radians as opposed to @@ -285,3 +288,68 @@ public struct Polyline { return closestCoordinate } } + + +/** + Creates a `Ring` struct that represents a closed figure that is bounded by three or more straight line segments. + */ +public struct Ring { + var coordinates: [CLLocationCoordinate2D] + + /** + * Calculate the approximate area of the polygon were it projected onto the earth, in square meters. + * Note that this area will be positive if ring is oriented clockwise, otherwise it will be negative. + * + * Reference: + * Robert. G. Chamberlain and William H. Duquette, "Some Algorithms for Polygons on a Sphere", JPL Publication 07-03, Jet Propulsion + * Laboratory, Pasadena, CA, June 2007 https://trs.jpl.nasa.gov/handle/2014/41271 + * + */ + public var area: Double { + var area: Double = 0 + let coordinatesCount: Int = coordinates.count + + if coordinatesCount > 2 { + for index in 0..