mcpField
A
alterlab-geopandas

alterlab-geopandas

github.com/alterlab-ieuデータ・データベース7866 回インストール5 週間前に更新
リポジトリ

alterlab-geopandas について

alterlab-geopandasは、GeoPandasを使った地理空間分析向けのスキルです。ベクターデータの入出力、空間演算、座標参照系の管理、地図作成を扱います。

主な機能

  1. 01複数形式のベクターデータ入出力
  2. 02空間結合とオーバーレイ分析
  3. 03座標参照系の管理と再投影
  4. 04静的・インタラクティブ地図の作成

活用シーン

  1. 01ShapefileやGeoJSONなどの変換
  2. 02点とポリゴンの空間結合
  3. 03適切な座標系での面積・距離測定
  4. 04ベクターデータから主題図を作成

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/alterlab-ieu/alterlab-academic-skills --skill alterlab-geopandas

インストール後、Claude Code で/alterlab-geopandasを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

evals/
evals.json
references/
crs-management.md
data-io.md
data-structures.md
geometric-operations.md
spatial-analysis.md
visualization.md

詳細

GeoPandas

GeoPandas は pandas を拡張し、幾何学型に対する空間操作を可能にします。地理空間データ分析のために、pandas と shapely の機能を組み合わせています。

このスキルを使用する場面

次のことをユーザーが希望する場合に、このスキルを使用します。

  • ベクターデータ(Shapefile、GeoJSON、GeoPackage、GeoParquet、PostGIS)の読み込み、書き込み、変換
  • 空間結合、オーバーレイ、バッファー、ディゾルブ、クリッピング、最近傍結合の実行
  • 座標参照系間の再投影、および面積・長さ・距離の正確な計測
  • 表形式のベクターデータから静的またはインタラクティブ(folium)なコロプレスマップを作成
該当しない場合
シナリオ代わりに使用
ラスター、衛星画像、DEM、スペクトル指数、STAC/COG、または地球観測(EO)機械学習alterlab-geomaster
ジオメトリを持たない関係性のネットワーク/グラフ分析(中心性、コミュニティ)alterlab-networkx
ゲノム区間の重なり(BED ファイル)—染色体に沿った「空間」結合alterlab-gtars
天球座標、FITS/WCS の天体位置alterlab-astropy

インストール

uv pip install geopandas   # 1.x (current 1.1.x as of 2026-09); shapely 2 + pyogrio

GeoPandas 1.x では shapely ≥ 2 が必要で、デフォルトでは pyogrio 経由で読み書きします (fiona はオプションです)。1.x 系で削除または非推奨となった機能:sjoin(op=...)(predicate= を使用)、 unary_union(union_all() を使用)、同梱の geopandas.datasets ファイル(geodatasets パッケージを使用)。

オプションの依存関係
# For interactive maps
uv pip install folium

# For classification schemes in mapping
uv pip install mapclassify

# For faster I/O operations (2-4x speedup)
uv pip install pyarrow

# For PostGIS database support (SQLAlchemy engine + driver; geoalchemy2 for to_postgis)
uv pip install sqlalchemy "psycopg[binary]" geoalchemy2

# For basemaps
uv pip install contextily

# For cartographic projections
uv pip install cartopy

クイックスタート

import geopandas as gpd

# Read spatial data
gdf = gpd.read_file("data.geojson")

# Basic exploration
print(gdf.head())
print(gdf.crs)
print(gdf.geometry.geom_type)

# Simple plot
gdf.plot()

# Reproject to a metric CRS suited to the data (local UTM zone)
gdf_projected = gdf.to_crs(gdf.estimate_utm_crs())

# Calculate area in m² (use an equal-area CRS such as EPSG:6933 for continental extents;
# never Web Mercator EPSG:3857 — it inflates areas ~4x at 60° latitude)
gdf_projected['area'] = gdf_projected.geometry.area

# Save to file
gdf.to_file("output.gpkg")

基本概念

データ構造
  • GeoSeries:空間操作が可能なジオメトリのベクトル
  • GeoDataFrame:ジオメトリ列を持つ表形式のデータ構造

詳しくは、data-structures.md を参照してください。

データの読み込みと書き込み

GeoPandas は複数の形式に対応しています。Shapefile、GeoJSON、GeoPackage、PostGIS、Parquet の読み書きが可能です。

# Read with filtering
gdf = gpd.read_file("data.gpkg", bbox=(xmin, ymin, xmax, ymax))

# Write with Arrow acceleration
gdf.to_file("output.gpkg", use_arrow=True)

包括的な入出力操作については、data-io.md を参照してください。

座標参照系

正確な空間操作のため、CRS を必ず確認し、適切に管理してください。

# Check CRS
print(gdf.crs)

# Reproject (transforms coordinates). EPSG:3857 (Web Mercator) is for web tiles only;
# measure in a local UTM (gdf.estimate_utm_crs()) or an equal-area CRS
gdf_projected = gdf.to_crs("EPSG:3857")

