Skip to content

Instantly share code, notes, and snippets.

@takunoko
Created January 19, 2017 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takunoko/380db99eaf1769fe147e24d707154522 to your computer and use it in GitHub Desktop.
Save takunoko/380db99eaf1769fe147e24d707154522 to your computer and use it in GitHub Desktop.
# esriジャパンから取得したデータを扱う
# 国土地理院発行の数値地図
# 地図データ[ESRIジャパン]: http://www.esrij.com/products/japan-shp/
import shapefile
import pandas as pd
import turtle as tt
# turtle画面設定 定数
TURTLE_WINDOW_WIDTH = 1080
TURTLE_WINDOW_HEIGHT = 720
# saheps[n].record の内容を引数に
# 同じ形式で地名とかを日本語変換したのを返す。
def jp_data_escape(data):
JCODE = data[0]
KEN = data[1].decode('sjis')
SICHO = data[2] if data[2] == '' else data[2].decode('sjis')
GUN = data[3] if data[3] == '' else data[3].decode('sjis')
SEIREI = data[4] if data[4] == '' else data[4].decode('sjis')
SIKUCHOSON = data[5].decode('sjis')
CITY_ENG = data[6]
P_NUM = data[7]
H_NUM = data[8]
d = [JCODE, KEN, SICHO, GUN, SEIREI, SIKUCHOSON, CITY_ENG, P_NUM, H_NUM]
return d
# pandasのデータセット型に情報を入れる。
def pd_data_set(s_data):
jp_data = []
for s in s_data:
row_data = jp_data_escape(s.record)
row_data.append(s.shape.points)
jp_data.append(row_data)
pd_data = pd.DataFrame(jp_data)
pd_data.columns = ['JCODE', 'KEN', 'SICHO', 'GUN', 'SEIREI', 'SIKUCHOSON', 'CITY_ENG', 'P_NUM', 'H_NUM', 'SHAPE']
return pd_data
# turtleを使う際の設定をまとめておく
def turtle_init():
tt.setup(width = TURTLE_WINDOW_WIDTH, height = TURTLE_WINDOW_HEIGHT, startx=0, starty=0)
tt.speed(10)
# turtleを用いてある市町村の図形を書く
def write_turtle_map(p_list, pos_scale=1000, c_x=136, c_y=35):
for pl in p_list:
for i,p in enumerate(pl):
v = 3000
if(i == 0):
tt.penup()
tt.setpos((c_x-p[0])*pos_scale, (c_y-p[1])*pos_scale)
tt.pendown()
else:
tt.setpos((c_x-p[0])*pos_scale, (c_y-p[1])*pos_scale)
tt.setpos((c_x-pl[0][0])*pos_scale, (c_y-pl[0][1])*pos_scale)
# マップの描画に必要な座標を取得する
# とても頭の悪いプログラムだ。
def get_map_size(data_list):
# 1つめのデータを取得とか面倒なので、適当な値を代入
px_max = 0.0; px_min = 180.0;
py_max = 0.0; py_min = 90.0;
d_arr = data_list.values
for dl in d_arr:
for pos in dl[0]:
if pos[0] > px_max:
px_max = pos[0]
if pos[0] < px_min:
px_min = pos[0]
if pos[1] > py_max:
py_max = pos[1]
if pos[1] < py_min:
py_min = pos[1]
print(px_max, px_min, py_max, py_min)
return px_max, px_min, py_max, py_min
if __name__ == "__main__":
# 図形ファイルへのパスはそれぞれの環境にあわせて変更する。
sf = shapefile.Reader("../japan_ver81/japan_ver81.shp")
shapes = sf.shapeRecords()
pd_data = pd_data_set(shapes)
turtle_init() # turtleを使う際の初期化をまとめたもの
# 千葉県の市町村のみ取り出してみる
indexer = pd_data['KEN'] == '千葉県'
px_max, px_min, py_max, py_min = get_map_size(pd_data.ix[indexer.index[indexer], ['SHAPE']])
map_w = px_max - px_min
map_h = py_max - py_min
if map_w > map_h:
scale = (TURTLE_WINDOW_WIDTH*0.85) / map_w
else:
scale = (TURTLE_WINDOW_HEIGHT*0.85) / map_h
for data_num in indexer.index[indexer]:
JCODE, SIKUCHOSON = pd_data.ix[data_num, ['JCODE', 'SIKUCHOSON']]
print("JCODE: {0:>5s}, 市区町村名: {1:>7s}".format(JCODE, SIKUCHOSON))
write_turtle_map(pd_data.ix[data_num, ['SHAPE']], pos_scale = -scale, c_x = px_min + map_w/2, c_y = py_min + map_h/2)
# (-scale と負の値にするのは描画した際の座標の上下を合わせるため)
input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment