Python – Calculating Area and Perimeter of Coordinate Points

coordinate systempython

I have a set of points that make up a polygon. I need to find the area and perimeter for the shape in Python. I have used the shapely library to calculate the area and perimeter but it is nonsensical because the points are latitude and longitude coordinates (in US States).

I need to convert the points to an equal-area projection system first. I was thinking of using Sinusoidal projection. I was able to do this and calculate area (code below), but I'm not sure how to interpret the result (also is it right?). What unit is it in? (m²?)

Next I need to calculate perimeter, but would the Sinusoidal projection distort it and throw it off? How would I deal with that?

I am a computer scientist and my work on a project has led me down this rabbit hole. How can I calculate the area/perimeter of this shape in the correct way?

def sinusoidal(lat, long):
    earth_radius = 6371009 # in meters
    lat_dist = pi * earth_radius / 180.0

    y = lat * lat_dist
    x = long * lat_dist * cos(radians(lat))

    return x, y

Best Answer

In this link you have an example for determining Geodesic area for a polygon located in antarctic region by using pyproj python3 module. Adapting such script for following polygon (green) in an arbitrary area of USA:

enter image description here

it looks as follows:

from pyproj import Geod

geod = Geod('+a=6378137 +f=0.0033528106647475126')

lons = [-102.20253523916332, -101.59096157206567, -100.65438018473898, -101.90199046561818]
lats = [37.21550522238942, 37.70825886273666, 36.93243398218993, 36.394249155143996 ]

poly_area, poly_perimeter = geod.polygon_area_perimeter(lons, lats)

print("area: {} , perimeter: {}".format(poly_area, poly_perimeter))

After running it in python console, result was as follow:

area: 10473941945.624039 , perimeter: 418057.54140898824

Obtained values are comparable (differences probably due to different used Geod in each case) to those showed in attributes table of green shapefile (area: 10476912480.580376, perimeter: 418057.541405565).

Related Question