# Set CRS (only when metadata missing)
gdf = gdf.set_crs("EPSG:4326")

CRS の操作については、crs-management.md を参照してください。

一般的な操作

幾何操作

バッファー、簡略化、重心、凸包、アフィン変換:

# Buffer by 10 units
buffered = gdf.geometry.buffer(10)

# Simplify with tolerance
simplified = gdf.geometry.simplify(tolerance=5, preserve_topology=True)

# Get centroids
centroids = gdf.geometry.centroid

すべての操作については、geometric-operations.md を参照してください。

空間分析

空間結合、オーバーレイ操作、ディゾルブ:

# Spatial join (intersects)
joined = gpd.sjoin(gdf1, gdf2, predicate='intersects')

# Nearest neighbor join
nearest = gpd.sjoin_nearest(gdf1, gdf2, max_distance=1000)

# Overlay intersection
intersection = gpd.overlay(gdf1, gdf2, how='intersection')

# Dissolve by attribute
dissolved = gdf.dissolve(by='region', aggfunc='sum')

分析操作については、spatial-analysis.md を参照してください。

可視化

静的およびインタラクティブなマップを作成します。

# Choropleth map
gdf.plot(column='population', cmap='YlOrRd', legend=True)

# Interactive map
gdf.explore(column='population', legend=True).save('map.html')

# Multi-layer map
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
gdf1.plot(ax=ax, color='blue')
gdf2.plot(ax=ax, color='red')

マッピング手法については、visualization.md を参照してください。

詳細ドキュメント

  • データ構造 - GeoSeries と GeoDataFrame の基礎
  • データ入出力 - ファイル、PostGIS、Parquet の読み書き
  • 幾何操作 - バッファー、簡略化、アフィン変換
  • 空間分析 - 結合、オーバーレイ、ディゾルブ、クリッピング
  • 可視化 - プロット、コロプレスマップ、インタラクティブマップ
  • CRS 管理 - 座標参照系と投影法

一般的なワークフロー

読み込み、変換、分析、エクスポート
# 1. Load data
gdf = gpd.read_file("data.shp")

# 2. Check and transform CRS (metric, local UTM zone)
print(gdf.crs)
gdf = gdf.to_crs(gdf.estimate_utm_crs())

# 3. Perform analysis
gdf['area'] = gdf.geometry.area
buffered = gdf.copy()
buffered['geometry'] = gdf.geometry.buffer(100)

# 4. Export results
gdf.to_file("results.gpkg", layer='original')
buffered.to_file("results.gpkg", layer='buffered')
空間結合と集計
# Join points to polygons
points_in_polygons = gpd.sjoin(points_gdf, polygons_gdf, predicate='within')

# Aggregate by polygon (named aggregation: total value and number of points)
aggregated = points_in_polygons.groupby('index_right').agg(
    value_sum=('value', 'sum'),
    n_points=('value', 'size'),
)

# Merge back to polygons
result = polygons_gdf.merge(aggregated, left_index=True, right_index=True)
複数ソースのデータ統合
# Read from different sources
roads = gpd.read_file("roads.shp")
buildings = gpd.read_file("buildings.geojson")
parcels = gpd.read_postgis("SELECT * FROM parcels", con=engine, geom_col='geom')

# Ensure matching CRS
buildings = buildings.to_crs(roads.crs)
parcels = parcels.to_crs(roads.crs)

# Perform spatial operations
buildings_near_roads = buildings[buildings.geometry.distance(roads.union_all()) < 50]

パフォーマンスのヒント

  1. 空間インデックスを使用する:GeoPandas はほとんどの操作で空間インデックスを自動的に作成します
  2. 読み込み時にフィルタリングする:bbox、mask、または where パラメーターを使い、必要なデータだけを読み込みます
  3. 入出力に Arrow を使用する:use_arrow=True を追加すると、読み書きが 2~4 倍高速になります
  4. ジオメトリを簡略化する:精度が重要でない場合は .simplify() を使って複雑さを軽減します
  5. バッチ処理する:ベクトル化された操作は、行を反復処理するよりも大幅に高速です
  6. 適切な CRS を使用する:面積には等積 CRS、距離やバッファーにはローカル UTM、保存には地理座標系を使用します

ベストプラクティス

  1. 空間演算の前に、必ずCRSを確認してください
  2. 計測には適切な投影CRSを使用してください。面積には等積図法(EPSG:6933や地域のアルバース図法など)、距離にはestimate_utm_crs()を使用し、EPSG:3857は使用しないでください
  3. 空間結合やオーバーレイの前に、CRSを一致させてください
  4. 演算の前に.is_validでジオメトリを検証してください
  5. 副作用を避けるため、ジオメトリ列を変更するときは.copy()を使用してください
  6. 解析用に簡略化する際は、トポロジーを保持してください
  7. 最新のワークフローでは、GeoPackage形式を使用してください(Shapefileより優れています)
  8. パフォーマンスを向上させるため、sjoin_nearestでmax_distanceを設定してください

AlterLab Academic Skillsスイートの一部です。

コメント