First, we load the countries and cities example datasets and select :
首先,我们加载国家和城市示例数据集并进行筛选:
In [33]: world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
In [34]: capitals = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))
# Select South America and some columns
In [35]: countries = world[world['continent'] == "South America"]
In [36]: countries = countries[['geometry', 'name']]
# Project to crs that uses meters as distance measure
In [37]: countries = countries.to_crs('epsg:3395')
In [38]: capitals = capitals.to_crs('epsg:3395')
To illustrate the overlay()
method, consider the following case in which one wishes to identify the “core” portion of each country – defined as areas within 500km of a capital – using a GeoDataFrame
of countries and a GeoDataFrame
of capitals.
为了说明 overlay() 方法,请考虑以下情况,其中人们希望使用国家 GeoDataFrame 和首都 GeoDataFrame 来识别每个国家的“核心”部分——定义为首都 500 公里以内的区域。
# Look at countries:
In [39]: countries.plot();
# Now buffer cities to find area within 500km.
# Check CRS -- World Mercator, units of meters.
In [40]: capitals.crs
Out[40]:
<Derived Projected CRS: EPSG:3395>
Name: WGS 84 / World Mercator
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- name: World between 80°S and 84°N.
- bounds: (-180.0, -80.0, 180.0, 84.0)
Coordinate Operation:
- name: World Mercator
- method: Mercator (variant A)
Datum: World Geodetic System 1984 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
# make 500km buffer
In [41]: capitals['geometry']= capitals.buffer(500000)
In [42]: capitals.plot();
To select only the portion of countries within 500km of a capital, we specify the how
option to be “intersect”, which creates a new set of polygons where these two layers overlap:
为了只选择首都500公里范围内的国家部分,我们指定如何选项为 “相交”,这将在这两层重叠的地方创建一个新的多边形集。
In [43]: country_cores = countries.overlay(capitals, how='intersection')
In [44]: country_cores.plot(alpha=0.5, edgecolor='k', cmap='tab10');
Changing the “how” option allows for different types of overlay operations. For example, if we were interested in the portions of countries far from capitals (the peripheries), we would compute the difference of the two.
改变 “how “选项可以进行不同类型的叠加操作。例如,如果我们对远离首都的国家部分(边缘地区)感兴趣,我们将计算两者的差值。
In [45]: country_peripheries = countries.overlay(capitals, how='difference')
In [46]: country_peripheries.plot(alpha=0.5, edgecolor='k', cmap='tab10');