Calculation of the estimated area of ​​MKPolygon Swift

I tried to get the MKPolygon calculation, and I followed a few links here and adjusted accordingly. It seems I can’t calculate the square meters correctly. If necessary, I can provide additional information.

Here is my code

func polygonArea() -> Double{
    var area: Double = 0
    var kEarthRadius:Double = 6378137
    var coord: NSArray = self.coordinates()
    if (coord.count > 2){
        var p1, p2, p3 : CLLocationCoordinate2D

        var lowerIndex, middleIndex, upperIndex: Int
        for var i = 0; i < points.count - 1; i++ {
            if (i == (points.count - 2)){
                lowerIndex = points.count - 2
                middleIndex = points.count - 1
                upperIndex = 0
            }else if (i == points.count - 1){
                lowerIndex = points.count - 1
                middleIndex = 0
                upperIndex = 1;

            }else{
                lowerIndex = i
                middleIndex = i + 1
                upperIndex = i + 2
            }
            p1 = points[lowerIndex]
            p2 = points[middleIndex]
            p3 = points[upperIndex]
            area +=  degreesToRadians(p2.longitude - p1.longitude) * (2 + sin(degreesToRadians(p1.latitude)) + sin(degreesToRadians(p2.latitude)))

        }
        area = area * kEarthRadius * kEarthRadius / 2

    }
    return area
    measureLabel.text = "\(area)"
}

I followed this link specifically Calculation of the area of ​​M.K. Polygon

+4
source share
3 answers

Here is the version of Objective-C. You can use it in your code without problems.

polygon on sphere 1 polygon on sphere 2

#define kEarthRadius 6378137
@implementation MKPolygon (AreaCalculation)

- (double) area {
  double area = 0;
  NSMutableArray *coords = [[self coordinates] mutableCopy];
  [coords addObject:[coords firstObject]];

  if (coords.count > 2) {
    CLLocationCoordinate2D p1, p2;
    for (int i = 0; i < coords.count - 1; i++) {
      p1 = [coords[i] MKCoordinateValue];
      p2 = [coords[i + 1] MKCoordinateValue];
      area += degreesToRadians(p2.longitude - p1.longitude) * (2 + sinf(degreesToRadians(p1.latitude)) + sinf(degreesToRadians(p2.latitude)));
    }

    area = - (area * kEarthRadius * kEarthRadius / 2);
  }
  return area;
}
- (NSArray *)coordinates {
  NSMutableArray *points = [NSMutableArray arrayWithCapacity:self.pointCount];
  for (int i = 0; i < self.pointCount; i++) {
    MKMapPoint *point = &self.points[i];
    [points addObject:[NSValue valueWithMKCoordinate:MKCoordinateForMapPoint(* point)]];
  }
  return points.copy;
}

double degreesToRadians(double radius) {
  return radius * M_PI / 180;
}

EDIT: Updated so that the calculation also considers the point on the line as inside the field.

+1
source

, UTM, long/lat. , , .

, , C, Swift, , . 4 ~ 1600 , , , .

class func polygonArea(points: Array<GAPaddockPoint>) -> Double {

    var area:Double = 0.0
    var j = points.count - 1
    for var i=0; i<points.count; i++ {
        area = area + ( (points[j].easting + points[i].easting) * (points[j].northing - points[i].northing) )
        j=i
    }

    return area * 0.5
}
0

A quick version was posted by @AVT in MKPolygon Area Calculation :

import MapKit
let kEarthRadius = 6378137.0

// CLLocationCoordinate2D uses degrees but we need radians
func radians(degrees: Double) -> Double {
    return degrees * M_PI / 180
}

func regionArea(locations: [CLLocationCoordinate2D]) -> Double {

    guard locations.count > 2 else { return 0 }
    var area = 0.0

    for i in 0..<locations.count {
        let p1 = locations[i > 0 ? i - 1 : locations.count - 1]
        let p2 = locations[i]

        area += radians(degrees: p2.longitude - p1.longitude) * (2 + sin(radians(degrees: p1.latitude)) + sin(radians(degrees: p2.latitude)) )
    }

    area = -(area * kEarthRadius * kEarthRadius / 2)

    return max(area, -area) // In order not to worry about is polygon clockwise or counterclockwise defined.
}
0
source

Source: https://habr.com/ru/post/1619425/


All Articles