高德地理信息转换和天气API

高德地理信息转换和天气API

https://lbs.amap.com/api/webservice/guide/api/georegeo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    url = r'https://restapi.amap.com/v3/geocode/regeo'
params = {
'key': settings.GD_KEY,
'location': str(longitude) + ',' + str(latitude),
'radius': 100,
'output': 'json'
}
response = requests.get(url=url, params=params)
print(response.json())
province = response.json()['regeocode']['addressComponent']['province']
district = response.json()['regeocode']['addressComponent']['district']
city = response.json()['regeocode']['addressComponent']['city']
location = response.json()['regeocode']['formatted_address']
# 高德地区编码,可用于查询天气等信息
adcode = response.json()['regeocode']['addressComponent']['adcode']
return location, province, city, district, adcode


def gd_weather(adcode, extensions: str = 'base') -> dict:
'''
all是返回格式参考:
{'city': '甘德县', 'adcode': '632623', 'province': '青海', 'reporttime': '2023-03-28 12:19:30', 'casts': [{'date': '2023-03-28', 'week': '2', 'dayweather': '小雪', 'nightweather': '小雪', 'daytemp': '2', 'nighttemp': '-9', 'daywind': '东', 'nightwind': '东', 'daypower': '4', 'nightpower': '≤3', 'daytemp_float': '1.5', 'nighttemp_float': '-8.9'}, {'date': '2023-03-29', 'week': '3', 'dayweather': '多云', 'nightweather': '多云', 'daytemp': '4', 'nighttemp': '-10', 'daywind': '西南', 'nightwind': '西', 'daypower': '4', 'nightpower': '≤3', 'daytemp_float': '3.7', 'nighttemp_float': '-9.6'}, {'date': '2023-03-30', 'week': '4', 'dayweather': '多云', 'nightweather': '阴', 'daytemp': '4', 'nighttemp': '-7', 'daywind': '西', 'nightwind':'西', 'daypower': '≤3', 'nightpower': '4', 'daytemp_float': '4.2', 'nighttemp_float': '-7.1'}, {'date': '2023-03-31', 'week': '5', 'dayweather': '阴', 'nightweather': '多云', 'daytemp': '4', 'nighttemp': '-9', 'daywind': '西', 'nightwind': '西南', 'daypower': '5', 'nightpower': '≤3', 'daytemp_float': '4.0', 'nighttemp_float': '-8.7'}]}
base返回样式参考:
{'province': '青海', 'city': '甘德县', 'adcode': '632623', 'weather': '多云', 'temperature': '-7', 'winddirection': '东', 'windpower': '≤3', 'humidity': '82', 'reporttime': '2023-03-28 12:19:30', 'temperature_float': '-7.0', 'humidity_float': '82.0'}
'''
url = r'https://restapi.amap.com/v3/weather/weatherInfo'
params = {
'key': settings.GD_KEY,
'city': adcode,
'extensions': 'base' if extensions == 'base' else 'all',
'output': 'JSON'
}
response = requests.get(url=url, params=params)
print(response.json())
if extensions == 'base':
return response.json()['lives'][0]
else:
return response.json()['forecasts'][0]


def gd_location_adcode(location:str):
url = r'https://restapi.amap.com/v3/geocode/geo'
params = {
'key': settings.GD_KEY,
'address': location,
'output': 'json'
}
response = requests.get(url=url, params=params)
print(response.json())
# 高德地区编码,可用于查询天气等信息
adcode = response.json()['geocodes'][0]['adcode']
return adcode