当前位置:   article > 正文

用Python历时十多天完成的全国所有区县经纬度采集_沿路采集经纬度

沿路采集经纬度

在高德坐标拾取器(坐标拾取器 | 高德地图API)中,如果输入北京,坐标获取结果为:116.407387,39.904179,这是经纬度坐标,在高德公开的全国所有区县编码表(https://a.amap.com/lbs/static/code_resource/AMap_adcode_citycode.zip)中,有3241行数据,字段分别为中文名,adcode,citycode,其中citycode就相当于电话区号,而adcode比较精确,每个区县是不同的,如果adcode最末位为0,表示前三位和它相同的adcode都是它的下辖区县。

中文名adcodecitycode
中华人民共和国100000
北京市110000010
东城区110101010

不过这张表没有给出经纬度。

可以使用高德开放平台(高德开放平台 | 高德地图API)的API采集经纬度,需要用到的是搜索POI。

搜索POI产品介绍

搜索服务API是一类简单的HTTP接口,提供多种查询POI信息的能力,其中包括关键字搜索、周边搜索、多边形搜索、ID查询四种筛选机制。

使用API前您需先申请Key,若无高德地图API账号需要先申请账号。

注意:在此接口之中,您可以通过city&citylimit参数指定希望搜索的城市或区县。而city参数能够接收citycode和adcode,citycode仅能精确到城市,而adcode却能够精确到区县。

具体参数详见(搜索POI-高级 API 文档-开发指南-Web服务 API | 高德地图API

本次请求的url= 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)

其中city=adcode,keywords=中文名

为了统一请求,提供一个通用的请求函数

  1. def openUrl(_url):
  2. # 设置请求头 request header
  3. headers = {
  4. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
  5. }
  6. response = requests.get(_url, headers=headers)
  7. if response.status_code == 200: # 如果状态码为200,寿命服务器已成功处理了请求,则继续处理数据
  8. return response.content
  9. else:
  10. return None

对每个地点都调用获取经纬度,返回POI的第一个

  1. def get_location(city,keywords):
  2. # return str(Lon,Lat)
  3. _url = 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)
  4. content = openUrl(_url,False)
  5. if content:
  6. res = json.loads(content)
  7. pois = res.get('pois')
  8. if len(pois) > 0:
  9. return pois[0].get('location')
  10. else:
  11. return None
  12. print('request not return 200')
  13. return None

为了能方便存取,先把从高德下载的excel表转存为csv,分别由两个函数存取

  1. def write_csv(filepath, data, head=None):
  2. if head:
  3. data = [head] + data
  4. with open(filepath, mode='w', encoding='UTF-8', newline='') as f:
  5. writer = csv.writer(f)
  6. for i in data:
  7. writer.writerow(i)
  8. def read_csv(filepath):
  9. data = []
  10. if os.path.exists(filepath):
  11. with open(filepath, mode='r', encoding='utf-8') as f:
  12. lines = csv.reader(f) # #此处读取到的数据是将每行数据当做列表返回的
  13. for line in lines:
  14. data.append(line)
  15. return data
  16. else:
  17. print('filepath is wrong:{}'.format(filepath))
  18. return []

最后,构建一个循环来一行一行地采集

  1. # 处理一个地点经纬度后更新csv
  2. def process_lon_lat():
  3. data = read_csv(CSV_PATH)
  4. header = data[0]
  5. data = data[1:]
  6. data_new = []
  7. done = False
  8. for city in data:
  9. if not done and not city[3]:
  10. loc = get_location(city[1],city[0])
  11. if loc:
  12. loc_split = loc.split(',')
  13. print(city,loc_split)
  14. data_new.append([city[0],city[1],city[2],loc_split[0],loc_split[1]])
  15. done = True
  16. else:
  17. data_new.append([city[0],city[1],city[2],'None','None'])
  18. print(city,'None')
  19. else:
  20. data_new.append(city)
  21. write_csv(CSV_PATH, data_new, header)
  22. # 更新csv经纬度主控
  23. def LonLat_update():
  24. data = read_csv(CSV_PATH)
  25. data = data[1:]
  26. for city in data:
  27. process_lon_lat()
  28. time.sleep(6)

由于API的使用配额是每天100次请求这个接口,我不得不使用了两个key来采集,总共历时十多天才完成,每天半小时,采集好的数据样例如下(完整的已传到我的资源中,https://download.csdn.net/download/cnnews/88938675):

中文名adcodecitycodeLonLat
北京市110000010116.40738739.904179
东城区110101010116.41633439.928359
西城区110102010116.3658539.9126

完整代码:

  1. # coding: utf-8
  2. import os
  3. import requests
  4. import csv
  5. import json
  6. import time
  7. CSV_PATH = 'AMap_adcode_citycode.csv'
  8. def openUrl(_url):
  9. # 设置请求头 request header
  10. headers = {
  11. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
  12. }
  13. response = requests.get(_url, headers=headers)
  14. if response.status_code == 200: # 如果状态码为200,寿命服务器已成功处理了请求,则继续处理数据
  15. return response.content
  16. else:
  17. return None
  18. def write_csv(filepath, data, head=None):
  19. if head:
  20. data = [head] + data
  21. with open(filepath, mode='w', encoding='UTF-8', newline='') as f:
  22. writer = csv.writer(f)
  23. for i in data:
  24. writer.writerow(i)
  25. def read_csv(filepath):
  26. data = []
  27. if os.path.exists(filepath):
  28. with open(filepath, mode='r', encoding='utf-8') as f:
  29. lines = csv.reader(f) # #此处读取到的数据是将每行数据当做列表返回的
  30. for line in lines:
  31. data.append(line)
  32. return data
  33. else:
  34. print('filepath is wrong:{}'.format(filepath))
  35. return []
  36. ##################################################################################
  37. # 获取地点经纬度
  38. # {'status': '0', 'info': 'USER_DAILY_QUERY_OVER_LIMIT', 'infocode': '10044'}
  39. def get_location(city,keywords):
  40. # return str(Lon,Lat)
  41. _url = 'https://restapi.amap.com/v3/place/text?city={}&keywords={}&citylimit=true&offset=20&page=1&key=YOURKEY&extensions=all'.format(city,keywords)
  42. content = openUrl(_url,False)
  43. if content:
  44. res = json.loads(content)
  45. pois = res.get('pois')
  46. if len(pois) > 0:
  47. return pois[0].get('location')
  48. else:
  49. return None
  50. print('request not return 200')
  51. return None
  52. # 处理一个地点经纬度后更新csv
  53. def process_lon_lat():
  54. data = read_csv(CSV_PATH)
  55. header = data[0]
  56. data = data[1:]
  57. data_new = []
  58. done = False
  59. for city in data:
  60. if not done and not city[3]:
  61. loc = get_location(city[1],city[0])
  62. if loc:
  63. loc_split = loc.split(',')
  64. print(city,loc_split)
  65. data_new.append([city[0],city[1],city[2],loc_split[0],loc_split[1]])
  66. done = True
  67. else:
  68. data_new.append([city[0],city[1],city[2],'None','None'])
  69. print(city,'None')
  70. else:
  71. data_new.append(city)
  72. write_csv(CSV_PATH, data_new, header)
  73. # 更新csv经纬度主控
  74. def LonLat_update():
  75. data = read_csv(CSV_PATH)
  76. data = data[1:]
  77. for city in data:
  78. process_lon_lat()
  79. time.sleep(6)
  80. if __name__ == '__main__':
  81. LonLat_update()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/677844
推荐阅读
相关标签
  

闽ICP备14008679号