geopandas
supports geocoding (i.e., converting place names to location on Earth) through geopy, an optional dependency of geopandas
. The following example shows how to get the locations of boroughs in New York City, and plots those locations along with the detailed borough boundary file included within geopandas
.
geopandas 通过 geopy 支持地理编码(即,将地名转换为地球上的位置),这是 geopandas 的可选依赖项。以下示例展示了如何获取纽约市行政区的位置,并绘制这些位置以及 geopandas 中包含的详细行政区边界文件。
In [1]: boros = geopandas.read_file(geopandas.datasets.get_path("nybb"))
In [2]: boros.BoroName
Out[2]:
0 Staten Island
1 Queens
2 Brooklyn
3 Manhattan
4 Bronx
Name: BoroName, dtype: object
In [3]: boro_locations = geopandas.tools.geocode(boros.BoroName)
In [4]: boro_locations
Out[4]:
geometry address
0 POINT (-74.149604800 40.583455700) Staten Island, City of New York, New York, Uni...
1 POINT (144.584490300 -22.164678200) Queensland, Australia
2 POINT (-73.949721100 40.652600600) Brooklyn, City of New York, New York, United S...
3 POINT (-73.959893900 40.789623900) Manhattan, City of New York, New York, United ...
4 POINT (-73.878593700 40.846650800) The Bronx, City of New York, New York, United ...
In [5]: import matplotlib.pyplot as plt
In [6]: fig, ax = plt.subplots()
In [7]: boros.to_crs("EPSG:4326").plot(ax=ax, color="white", edgecolor="black");
In [8]: boro_locations.plot(ax=ax, color="red");
By default, the geocode()
function uses the Photon geocoding API. But a different geocoding service can be specified with the provider
keyword.
默认情况下,geocode() 函数使用 Photon 地理编码 API。但是可以使用 provider 关键字指定不同的地理编码服务。
The argument to provider
can either be a string referencing geocoding services, such as 'google'
, 'bing'
, 'yahoo'
, and 'openmapquest'
, or an instance of a Geocoder
from geopy
. See geopy.geocoders.SERVICE_TO_GEOCODER
for the full list. For many providers, parameters such as API keys need to be passed as **kwargs
in the geocode()
call.
provider 的参数可以是引用地理编码服务的字符串,例如“google”、“bing”、“yahoo”和“openmapquest”,也可以是来自 geopy 的 Geocoder 实例。有关完整列表,请参阅 geopy.geocoders.SERVICE_TO_GEOCODER。对于许多提供程序,API 密钥等参数需要在 geocode() 调用中作为 **kwargs 传递。
For example, to use the OpenStreetMap Nominatim geocoder, you need to specify a user agent:
例如,要使用 OpenStreetMap Nominatim 地理编码器,需要指定一个用户代理:
geopandas.tools.geocode(boros.BoroName, provider='nominatim', user_agent="my-application")
Attention:
Please consult the Terms of Service for the chosen provider. The example above uses 'photon'
(the default), which expects fair usage – extensive usage will be throttled. (Photon’s Terms of Use).
请查阅所选供应商的服务条款。上面的例子使用 “Photon”(默认),它期望合理使用,大量使用将会被限制。(Photon的使用条款)。