/usr/lib/python2.7/dist-packages/geopandas/geocode.py is in python-geopandas 0.1.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | from collections import defaultdict
import time
from fiona.crs import from_epsg
import numpy as np
import pandas as pd
from shapely.geometry import Point
from six import iteritems
import geopandas as gpd
def _throttle_time(provider):
""" Amount of time to wait between requests to a geocoding API.
Currently implemented for Nominatim, as their terms of service
require a maximum of 1 request per second.
https://wiki.openstreetmap.org/wiki/Nominatim_usage_policy
"""
if provider == 'nominatim':
return 1
else:
return 0
def geocode(strings, provider='googlev3', **kwargs):
"""
Geocode a set of strings and get a GeoDataFrame of the resulting points.
Parameters
----------
strings : list or Series of addresses to geocode
provider : geopy geocoder to use, default 'googlev3'
Some providers require additional arguments such as access keys
See each geocoder's specific parameters in geopy.geocoders
* googlev3, default
* bing
* google
* yahoo
* mapquest
* openmapquest
Ensure proper use of the results by consulting the Terms of Service for
your provider.
Geocoding requires geopy. Install it using 'pip install geopy'. See also
https://github.com/geopy/geopy
Example
-------
>>> df = geocode(['boston, ma', '1600 pennsylvania ave. washington, dc'])
address geometry
0 Boston, MA, USA POINT (-71.0597731999999951 42.3584308000000007)
1 1600 Pennsylvania Avenue Northwest, President'... POINT (-77.0365122999999983 38.8978377999999978)
"""
import geopy
from geopy.geocoders.base import GeocoderQueryError
if not isinstance(strings, pd.Series):
strings = pd.Series(strings)
# workaround changed name in 0.96
try:
Yahoo = geopy.geocoders.YahooPlaceFinder
except AttributeError:
Yahoo = geopy.geocoders.Yahoo
coders = {'googlev3': geopy.geocoders.GoogleV3,
'bing': geopy.geocoders.Bing,
'yahoo': Yahoo,
'mapquest': geopy.geocoders.MapQuest,
'openmapquest': geopy.geocoders.OpenMapQuest,
'nominatim' : geopy.geocoders.Nominatim}
if provider not in coders:
raise ValueError('Unknown geocoding provider: {0}'.format(provider))
coder = coders[provider](**kwargs)
results = {}
for i, s in iteritems(strings):
try:
results[i] = coder.geocode(s)
except (GeocoderQueryError, ValueError):
results[i] = (None, None)
time.sleep(_throttle_time(provider))
df = _prepare_geocode_result(results)
return df
def _prepare_geocode_result(results):
"""
Helper function for the geocode function
Takes a dict where keys are index entries, values are tuples containing:
(address, (lat, lon))
"""
# Prepare the data for the DataFrame as a dict of lists
d = defaultdict(list)
index = []
for i, s in iteritems(results):
address, loc = s
# loc is lat, lon and we want lon, lat
if loc is None:
p = Point()
else:
p = Point(loc[1], loc[0])
if address is None:
address = np.nan
d['geometry'].append(p)
d['address'].append(address)
index.append(i)
df = gpd.GeoDataFrame(d, index=index)
df.crs = from_epsg(4326)
return df
|