LoginSignup
2
5

More than 5 years have passed since last update.

Pythonで世界地図(cartopy版)

Last updated at Posted at 2018-07-07
実行した環境

Ubuntu Stdio 18.04LTS
Python 2.7.15rc1

Python3は、下記エラーが解決出来ませんでした。
Python3はitertoolsのifilterが実装されていないようです。
http://kk6.hateblo.jp/entry/20110521/1305984781
File "/usr/local/lib/python3.6/dist-packages/shapely/topology.py", line 18, in _validate
raise ValueError("Null geometry supports no operations")
ValueError: Null geometry supports no operations

参考

Met Postさん
http://metpost.hatenablog.com/

インストールしたもの

最初は別のものをやっていたので下記のものが必要かどうかわかりません。
下記以外も必要ですが、すでにインストールしてあったものです。
$ pip install xray
$ pip3 install Pydap

都道府県を色分けする。

奄美大島が沖縄と同じ色になっていますが、1953年の奄美大島がアメリカから変換される前のデータのようです。

#!/usr/bin/python
# coding: UTF-8

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature #県境・川

#都道府県着色---
import cartopy.io.shapereader as shapereader
import itertools

# Natural Earthから州データセットを取得する
shpfilename = shapereader.natural_earth(resolution='10m',
                                        category='cultural',
                                        name='admin_1_states_provinces')

# get shapefile records
reader = shapereader.Reader(shpfilename)
provinces = reader.records()

# 日本の国をフィルタリングする
provinces_of_japan = itertools.ifilter(lambda province: province.attributes['admin'] == 'Japan', provinces)

# plot
colors = itertools.cycle(['red', 'blue', 'green', 'lime', 'orange', 'cyan', 'purple','gray','yellow','violet','magenta'])
#都道府県着色---

plt.figure(figsize=[5,5])

states_10m  = cfeature.NaturalEarthFeature('cultural', 'admin_1_states_provinces_lines', '10m', #県境
                                           edgecolor='gray',
                                           facecolor='none')  # no filled color
## 10m resolution
ax1 = plt.axes(projection=ccrs.PlateCarree())
ax1.coastlines(resolution='10m')

#都道府県着色---
for province in provinces_of_japan:
    geometry = province.geometry
    ax1.add_geometries(geometry, ccrs.PlateCarree(), facecolor=next(colors))
#都道府県着色---

ax1.set_extent([120,150,20,50], ccrs.PlateCarree())
ax1.add_feature(states_10m) #県境
ax1.set_title('10m coastline')

plt.show()

image.png

2
5
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
2
5