python 高德地图 距离计算 面积计算

参考了这个: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一致。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值