批量请求高德地图API解析经纬度

依然范特西╮ 提交于 2019-12-27 05:45:22

使用高德逆地理编码批量请求解析经纬度到具体的位置信息

项目中涉及到要根据经纬度解析实际的地理位置,所以就需要搞一把。
纵观出申请一个企业开发者账号,每天的api调用就可以达到300w次了,开心。

个人开发者和企业开发者的调用限制:
在这里插入图片描述

这是注册地址:

 https://lbs.amap.com/dev/id/choose

web API调用参数
在这里插入图片描述
下面是用python写的一个demo,可以参考一下。



# -*- coding: utf-8 -*-

import uuid
import requests
import json



class Location_query(object):
    def __init__(self, key):
        # 传入参数,申请到的高德api
        self.key = key
        if len(self.key) != 32:
            print("你的key好像不对,大兄弟...")
            exit(0)


    def get_map_address(self, gd_coordinates):
        """
        逆地理编码(高德坐标转地址)
        :param gd_coordinates:多个位置"高德经度,高德纬度"的集合(元组或数组都可以)
        :return:多个位置地址信息的列表,最多一次查询20个经纬度点
        """
        try:
            coordinates = "|".join(gd_coordinates)
            batch = "true" if len(gd_coordinates) > 1 else "false"
            url = "https://restapi.amap.com/v3/geocode/regeo?key={0}&location={1}&batch={2}&radius=500&extensions=base&output=json".format(
                self.key, coordinates, batch)
            response = requests.get(url)
            result = json.loads(response.text)
            if "1" == result["status"]:
                address = []
                if batch == "true":
                    address.extend([[add['addressComponent']['district'],add["formatted_address"]]
                                    for add in list(result["regeocodes"])])
                else:
                    fmt_add = result["regeocode"]["formatted_address"]
                    district = result['regeocode']['addressComponent']['district']
                    address.append([district,fmt_add])
                return address
            else:
                print("Unknown")
        except Exception as e:
            raise e


if __name__ == "__main__":

    key = "24306d0fc1cd30e49be57305ce824b**"  # 填写你申请到的高德开放平台api
    addr = Location_query(key)
    list_loctions = []

    # 写出
    fwrite = open('/root/GEO_data/ok/gps01.txt','w+',encoding='utf-8')
    # 读入
    # with open("D://model//PaddleSeg//python_Algorithm//loc.csv",'r',encoding='utf-8') as f:
    with open('/root/GEO_data/original/gps0.txt', 'r', encoding='utf-8') as f:
        flines = f.readlines()
        try:
            for idx, line in enumerate(flines, start=1):
                sp_line = line.split(",")
                if(float(sp_line[4])>0.0 and float(sp_line[5])>0.0):
                    location = ",".join([str(sp_line[4]), str(sp_line[5])])
                    print(location)
                    list_loctions.append(location)
                if idx % 20 == 0:
                    try:
                        res = addr.get_map_address(list_loctions)
                        if res is not None:
                            for i in res:
                                dist, detail = i[0], i[1]
                                basetime = sp_line[3]
                                gpstime = ' '.join(['-'.join([basetime[:4], basetime[4:6], basetime[6:8]]),
                                                    ':'.join([basetime[8:10], basetime[10:12], basetime[12:14]])])
                                year = basetime[:4]
                                month = basetime[4:6]
                                day = basetime[6:8]
                                hour = basetime[8:10]
                                suuid = str(uuid.uuid4()).replace("-", "")
                                fout = ','.join([suuid, dist, detail, year, month, day, hour, gpstime, line])
                                print(fout)
                                fwrite.write(fout)
                    except Exception as e:
                        print("Exception {} \n  input :{}".format(e, list_loctions))
                    list_loctions = []
        except Exception as e:
            print(e)
        finally:






标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!