参考了这个:https://blog.csdn.net/u014793102/article/details/103869480
面积计算:
高德网址:https://developer.amap.com/demo/javascript-api/example/calcutation/ring-area
也就是说 这些个坐标边界计算出来的结果面积就是左边那个数字。
代码:
from math import asin, sqrt, cos, pi
def st_area(wkt_list):
# 弧度
radian = pi / 180.0
# 地球半径
radius = 6378137
# 地球弧度
earth_radian = radius * radian
# 面积
area = 0
length = len(wkt_list)
if length < 3:
return 0
for index in range(0, length - 1):
front_point = wkt_list[index]
rear_point = wkt_list[index + 1]
front_sector = front_point[0] * earth_radian * cos(front_point[1] * radian)
front_line = front_point[1] * earth_radian
rear_sector = rear_point[0] * earth_radian * cos(rear_point[1] * radian)
area += (front_sector * rear_point[1] * earth_radian - rear_sector * front_line)
wkt_rear = wkt_list[length - 1]
wkt_front = wkt_list[0]
wkt_rear_sector = wkt_rear[0] * earth_radian * cos(wkt_rear[1] * radian)
wkt_rear_line = wkt_rear[1] * earth_radian
wkt_front_sector = wkt_front[0] * earth_radian * cos(wkt_front[1] * radian)
area += wkt_rear_sector * wkt_front[1] * earth_radian - wkt_front_sector * wkt_rear_line
return 0.5 * abs(area) / 1000000
a = [
[116.169465, 39.932670],
[116.160260, 39.924492],
[116.150625, 39.710019],
[116.183198, 39.709920],
[116.226950, 39.777616],
[116.442621, 39.799892],
[116.463478, 39.790066],
[116.588276, 39.809551],
[116.536091, 39.808859],
[116.573856, 39.839643],
[116.706380, 39.916740],
[116.600293, 39.937770],
[116.514805, 39.982375],
[116.499935, 40.013710],
[116.546520, 40.030443],
[116.687668, 40.129961],
[116.539697, 40.080659],
[116.503390, 40.058474],
[116.468800, 40.052578]
]
print(st_area(a))
结果是:
952.8632989570312
单位是平方千米,和高德地图API一致。
两点之间的距离计算:
https://developer.amap.com/demo/javascript-api/example/calcutation/calculate-distance-between-two-markers
from math import asin, sqrt, cos, pi
# point_a,point_b是经纬度,格式为[lng,lat]
def st_distance(point_a, point_b):
# 弧度
radian = pi / 180.0
# 地球半径
radius = 6378137
f = point_a[1] * radian
h = point_b[1] * radian
k = 2 * radius
d = point_b[0] * radian - point_a[0] * radian
e = (1 - cos(h - f) + (1 - cos(d)) * cos(f) * cos(h)) / 2
return k * asin(sqrt(e))
print(st_distance([116.368904, 39.923423],[116.387271, 39.922501]))
代码结果是1571.3795388458316
单位是米,和高德地图API一致。