LoginSignup
37
35

More than 5 years have passed since last update.

geopandasを使ってみる

Last updated at Posted at 2016-08-30

これなに

PyData.Tokyo Meetup #9 -「地理情報データ」で紹介されていた geopandasStatsFragments を参考に試してみました。

インストール

Anaconda入れて、Anaconda cloud の conda-forge 使えば簡単。下記のコマンド一発1

bash
conda install -y geopandas -c conda-forge

Dockerイメージtsutomu7/geopandasも用意しました。下記のようにすればできるはず。(Jupyterサーバー起動後にブラウザを更新してください)

bash
firefox http://localhost:8888 &
docker run -it --rm -p 8888:8888 tsutomu7/geopandas sh -c "jupyter notebook --ip=*"

Jupyterで試してみる

準備。flattenは、配列の配列を配列にするもの。

python3
%matplotlib inline
import numpy as np, pandas as pd, geopandas as gpd
from bokeh.plotting import output_notebook, show, figure
output_notebook()
flatten = lambda i: [a for b in i for a in (flatten(b) if hasattr(b,'__iter__') else (b,))]

geopandas のバージョンは、0.2.1 で現時点で最新。

python3
gpd.__version__
>>>
'0.2.1'

東京のデータを地球地図日本からダウンロード。

python3
!wget --no-check-certificate https://github.com/dataofjapan/land/raw/master/tokyo.geojson

最初の3行を見てみる。

python3
df = gpd.read_file('tokyo.geojson')
df[:3]
area_en area_ja code geometry ward_en ward_ja
0 Tokubu 都区部 131211.0 POLYGON ((139.821051 35.815077, 139.821684 35.... Adachi Ku 足立区
1 Tokubu 都区部 131059.0 POLYGON ((139.760933 35.732206, 139.761002 35.... Bunkyo Ku 文京区
2 Tokubu 都区部 131016.0 POLYGON ((139.770135 35.705352, 139.770172 35.... Chiyoda Ku 千代田区

geometry にポリゴンデータが入っています。
matplotlib で描画。

python3
df[df['area_en'] == 'Tokubu'].plot();

image

今度は、bokehで描画。
geometryのポリゴンは、shapely.geometry.polygon.Polygon と shapely.geometry.multipolygon.MultiPolygon が混じっているので、flattenでPolygonの配列にします。

python3
xy = [i.exterior.coords.xy for i in flatten(df[df.area_en == 'Tokubu'].geometry)]
p = figure(plot_width=400, plot_height=300)
p.patches([tuple(i[0]) for i in xy], [tuple(i[1]) for i in xy], 
          fill_color='white', line_color="black", line_width=0.5)
show(p);

image

Geocodingはうまくいきませんでした。

参考リンク

以上


  1. "conda install -y pyproj shapely fiona; pip install geopandas" でもできました。 

37
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
35