我有以下方法检查points
是否为多边形
from shapely import wkt
def validate_polygon(points):
try:
wkt.loads("POLYGON((%s))" % points)
except Exception as ex:
raise WrongRequestDataError("Incorrect points format. " + str(ex))
如果polygon
面积大于400平方英里,我如何计算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")
print(polygon.area)
4.406979500001112e-06
的面积并抛出异常?
我试过检查一下:
Next
但它似乎是错误的答案或它用于此值的单位类型以及如何将其转换为平方英里或平方公里^?
答案 0 :(得分:3)
通常你会计算这样的区域:
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)
# the area in square degrees
area_sdeg = polygon.area
注意:对于以平方米为单位的面积计算,您必须使用https://gist.github.com/robinkraft/c6de2f988c9d3f01af3c
中所述的投影答案 1 :(得分:0)
如果你查看documentation,有一种计算area的通用方法。
答案 2 :(得分:0)
这是最适合我的最佳解决方案
def compute_polygon_area(points):
coordinates = (tuple(map(float, x.split())) for x in points.split(', '))
xy_coordinates = switch_to_xy_coordinates(coordinates)
return Polygon(xy_coordinates).area
def switch_to_xy_coordinates(coordinates):
earth_radius = 6371 # in km
lat_dist = pi * earth_radius / 180.0
latitudes, longitudes = zip(*coordinates)
y = (lat * lat_dist for lat in latitudes)
x = (lon * lat_dist * cos(radians(lat))
for lat, lon in zip(latitudes, longitudes))
return list(zip(x, y))