In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Python how to obtain longitude and latitude data of major map platforms". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to obtain longitude and latitude data of major map platforms by Python".
1. Tencent Map
First, we need to register with the Tencent location service platform and get a key as the key for location data calls. The steps are as follows:
1. Constructor
After obtaining the key, we can construct the API data request function tengxun () to parse the latitude and longitude in the data in json format. The Python code is as follows:
Import requestsdef tengxun (addr): url = "https://apis.map.qq.com/jsapi?" # Tencent Map API interface para = {" qt ":" geoc "," addr ": addr, # pass in the address parameter" output ":" json "," key ":" D7EBZ-NHYKX-UAH4A-74TW4-6M2JE-UHFLY ", # that is, Tencent Map API's key" pf ":" jsapi " "ref": "jsapi"} req = requests.get (url,para) # request data req = req.json () # convert to json format # print (req) m = req ["detail"] g = f "{m ['pointx']}, {m [' pointy']}" # parse to longitude and latitude data print (g) return gtengxun (addr= "Shenzhen")
Input the independent variable "Shenzhen" and run the Python code to obtain the longitude and latitude data of Shenzhen:
'113.883080, 22.5532902.Reading data
After the function is constructed, import the prepared excel file, which contains the address data of 500 schools in Guangzhou. It can be obtained automatically by replying to the school in the public background of "vegetable J learn Python".
Import pandas as pddf1 = pd.read_excel ("gz_school.xlsx") df1.head ()
Data Preview:
3. Application function
The address column in the school data is applied to the function constructed above, and the longitude and latitude data of 500 schools are obtained in batches and timed by the program.
Import timetime_start = time.time () # Program start time df1 ['longitude and latitude'] = df1 ['address'] .apply (tengxun) # call function time_end = time.time () # Program end time t = time_end-time_start # run time print (' shared% s seconds'% t)
It takes about 52.40 seconds for Tencent Map to parse 500 addresses in batch to obtain longitude and latitude data, and the longitude and latitude of 9 addresses can be obtained in an average of 1 second.
When sharing 52.39904499053955 seconds 4, save data
After obtaining the longitude and latitude data, save it as an excel file.
Df1.head () df1.to_excel ('result.xlsx',index = False)
Data Preview:
Amap open platform
1. Constructor import pandas as pdimport requestsimport timeimport csvimport jsondef gaode (addr): para = {'key':' your own', # the key' address':addr # incoming address parameter applied by Amap for open platform} url = 'https://restapi.amap.com/v3/geocode/geo?' # Amap API interface req = requests.get (url) Para) req = req.json () print ('-'* 30) m = req ['geocodes'] [0] [' location'] print (m) return mgaode (addr= "Shenzhen") 2. Apply the function df2 = pd.read_excel ('gz_school.xlsx') # to read address data time_start = time.time () df2 [' latitude and longitude'] = df2 ['address'] .apply (gaode) # call function time_end = time.time () t = time_end-time_startprint (' shared% s seconds'% t)
It takes about 37.74 seconds for Amap to parse 500 addresses in batch to obtain longitude and latitude data, and the average longitude and latitude of 13 addresses can be obtained in 1 second. The resolution speed is higher than Tencent Map.
Shared time 37.740272998809814 seconds, Baidu map
Baidu map batch acquisition longitude and latitude method is consistent with Amap.
Baidu Map Open platform
1. Constructor import pandas as pdimport requestsimport timeimport csvimport jsondef baidu (addr): url = "http://api.map.baidu.com/geocoding/v3/?" # Baidu Map API Interface para = {" address ": addr, # pass in address parameters" output ":" json "," ak ":" your own "# Baidu Map Open platform Application ak} req = requests.get (url Para) req = req.json () # print (req) print ('-'* 30) m = req ["result"] ["location"] g = f "{m ['lng']} {m ['lat']} "print (g) return gbaidu (addr=" Shenzhen ") 2, application function df3 = pd.read_excel (' gz_school.xlsx') time_start = time.time () df3 ['longitude and latitude'] = df3 ['address'] .apply (baidu) time_end = time.time () t = time_end-time_startprint (' shared% s seconds'% t)
It takes about 24.06 seconds to obtain longitude and latitude data by parsing 500 addresses in batch through Baidu map, and the longitude and latitude of 20 addresses can be obtained in an average of 1 second, which is faster than Amap and Tencent Map.
Sharing time 24.0550799369812 seconds thank you for reading, the above is the content of "how Python obtains longitude and latitude data of major map platforms". After the study of this article, I believe you have a deeper understanding of how Python obtains longitude and latitude data of major map platforms, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.