5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで世界地図-10(日本地図に都府県境界線を追加する)

Last updated at Posted at 2018-06-25

都府県境界線の境界線は関西まで追加しましたが、ライブラリ化しましたので、東北〜中部はここには追加しません。
Pythonで世界地図-11(日本地図に都府県境界線を追加するライブラリ)に追加します。

mpl_toolkits.basemapには、アメリカの州の境界線を描画する関数があるのですが、日本の都府県境界線はありません。

国土数値情報 ダウンロードサービス
http://nlftp.mlit.go.jp/ksj/
にある、行政区域で都府県境界線を描画できるかと思っていましたが、市区町村の境界線まであって都府県の県境がわかりません。
.shpファイルを研究すれば出来るかと思いましたが、スキルが不足していて出来ません。

県別データの可視化
https://qiita.com/SaitoTsutomu/items/6d17889ba47357e44131
で日本地図を表示・加工するというのを見つけました。
この中に、境界線のベクトルデータを取得する関数があって、そのデータを加工して都府県境界線を描画することが出来ました。

mpl_toolkits.basemapの海岸線とは厳密には接続していませんが、パット見た目にはわかりません。

image.png

本州はデータが非常に多いのでまだ出来ていません。

インストール

Ubuntu Stdio 18.04の場合

$ pip3 install japanmap

numpyもインストールが必要です。

japanmapで日本地図を描画する場合は、OpenCV, pillowもインストールが必要ですが、都府県境界線だけを利用する場合は不要です。

行政区域の境界線を描画するには、下記のページを参照。
Pythonで世界地図-3(日本地図)
https://qiita.com/ty21ky/items/b59e7d9e4b164a803c1f

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

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from japanmap import *

# 「N03-17_170101.shp」のdownloadが必要
def administrative_area(): #行政区域(国土交通省の行政区域の境界線。精密な分非常に時間がかかる。)
    import cartopy.crs as ccrs
    import cartopy.io.shapereader as shpreader

    fname = '/home/ty/python/map/japan/N03-170101_GML/N03-17_170101.shp'
    shapes = list(shpreader.Reader(fname).geometries())

    ax = plt.axes(projection=ccrs.PlateCarree())
    ax.add_geometries(shapes, ccrs.PlateCarree(), edgecolor='black', facecolor='gray', alpha=0.3)
    ax.set_extent([127, 147, 27, 47], ccrs.PlateCarree())

def print_bound(no, start, end, start_p, color, linewidth, linestyle): #県境の描画
    ii = 0
    for n in no:
        a = pnts[n]

        x = []
        y = []

        for i in range(start[ii], len(a) - 1 - end[ii]):
            x.append(a[i][0])
            y.append(a[i][1])

        if start_p[ii]:
            x.append(a[0][0])
            y.append(a[0][1])

        plt.plot(x, y, linewidth = linewidth, color = color, linestyle = linestyle)
        ii += 1

def shikoku_bound(): #四国県境
    no = [35,36,38] #徳島県,香川県,高知県
    start = [59,0,112] #0は最初から、それ以外はその番号から
    end = [0,69,0] #0は最後まで、それ以外は最後の番号から数値分を引く
    start_p = [True,False,True] #Trueの場合、最初のポイントが必要
    color = 'red'
    linewidth = 0.5
    linestyle = '--'
    print_bound(no, start, end, start_p, color, linewidth, linestyle)

def kyushu_bound(): #九州県境
    no = [39,40,41,43,44,45] #福岡県,佐賀県,長崎県,大分県,宮崎県,鹿児島県
    start = [18,48,0,0,20,0] #0は最初から、それ以外はその番号から
    end = [117,16,166,103,61,112] #0は最後まで、それ以外は最後の番号から数値分を引く
    start_p = [False,False,False,False,False,False] #Trueの場合、最初のポイントが必要
    color = 'red'
    linewidth = 0.5
    linestyle = '--'
    print_bound(no, start, end, start_p, color, linewidth, linestyle)

def chugoku_region(): #中国県境
    no = [30,31,32,33,34] #鳥取県,島根県,岡山県,広島県,山口県
    start = [23,0,39,0,0] #0は最初から、それ以外はその番号から
    end = [14,49,69,110,155] #0は最後まで、それ以外は最後の番号から数値分を引く
    start_p = [False,False,False,False,False] #Trueの場合、最初のポイントが必要
    color = 'red'
    linewidth = 0.5
    linestyle = '--'
    print_bound(no, start, end, start_p, color, linewidth, linestyle)

def kansai_region(): #関西県境
    no = [23,24,25,26,27,27,27,28,29] #三重県,滋賀県,京都府,大阪府,兵庫県,兵庫県,兵庫県,奈良県,和歌山県
    start = [138,17,89,0,0,116,24,0,83] #0は最初から、それ以外はその番号から
    end = [0,15,0,48,121,0,73,65,0] #0は最後まで、それ以外は最後の番号から数値分を引く
    start_p = [True,False,True,False,False,True,False,False,True] #Trueの場合、最初のポイントが必要
    color = 'red'
    linewidth = 0.5
    linestyle = '--'
    print_bound(no, start, end, start_p, color, linewidth, linestyle)

qpqo = get_data()
pnts = pref_points(qpqo)
def prefectural_bound(): #県境
    shikoku_bound()
    kyushu_bound()
    chugoku_region()
    kansai_region()
    #chubu_region()
    #hokuriku_region()
    #kanto_region()
    #yohoku_region()

# administrative_area() #行政区域(国土交通省の行政区域の境界線。精密な分非常に時間がかかる。)
prefectural_bound() #県境

lon1 = 137.0
lat1 = 37.0
lon2 = 10.0
lat2 = 10.0

map = Basemap(llcrnrlon = lon1 - lon2, llcrnrlat = lat1 - lat2, urcrnrlon = lon1 + lon2, urcrnrlat = lat1 + lat2, resolution = 'h', projection='cyl')

map.drawcoastlines() #海岸線(行政区域を表示する場合は不要。行政区域の方が精度は高い)

plt.show()

plt.plot ドキュメント
https://matplotlib.org/users/pyplot_tutorial.html

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?