-->

convert county state names to coordinates

2019-08-25 09:09发布

问题:

I want to convert county state name to coordinates.

county:

fips    state_fips  county_fips state   county
1000    1   0   Alabama Alabama
1005    1   5   Alabama Barbour County
1007    1   7   Alabama Bibb County
1009    1   9   Alabama Blount County
1011    1   11  Alabama Bullock County
6085    6   85  California  Santa Clara County
6089    6   89  California  Shasta County
6091    6   91  California  Sierra County
32021   32  21  Nevada  Mineral County
32023   32  23  Nevada  Nye County
32027   32  27  Nevada  Pershing County
32029   32  29  Nevada  Storey County

I want to use python geopy package.

from geopy.geocoders import Nominatim
import pandas as pd
import numpy as np
geolocator = Nominatim()
county['coord'] = county['county'].apply(geolocator.geocode)

I got the error:

socket.timeout: timed out
During handling of the above exception, another exception occurred:
urllib.error.URLError: <urlopen error timed out>
During handling of the above exception, another exception occurred:
geopy.exc.GeocoderTimedOut: Service timed out

How should I fix it? thanks

回答1:

from time import sleep
from geopy.geocoders import Nominatim
import pandas as pd
import numpy as np

geolocator = Nominatim()

def geocode_with_sleep(query):
    sleep(1)
    return geolocator.geocode(query)

county['coord'] = county['county'].apply(geocode_with_sleep)

Another way is described in http://www.jackmaney.com/2015/01/09/geocoding-rate-limited-queue/



标签: python geopy