从地址geopandas获取纬度和经度

问题描述 投票:0回答:2

我有大约 1 亿条日志的 csv。其中一列是地址,我试图获取地址的纬度和经度。我想尝试像解决方案中提到的那样,但是给出的解决方案

arcGIS
,这是一个商业工具。我确实尝试了
google API
,它的条目限制只有 2000 个。

将地址的纬度和经度放入大型数据集中的下一个最佳替代方案是什么。

输入:

Site
列是巴黎市的地址

start_time,stop_time,duration,input_octets,output_octets,os,browser,device,langue,site
2016-08-27T16:15:00+05:30,2016-08-27T16:28:00+05:30,721.0,69979.0,48638.0,iOS,CFNetwork,iOS-Device,zh_CN,NULL
2016-08-27T16:16:00+05:30,2016-08-27T16:30:00+05:30,835.0,2528858.0,247541.0,iOS,Mobile Safari UIWebView,iPhone,en_GB,Berges de Seine Rive Gauche - Gros Caillou
2016-08-27T16:16:00+05:30,2016-08-27T16:47:00+05:30,1805.0,133303549.0,4304680.0,Android,Android,Samsung GT-N7100,fr_FR,Centre d'Accueil Kellermann
2016-08-27T16:17:00+05:30,,2702.0,32499482.0,7396904.0,Other,Apache-HttpClient,Other,NULL,Bibliothèque Saint Fargeau
2016-08-27T16:17:00+05:30,2016-08-27T17:07:00+05:30,2966.0,39208187.0,1856761.0,iOS,Mobile Safari UIWebView,iPad,fr_FR,NULL
2016-08-27T16:18:00+05:30,,2400.0,1505716.0,342726.0,NULL,NULL,NULL,NULL,NULL
2016-08-27T16:18:00+05:30,,302.0,3424123.0,208827.0,Android,Chrome Mobile,Samsung SGH-I337M,fr_CA,Square Jean Xxiii
2016-08-27T16:19:00+05:30,,1500.0,35035181.0,1913667.0,iOS,Mobile Safari UIWebView,iPhone,fr_FR,Parc Monceau 1 (Entrée)
2016-08-27T16:19:00+05:30,,6301.0,9227174.0,5681273.0,Mac OS X,AppleMail,Other,fr_FR,Bibliothèque Parmentier

带有 NULL 的地址可以忽略,也可以从输出中删除。

输出应该有以下列

start_time,stop_time,duration,input_octets,output_octets,os,browser,device,langue,site, latitude, longitude

感谢所有的帮助,提前感谢您!!

python pandas latitude-longitude arcgis geopandas
2个回答
4
投票
import csv
from geopy.geocoders import Nominatim

#if your sites are located in France only you can use the country_bias parameters to restrict search
geolocator = Nominatim(country_bias="France")

with open('c:/temp/input.csv', 'rb') as csvinput:
    with open('c:/temp/output.csv', 'wb') as csvoutput:
       output_fieldnames = ['Site', 'Address_found', 'Latitude', 'Longitude']
       writer = csv.DictWriter(csvoutput, delimiter=';', fieldnames=output_fieldnames)
       writer.writeheader()
       reader = csv.DictReader(csvinput)
       for row in reader:
            site = row['site']
            if site != "NULL":
                try:
                    location = geolocator.geocode(site)
                    address = location.address
                    latitude = location.latitude
                    longitude = location.longitude
                except:
                    address = 'Not found'
                    latitude = 'N/A'
                    longitude = 'N/A'
            else:
                address = 'N/A'
                latitude = 'N/A'
                longitude = 'N/A'

            #here is the writing section
            output_row = {}
            output_row['Site'] = row['site']
            output_row['Address_found'] = address.encode("utf-8")
            output_row['Latitude'] = latitude
            output_row['Longitude'] = longitude
            writer.writerow(output_row)

0
投票

现在直接将其烘焙到 Geopandas 中:https://geopandas.org/en/stable/docs/user_guide/geocoding.html

直接取自他们的文档:

import geodatasets

boros = geopandas.read_file(geodatasets.get_path("nybb"))

boros.BoroName
Out[3]: 
0    Staten Island
1           Queens
2         Brooklyn
3        Manhattan
4            Bronx
Name: BoroName, dtype: object

boro_locations = geopandas.tools.geocode(boros.BoroName)

boro_locations
Out[5]: 
                     geometry                                           address
0  POINT (-74.14960 40.58346)  Staten Island, New York, New York, United States
1  POINT (-73.82831 40.71351)         Queens, New York, New York, United States
2  POINT (-73.94972 40.65260)       Brooklyn, New York, New York, United States
3  POINT (-73.95989 40.78962)      Manhattan, New York, New York, United States
4  POINT (-73.87859 40.84665)      The Bronx, New York, New York, United States
© www.soinside.com 2019 - 2024. All rights reserved.