タカ
本稿では、Cartopyで都道府県の境界を取得してみます。
shpから、名前を取得してみます。
サンプルコード
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shapereader
shpfilename = shapereader.natural_earth(resolution='10m',
category='cultural',
name='admin_1_states_provinces')
provinces = shapereader.Reader(shpfilename).records()
provinces_of_japan = filter(
lambda province: province.attributes['admin'] == 'Japan', provinces)
for province in provinces_of_japan:
print(province.attributes['name'])
続けて、地図へ追加して描画してみます。
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shapereader
shpfilename = shapereader.natural_earth(resolution='10m',
category='cultural',
name='admin_1_states_provinces')
provinces = shapereader.Reader(shpfilename).records()
provinces_of_japan = filter(
lambda province: province.attributes['admin'] == 'Japan', provinces)
plt.figure(figsize=[8, 8])
ax = plt.axes(projection=ccrs.PlateCarree())
for province in provinces_of_japan:
geometry = province.geometry
ax.add_geometries(geometry, ccrs.PlateCarree())
ax.coastlines(resolution='10m')
ax.set_extent([133, 145, 33, 40])
plt.show()