高德地图API全家桶python3

高德地图全家桶

首先写下这几个标准操作

import pandas as pd # 实用的制表标准库
from pandas.io.json import json_normalize # 这个可以将json变成表格

import requests # 网络爬虫标准配置
key ="3f773d**********62d221d"

获取地理编码

def geocode(address,city=None,batch=None,sig=None)->dict:
    """获取地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
        'key': key,
        'address':address,
        'city':city,
        'batch':batch,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
#     data_df = json_normalize(response.json()['geocodes'])
    return data

家 = geocode('广…省潮…市潮…区潮……') # 写下家的详细地址
家_location = 家['geocodes'][0]['location'] # 这就是家的地理编码

逆地理编码

将经纬度转换为详细结构化的地址,且返回附近周边的POI、AOI信息。

def regeocode(location,poitype=None,radius=None,extensions="base",batch=False,roadlevel=None,sig=None,homeorcorp=None)->dict:
    """获取逆地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params={
        'key': key,
        'location':location,
        'poitype':poitype,
        'radius':radius,
        'extensions':extensions,
        'batch':batch,
        'roadlevel':roadlevel,
        'homeorcorp':homeorcorp,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

同时可以将地理编码和你地理编码的两个函数包装在一起,

def df_regecode(locale):
    地理编码 = geocode(locale)['geocodes'][0]['location']
    逆地理编码 = regeocode(地理编码,extensions="all")
    df_逆地理编码 = json_normalize(逆地理编码['regeocode']['pois'])
    return df_逆地理编码
df_regecode('广东省*****察大队')

路线规划

在此之前我们获取我所在的高中的地理编码“中学_location”。

步行

def walking(origin,destination,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()['route']['paths'][0]['steps']
    data_df = json_normalize(data)
    return data_df

家_中学 = walking(家_location,中学_location)

(tips:这个函数是直接返回一个表格,如果想返回json格式则是

# 将
    data = response.json()['route']['paths'][0]['steps']
    data_df = json_normalize(data)
# 改成
    data = response.json()

公交车

def bus(origin,destination,city,cityd,strategy,nightflag)->dict:
    url = 'https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination,
        'city':city,
        'cityd':cityd,
        'strategy':strategy,
        'nightflag':nightflag,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    x = 0
    data_df = []
    for i in data['route']['transits']:
        display(json_normalize(data['route']['transits'][x]['segments'][0]['bus']['buslines'][0]['via_stops']))
        x += 1
    return
家_中学_bus = bus(家_location,中学_location,'潮州市','潮州市',0,0)

将地理编码和公交路径包装成一个函数

the_origin=input('输入起点:')
the_end=input('输入终点:')

def bus_station(origin,end):
    起点地理编码 = geocode(orgin)['geocodes'][0]['location']
    终点地理编码 = geocode(end)['geocodes'][0]['location']
    起点所在城市 = geocode(orgin)['geocodes'][0]['city']
    终点所在城市 = geocode(end)['geocodes'][0]['city']
    bus(起点地理编码,终点地理编码,起点所在城市,终点所在城市,0,0)
    return

这样我们只要直接输入起点和目的地就可以自动给我们规划了。

行政区查询

def canton(keywords=None,subdistrict=None,page=None,offset=None,extensions=None,filter=None,callback=None)->dict:
    url = 'https://restapi.amap.com/v3/config/district?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'page':page,
        'offset':offset,
        'extensions':extensions,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
潮州各区 = canton(keywords='潮州',subdistrict='2',extensions='base')
display(json_normalize(潮州各区['districts'][0]['districts'][2]['districts']))
print(潮州各区)

subdistrict参数

规则:设置显示下级行政区级数(行政区级别包括:国家、省/直辖市、市、区/县4个级别)

可选值:0、1、2、3
0:不返回下级行政区;
1:返回下一级行政区;
2:返回下两级行政区;
3:返回下三级行政区;

搜索POI

  • 关键字搜索:通过用POI的关键字进行条件搜索,例如:肯德基、朝阳公园等;同时支持设置POI类型搜索,例如:银行
  • 周边搜索:在用户传入经纬度坐标点附近,在设定的范围内,按照关键字或POI类型搜索;
  • 多边形搜索:在多边形区域内进行搜索
def place_text(keywords,types=None,city=None,citylimit=None,children=None,page=None,extensions='base',sig=None)->dict:
    """获取逆地理编码"""
    url = 'https://restapi.amap.com/v3/place/text?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'types':types,
        'city':city,
        'citylimit':citylimit,
        'children':children,
        'page':page,
        'extensions':extensions,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
潮州菜 = place_text('潮州','餐饮服务;中餐厅;潮州菜',city='潮州市',page=1,children=1,extensions='all')
display(json_normalize(潮州菜["pois"]))

IP查询

def IP(ip=None,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/ip?parameters'
    params={
        'key':key,
        'ip':ip,
        'sip':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

查看ip地址

批量请求

body = {"ops":[{
            "url": "/v3/place/around?offset=10&page=1&key=你的key&location=地理编码&output=json&radius=100000&types=080000"
        },
        {
            "url": "/v3/place/around?offset=10&page=1&key=3f7********d221d&location=116.612468,23.557197&output=json&radius=100000&types=080000"
        },
        {
            "url": "/v3/place/around?offset=10&page=1&key=3f7********d221d&location=116.67107,23.46796&output=json&radius=100000&types=080000"
        },
        {
            "url": "/v3/place/around?offset=10&page=1&key=3f7********d221d&location=116.648127,23.65672&output=json&radius=100000&types=080000"
        }]}
def batch_request():
    url = 'https://restapi.amap.com/v3/batch'
    params={
        'key':key,
    }
    response = requests.post(url,params=params,json=body)
    data = response.json()
    return data
batch_request()

周边搜索

def place_around(location,keywords=None,types=None,city=None,redius=None,sortrule=None,offset=None,page=None,extensions='base',sig=None)->dict:
    url = 'https://restapi.amap.com/v3/place/around?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'location':location,
        'types':types,
        'city':city,
        'redius':redius,
        'sortrule':sortrule,
        'offset':offset,
        'page':page,
        'extensions':extensions,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
place_around(家['geocodes'][0]['location'])['pois']

静态地图

静态地图有两个新朋友

from PIL import Image
from io import BytesIO

引入两个关于图片和图片解析的标准库

def staticmap(location,zoom,size=None,scale=1,markers=None,labels=None,paths=None,traffic=0,page=None,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/staticmap?parameters'
    params={
        'key':key,
        'location':location,
        'zoom':zoom,
        'size':size,
        'scale':scale,
        'markers':markers,
        'labels':labels,
        'paths':paths,
        'traffic':traffic,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = Image.open(BytesIO(response.content))
    return data
staticmap(location=家_location,zoom=16)

(tips:zoom指地图缩放级别,填入1~17)

坐标转换

将用户输入的非高德坐标(GPS坐标、mapbar坐标、baidu坐标)转换成高德坐标。

def coordinate(locations,coordsys='autonavi',sig=None):
    url = 'https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'
    params={
        'key':key,
        'locations':locations,
        'coordsys':coordsys,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
coordinate(家['geocodes'][0]['location'],'gps')

(tips:coordsys填入源原坐标系,包括gps;mapbar;baidu;autonavi)

天气查询

def weather(city,extensions=None):
    url = 'https://restapi.amap.com/v3/weather/weatherInfo?parameters'
    params={
        'key':key,
        'city':city,
#         'extensions':extensions',
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
潮安天气 = weather(家['geocodes'][0]['adcode'])
json_normalize(潮安天气['lives'][0])

输入提示

def tips(keywords=None,type=None,location=None,city=None,citylimit='false',datatype='all',sig=None,callback=None):
    url = 'https://restapi.amap.com/v3/assistant/inputtips?parameters'
    params={
        'key':key,
        'keywords':keywords,
        'type':type,
        'location':location,
        'city':city,
        'citylimit':citylimit,
        'datatype':datatype,
        'sig':sig,
        'output':'json',
        'callback':callback
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
tips_data = tips('肯德基','050301','116.678727,23.463901','潮州市')
tips_data

交通态势

目前支持城市:北京,上海,广州,深圳,宁波,武汉,重庆,成都,沈阳,南京,杭州,长春,常州,大连,东莞,福州,青岛,石家庄,天津,太原,西安,无锡,厦门,珠海,长沙,苏州,金华,佛山,济南,泉州,嘉兴,西宁,惠州,温州,中山,合肥,乌鲁木齐,台州,绍兴,昆明。没有我家不开心

def traffic(location,radius='1000',level=None,extensions=None,sig=None,callback=None):
    url = 'https://restapi.amap.com/v3/traffic/status/circle?parameters'
    params={
        'key':key,
        'level':level,
        'sig':sig,
        'callback':callback,
        'location':location,
        'radius':radius,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
df_traffic = traffic('116.3057764,39.98641364','1500','5')['trafficinfo']
json_normalize(df_traffic)

地理围栏(只做了一半)

可以用来看看我到底在不在围栏里。首先要创建围栏,然后通过自己所在的位置到底在不在围栏里。

# 创建围栏
import requests,json
key='3f77*********d221d'

def creat_wl():
    parameters = {
        "name":"测试围栏",
        "points":"116.670040,23.468340;116.669649,23.466642;116.671216,23.466342;116.672627,23.468448;",
        "enable":"true",
        "repeat": "Mon,Tues,Wed,Thur,Fri,Sat,Sun",
#         "valid_time":"2020-4-23"
    }
    url = 'https://restapi.amap.com/v4/geofence/meta?key=我的key'
    answer = requests.post(url,json=parameters).json()
    return answer
creat_wl()
# points填入3个以上的点的地理编码

此时它会返回

{'data': {'gid': '208684d8-ff1a-4c41-a62a-ffdff10f6d41',
  'id': '0',
  'message': '成功',
  'status': '0'},
 'errcode': 0,
 'errdetail': None,
 'errmsg': 'OK',
 'ext': None}

或者

{'data': {'message': '新增围栏已存在', 'status': '106'},
 'errcode': 0,
 'errdetail': None,
 'errmsg': 'OK',
 'ext': None}

其次我们再进行判断

def is_in(location):
    parameters = {
        'key':'3f773d5e03aae2b57ee77deba62d221d',
        'diu':'869455039541285',
        'locations':location
    }
    url = 'https://restapi.amap.com/v4/geofence/status'
    answer = requests.get(url, parameters).json()
    return answer
is_in('116.67106,23.46796,1587573296')
# locations是(x,y,时间戳)
# 时间戳的获取方法
import time
time.time()

轨迹纠错

不知道怎么用,姑且看看就是了。

JsonObject = [{'x': 116.449429, 'y': 40.014844, 'sp': 4, 'ag': 110, 'tm': 1478831753},
 {'x': 116.449639, 'y': 40.014776, 'sp': 3, 'ag': 110, 'tm': 23},
 {'x': 116.449859, 'y': 40.014716, 'sp': 3, 'ag': 111, 'tm': 33},
 {'x': 116.450074, 'y': 40.014658, 'sp': 3, 'ag': 110, 'tm': 31},
 {'x': 116.450273, 'y': 40.014598, 'sp': 3, 'ag': 111, 'tm': 20}]
def track(x=None,y=None,sp=None,ag=None,tm=None,):
    url = 'https://restapi.amap.com/v4/grasproad/driving'
    params={
        'key':key,
    }
    response = requests.post(url,params=params,json=JsonObject)
    data = response.json()
    return data
track(x=116.449429, y=40.014844,sp=4,ag=110,tm=20)

了解AI、ML

人工智能(简称AI)是通过模拟人类行为,来智能地解决现实问题的各种过程。这些过程包括:学习、分析、计划、感知、推理、校正、语音识别、语言互动、和其他与人类认知科学相关的过程。
机器学习(简称ML)是人工智能的一个分支,其旨在按照既定的步骤向系统馈入各种新的规则和行动参考信息。这些信息能够被系统通过自动学习,来不断地积累经验并实现“自我提升”。同时,那些由程序所生成的算法能够整合各种输入,并产生高效的输出。
简单点一句话:ML是AI的一种实现方法。

高德地图API背后的AI/ML

地理编码、路径规划、行政区域、IP定位、批量请求接口等功能都是通过机器学习数据,分析、推理、校正数据,返回适合的结果。比如路径规划,就是通过导入数据,机器学习、分析,再返回适合用户的路径规划。

设计一款观光app

  • 通过地图规划处最短路径,使得可以用最短的时间游遍最多的景点,不走冤枉路,不走回头路,这需要用到路线规划
  • 同时要通过对交通路线的评估,知道哪里是行车高峰,需要使用到交通态势。
  • 如果走的路线远一点,需要一天时间,可以使用“POI周边搜索”功能查看景点附近有什么落脚点和餐饮店。

你可能感兴趣的:(机器学习)