[PYTHON] Try using geopandas

what is this

StatsFragments instead of ** geopandas ** introduced in PyData.Tokyo Meetup # 9-"Geographic Information Data" I tried it with reference to .hatenablog.com/entry/2015/07/18/215951).

Installation

It's easy if you put Anaconda and use conda-forge of Anaconda cloud. One command below [^ 1].

bash


conda install -y geopandas -c conda-forge

I also prepared a Docker image tsutomu7 / geopandas. You should be able to do it as follows. (Please refresh your browser after starting the Jupyter server)

bash


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

Try it with Jupyter

Preparation. flatten is an array of arrays.

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,))]

The version of geopandas is 0.2.1, which is the latest at this time.

python3


gpd.__version__
>>>
'0.2.1'

Download Tokyo data from Global Map Japan.

python3


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

Take a look at the first three lines.

python3


df = gpd.read_file('tokyo.geojson')
df[:3]
area_en area_ja code geometry ward_en ward_ja
0 Tokubu Tokyo 23 wards 131211.0 POLYGON ((139.821051 35.815077, 139.821684 35.... Adachi Ku
1 Tokubu Tokyo 23 wards 131059.0 POLYGON ((139.760933 35.732206, 139.761002 35.... Bunkyo Ku
2 Tokubu Tokyo 23 wards 131016.0 POLYGON ((139.770135 35.705352, 139.770172 35.... Chiyoda Ku

The geometry contains polygon data. Draw with matplotlib.

python3


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

image

This time, draw with bokeh. Since the polygon of geometry is a mixture of shapely.geometry.polygon.Polygon and shapely.geometry.multipolygon.MultiPolygon, flatten it to make an array of 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 didn't work.

Reference link

-I want to plot geographic information with Python geopandas + Bokeh

[^ 1]: You can also do it with "conda install -y pyproj shapely fiona; pip install geopandas".

that's all

Recommended Posts

Try using geopandas
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
[Azure] Try using Azure Functions
Try using virtualenv now
Try using W & B
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Try using Tweepy [Python2.7]
Try using Pytorch's collate_fn
Try using PythonTex with Texpad.
[Python] Try using Tkinter's canvas
Try using Jupyter's Docker image
Try using scikit-learn (1) --K-means clustering
Try function optimization using Hyperopt
Try using matplotlib with PyCharm
Try using Azure Logic Apps
Try using Kubernetes Client -Python-
[Kaggle] Try using xg boost
Try using the Twitter API
Try using OpenCV on Windows
Try using Jupyter Notebook dynamically
Try using AWS SageMaker Studio
Try tweeting automatically using Selenium.
Try using SQLAlchemy + MySQL (Part 1)
Try using the Twitter API
Try using SQLAlchemy + MySQL (Part 2)
Try using Django's template feature
Try using the PeeringDB 2.0 API
Try using Pelican's draft feature
Try using pytest-Overview and Samples-
Try using folium with anaconda
Try using Janus gateway's Admin API
[Statistics] [R] Try using quantile regression.
Try using Spyder included in Anaconda
Try using design patterns (exporter edition)
Try using Pillow on iPython (Part 1)
Try using Pillow on iPython (Part 2)
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using pynag to configure Nagios
Try using PyCharm's remote debugging feature
Try using ArUco on Raspberry Pi
Try using cheap LiDAR (Camsense X1)
[Sakura rental server] Try using flask.
Try using Pillow on iPython (Part 3)
Reinforcement learning 8 Try using Chainer UI
Try to get statistics using e-Stat
Try using Python argparse's action API
Try using the Python Cmd module
Try using Python's networkx with AtCoder