-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add polygon area calculation #21
Changes from 3 commits
d77070d
e340401
d89c654
4adfa4f
a56f773
0c4382e
dd6eac8
d3ead9a
4b4ff4e
944e581
047fe58
dea04ac
ead6d97
51f86dc
6e57647
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ public typealias RadianDistance = Double | |
public typealias RadianDirection = Double | ||
|
||
let metersPerRadian = 6_373_000.0 | ||
let equitorialRadius:Double = 6378137 | ||
|
||
/** | ||
A `RadianCoordinate2D` is a coordinate represented in radians as opposed to | ||
|
@@ -285,3 +286,61 @@ public struct Polyline { | |
return closestCoordinate | ||
} | ||
} | ||
|
||
public struct Polygon { | ||
var coordinates: [CLLocationCoordinate2D] | ||
|
||
var polygonArea: Double { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This property already lives inside a struct named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment with a permalink to the specific commit of the Turf package you’re porting this code from, in case we need to track down a discrepancy in the future. |
||
return 0; | ||
} | ||
|
||
/** | ||
* Calculate the approximate area of the polygon were it projected onto the earth. | ||
* 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 http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409 | ||
* | ||
*/ | ||
|
||
var ringArea: Double { | ||
var p1:CLLocationCoordinate2D | ||
var p2:CLLocationCoordinate2D | ||
var p3:CLLocationCoordinate2D | ||
var lowerIndex:Int | ||
var middleIndex:Int | ||
var upperIndex:Int | ||
var i:Int | ||
var area:Double = 0 | ||
var coordsLength:Int = coordinates.count | ||
|
||
if (coordsLength > 2) { | ||
for(index, coordinate) in coordinates.enumerated() { | ||
if (index == coordsLength - 2) { | ||
lowerIndex = coordsLength - 2 | ||
middleIndex = coordsLength - 1 | ||
upperIndex = 0 | ||
} else if(index == coordsLength - 1) { | ||
lowerIndex = coordsLength - 1; | ||
middleIndex = 0; | ||
upperIndex = 1; | ||
} else { | ||
lowerIndex = index | ||
middleIndex = index + 1 | ||
upperIndex = index + 2 | ||
} | ||
|
||
p1 = coordinates[lowerIndex] | ||
p2 = coordinates[middleIndex] | ||
p3 = coordinates[upperIndex] | ||
area += (p3.longitude.toRadians() - p1.longitude.toRadians()) * sin(p2.latitude.toRadians()) | ||
} | ||
|
||
area = area * equitorialRadius * equitorialRadius / 2; | ||
|
||
} | ||
|
||
return area | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[ | ||
-77.0416259765625, | ||
39.06611426153784 | ||
], | ||
[ | ||
-77.32177734375, | ||
38.843986129756615 | ||
], | ||
[ | ||
-76.7669677734375, | ||
38.831149809348744 | ||
], | ||
[ | ||
-77.0416259765625, | ||
39.06611426153784 | ||
] | ||
] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capturing from chat: The coordinates property on a GeoJSON Polygon should be represented as a[[CLLocationCoordate2D]]
Even better now encapsulated in a Ring.