Ivan Petrushenko Ivan Petrushenko - 1 year ago 109
Python Question

How to calculate area of a polygon with latitude and longitude using shapely lib?

I have the following method that checks whether is

are polygon

from shapely import wkt

def validate_polygon(points):
wkt.loads("POLYGON((%s))" % points)
except Exception as ex:
raise WrongRequestDataError("Incorrect points format. " + str(ex))

How can I calculate area of the
and throw exception if it's greater than 400 square miles?

I have tried to check this:

polygon = wkt.loads("POLYGON((%s))" % "34.093523 -118.274893,34.091414 -118.275887,34.092082 -118.278062,34.093867 -118.276609,34.093523 -118.274893")


but it seems like the incorrect answer or what type of units it uses for this value and how to translate it to square miles or km^2?

udo udo
Answer Source

typically you would compute the area like this:

from shapely.geometry import Polygon
points = [(34.093523, -118.274893), (34.091414, -118.275887), (34.092082, -118.278062), (34.093867, -118.276609), (34.093523, -118.274893)]
polygon = Polygon(points)
earth_radius_miles = 3959
# this is a calculation presuming the earth is "flat"
area_sqm = polygon.area*earth_radius_miles**2

note: for an accurate area calculation, you would have to look at projections as in https://stackoverflow.com/a/21420950

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download