通過百度地圖API--獲取全國地圖的經緯度


因為要做一個前端畫圖需要經緯度,一個個的查詢過麻煩,最終弄出這個,以備后查!

 

  1 import threading , time
  2 import requests
  3 from decimal import Decimal
  4 #爬取數據
  5 def hq(address,name_id):
  6     url = 'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=' + str(address)
  7     response = requests.get(url)
  8     answer = response.json()
  9     print('得到反解數據', answer)
 10     lng = answer['result']['location']['lng']
 11     lat = answer['result']['location']['lat']
 12     formatted_address = answer['result']['formatted_address']
 13     business = answer['result']['business']
 14     city = answer['result']['addressComponent']['city']
 15     direction = answer['result']['addressComponent']['city']
 16     distance = answer['result']['addressComponent']['direction']
 17     district = answer['result']['addressComponent']['district']
 18     province = answer['result']['addressComponent']['province']
 19     street = answer['result']['addressComponent']['street']
 20     street_number = answer['result']['addressComponent']['street_number']
 21     cityCode = answer['result']['cityCode']
 22     lin_list = str('%6f' % lng) + '|' + str('%6f' % lat) + '|' + str(formatted_address) + '|' + str(business) + '|' + str(
 23         city) + '|' + str(direction) + '|' + str(distance) + '|' + str(district) + '|' + str(province) + '|' + str(
 24         street) + '|' + str(street_number) + '|' + str(cityCode)
 25     if cityCode==0:
 26         #print('外國')
 27         pass
 28     else:
 29    
 30         name=str(name_id)+'list'
 31         print(name)
 32         with open(name, 'a+', encoding=('utf-8')) as f:
 33             f.write(lin_list+'\n')
 34         print('文件寫入完成')
 35 
 36 #經度緯度處理
 37 def longitude_proces(longitude, interval, latitude, latitude_end, name_id):
 38     while longitude >= latitude:
 39         address = '%s,%s' % (longitude, latitude_end)  # 請求時,經度,緯度,需要互換
 40         hq(address, name_id)
 41         longitude -= interval
 42 
 43 
 44 class Thre(threading.Thread):#繼承線程中的類
 45     def __init__(self,lists,interval,name_id,times):
 46         super(Thre,self).__init__()
 47         self.interval=interval
 48         self.lists=lists
 49         self.name_id=name_id
 50         self.times=times
 51     def run(self):
 52         print('執行線程開始時間:',self.times,self.lists,'==================start=============================================')
 53         slog,elog,slat,elat=self.lists
 54         #print(slog,elog,slat,elat)
 55         longitude = Decimal(slog)#經度longitude開始
 56         longitude_end = Decimal(elog)#經度結束
 57         latitude=Decimal(slat)# 緯度latitude開始
 58         latitude_end=Decimal(elat)#緯度結束
 59         
 60         while latitude>=latitude_end:
 61             latitude -= self.interval
 62             longitude_proces(longitude,self.interval,longitude_end,latitude,self.name_id)
 63         else:
 64             den_time=time.time()-self.times
 65             print('執行線程所用時間:',den_time,self.lists,'==================end=============================================')
 66         
 67 
 68 
 69 def main():
 70     itude_list=[
 71         ['42.000000', '30.000000', '105.000000', '79.800000'],  # 
 72         ['42.000000', '21.000000', '129.000000', '105.000000'],  # 
 73         ['50.000000', '42.000000', '135.000000','129.000000'],  #
 74         ['54.000000', '42.000000', '129.000000', '115.000000'],  #
 75         ['45.500000', '42.000000', '115.000000', '105.000000'],  #
 76         ['49.200000', '42.000000', '91.500000', '79.800000'],  # 
 77         ['42.000000', '30.000000', '79.800000', '73.400000'],  # 
 78         ['45.500000', '42.000000', '105.000000', '91.500000'],  #
 79         ['30.000000', '21.000000', '105.000000', '97.300000'],  # 
 80         ['42.000000', '30.000000', '97.300000', '79.800000'],  #
 81         ['21.000000', '3.000000', '129.000000', '105.000000'],  # (南海)
 82     ]
 83 
 84     interval = Decimal('3.0001000')#間隔
 85     number = 0
 86     thre_list=[]
 87 
 88     for itude in itude_list:
 89         start_times=time.time()
 90         number += 1
 91         temp=Thre(itude_list,interval,number,start_times)
 92         thre_list.append(temp)
 93         
 94     for thre in thre_list:
 95         thre.start()
 96         
 97 
 98 
 99 if __name__ == '__main__':
100     main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM