在 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 而言,會對每個 geom 以 function(geom, **kwargs) 方式呼叫
要套用的函式:
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 中處理地理空間資料