在 Python 中处理地理空间数据
Joris Van den Bossche
Open source software developer and teacher, GeoPandas maintainer

针对单个点(cairo):
area = cairo.buffer(50000)rivers_within_area = rivers.intersection(area)print(rivers_within_area.length.sum() / 1000)
186.397219642
Series.apply(): 对 Series 的每个值调用函数
Series.apply(function, **kwargs)
function:要调用的函数;该值作为第一个参数传入**kwargs:传给函数的其他参数对于 GeoSeries,函数以 function(geom, **kwargs) 形式对每个 geom 调用
要应用的函数:
def river_length(geom, rivers):
area = geom.buffer(50000)
rivers_within_area = rivers.intersection(area)
return rivers_within_area.length.sum() / 1000
对单个几何体调用:
river_length(cairo, rivers=rivers)
186.3972196423455
对所有城市应用:
cities.geometry.apply(river_length, rivers=rivers)
对所有城市应用:
cities.geometry.apply(river_length, rivers=rivers)
0 0.000000
1 0.000000
2 106.072198
...
对所有城市应用并将结果赋给新列:
cities['river_length'] = cities.geometry.apply(river_length, rivers=rivers)
cities.head()
name geometry river_length
0 Vatican City POINT (1386304.6 5146502.5) 0.000000
1 San Marino POINT (1385011.5 5455558.1) 0.000000
2 Vaduz POINT (1059390.7 5963928.5) 106.072198
.. ... ... ...
在 Python 中处理地理空间数据