カスタム空間演算の適用

Pythonで扱う地理空間データ

Joris Van den Bossche

Open source software developer and teacher, GeoPandas maintainer

Pythonで扱う地理空間データ

各都市から50km以内の河川延長の合計は?

単一点(cairo)の場合:

area = cairo.buffer(50000)

rivers_within_area = rivers.intersection(area)
print(rivers_within_area.length.sum() / 1000)
186.397219642
Pythonで扱う地理空間データ

apply() メソッド

Series.apply():Series の各値に関数を適用

Series.apply(function, **kwargs)

  • function:各値に対して呼び出す関数。値は最初の引数として渡される
  • **kwargs:関数へ渡す追加引数

GeoSeries の場合、各 geom に対し function(geom, **kwargs) として呼び出される

Pythonで扱う地理空間データ

カスタム空間演算の適用

適用する関数:

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)
Pythonで扱う地理空間データ

カスタム空間演算の適用

全都市に適用:

cities.geometry.apply(river_length, rivers=rivers)
0        0.000000
1        0.000000
2      106.072198
          ...    
Pythonで扱う地理空間データ

カスタム空間演算の適用

全都市に適用し、結果を新列に代入:

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で扱う地理空間データ

Passons à la pratique !

Pythonで扱う地理空間データ

Preparing Video For Download...