@@ -26,49 +26,50 @@ def parse(self, output_dir: str = ""):
26
26
output_dir : str
27
27
pngの出力先
28
28
"""
29
- # lontitude, latitude, altitudeを取得
30
- lons , lats , maps = self ._parse_tile ()
31
- # storeを作成
32
- store = Store ()
33
- # storeのaddメソッドをベクトル化
34
- vfunc = np .vectorize (store .add )
35
- # storeにlontitude, latitude, altitudeを追加
36
- vfunc (lons , lats , maps )
37
- # pngを書き出す
38
- writer = PngWriter (output_dir , 15 )
39
- writer .setStore (store )
40
- writer .write ()
29
+ # lontitude, latitude, classificationsを取得
30
+ lons , lats , _ , classifications = self ._parse_tile ()
31
+ # ズームレベル8から15までのpngを作成
32
+ for zoom in range (8 , 16 ):
33
+ # storeを作成
34
+ store = Store (zoom )
35
+ # storeのaddメソッドをベクトル化
36
+ vfunc = np .vectorize (store .add )
37
+ # storeにlontitude, latitude, classificationsを追加
38
+ vfunc (lons , lats , classifications )
39
+ # pngを書き出す
40
+ writer = PngWriter (output_dir , zoom )
41
+ writer .setStore (store )
42
+ writer .write ()
41
43
42
44
43
45
class Store (object ):
44
46
"""タイルの座標及び標高を保持するクラス"""
45
47
46
- def __init__ (self ):
47
- self .zoom = 15
48
+ def __init__ (self , zoom ):
49
+ self .zoom = zoom
48
50
self .storage = dict ()
49
51
50
- def add (self , x , y , z ):
52
+ def add (self , x , y , classification ):
51
53
"""タイルの座標及び標高を格納するメソッド"""
52
- longitude , latitude , altitude = x , y , z
54
+ longitude , latitude = x , y
53
55
# 座標からタイルの座標とタイル内の座標を取得
54
56
x , y , pos_x , pos_y = self ._coordinate_to_position (longitude , latitude )
55
57
# storageに格納
56
- self ._insert (x , y , pos_x , pos_y , altitude )
58
+ self ._insert (x , y , pos_x , pos_y , classification )
57
59
58
- def _insert (self , x , y , pos_x , pos_y , altitude ):
60
+ def _insert (self , x , y , pos_x , pos_y , classification ):
59
61
# keyがstorageに存在する場合はその値を取得
60
62
key = (x , y )
61
63
if key in self .storage .keys ():
62
64
array = self .storage [key ]
63
65
else :
64
66
# 存在しない場合は256*256の配列を作成
65
- array = np .zeros ((256 , 256 ))
66
- array .fill (- np .inf )
67
+ array = np .zeros ((256 , 256 ), dtype = np .int32 )
67
68
self .storage [key ] = array
68
69
# 標高を格納
69
70
current = array [pos_x ][pos_y ]
70
- if current < altitude :
71
- array [pos_x ][pos_y ] = altitude
71
+ if current < classification :
72
+ array [pos_x ][pos_y ] = classification
72
73
self .storage [key ] = array
73
74
74
75
def _coordinate_to_position (self , longitude , latitude ):
@@ -157,55 +158,50 @@ def _write(self, x, y, value):
157
158
sys .exit (- 1 )
158
159
# 標高をpngに変換
159
160
dt = np .dtype (
160
- {"names" : ["r" , "g" , "b" ], "formats" : [np .uint8 , np .uint8 , np .uint8 ]}
161
+ {
162
+ "names" : ["r" , "g" , "b" , "a" ],
163
+ "formats" : [np .uint8 , np .uint8 , np .uint8 , np .uint8 ],
164
+ }
161
165
)
162
166
converted1 = np .array (
163
- [tuple (self ._dem_to_png (v )) for v in value .reshape (value .size )], dtype = dt
167
+ [tuple (self ._classification_to_png (v )) for v in value .reshape (value .size )],
168
+ dtype = dt ,
164
169
)
165
170
converted2 = converted1 .reshape (value .shape )
166
171
filename = f"{ self .directory } /{ self .zoom } /{ x } /{ y } .png"
167
172
width = 256
168
- img = Image .new ("RGB " , (width , width ), (128 , 0 , 0 ))
173
+ img = Image .new ("RGBA " , (width , width ), (128 , 0 , 0 , 0 ))
169
174
draw = ImageDraw .Draw (img )
170
175
for i in range (0 , width ):
171
176
for j in range (0 , width ):
172
177
p = converted2 [i ][j ]
173
- draw .point ([(i , j )], (int (p [0 ]), int (p [1 ]), int (p [2 ])))
178
+ draw .point ([(i , j )], (int (p [0 ]), int (p [1 ]), int (p [2 ]), int ( p [ 3 ]) ))
174
179
img .save (filename )
175
180
176
- def _dem_to_png (self , dem ):
177
- # 標高をPNGのRGBに変換するメソッド
178
- # 内容が入っていなかったら白色
179
- if dem == - np .inf :
180
- return (0xFF , 0xFF , 0xFF )
181
- # 標高に応じて色を変更
182
- if dem > 20 :
183
- # 185 26 248
184
- return (0xB9 , 0x1A , 0xF8 )
185
- if dem > 10 :
186
- # 169 8 91
187
- return (0xA9 , 0x08 , 0x5B )
188
- if dem > 5 :
189
- # 253 35 21
190
- return (0xFD , 0x23 , 0x15 )
191
- if dem > 4 :
192
- # 236 106 141
193
- return (0xEC , 0x6A , 0x8D )
194
- if dem > 3 :
195
- # 254 115 117
196
- return (0xFE , 0x73 , 0x75 )
197
- if dem > 2 :
198
- # 236 182 182
199
- return (0xEC , 0xB6 , 0xB6 )
200
- if dem > 1 :
201
- # 255 141 36
202
- return (0xFF , 0x8D , 0x24 )
203
- if dem > 0.3 :
204
- # 255 225 53
205
- return (0xFF , 0xE1 , 0x35 )
206
- if dem > 0.01 :
207
- # 48 254 55
208
- return (0x30 , 0xFE , 0x37 )
209
- # 0.01以下は白色
181
+ def _classification_to_png (self , classification ):
182
+ # 属性をPNGのRGBに変換するメソッド
183
+ # 内容が入っていなかったら透明色
184
+ if classification == 0 :
185
+ return (0xFF , 0xFF , 0xFF , 0x00 )
186
+ # 属性に応じて色を変更
187
+ if classification == 6 :
188
+ # 220 122 220
189
+ return (0xDC , 0x7A , 0xDC , 0xFF )
190
+ if classification == 5 :
191
+ # 242 133 201
192
+ return (0xF2 , 0x85 , 0xC9 , 0xFF )
193
+ if classification == 4 :
194
+ # 255 145 145
195
+ return (0xFF , 0x91 , 0x91 , 0xFF )
196
+ if classification == 3 :
197
+ # 255 183 183
198
+ return (0xFF , 0xB7 , 0xB7 , 0xFF )
199
+ if classification == 2 :
200
+ # 255 216 192
201
+ return (0xFF , 0xD8 , 0xC0 , 0xFF )
202
+ if classification == 1 :
203
+ # 247 245 169
204
+ return (0xF7 , 0xF5 , 0xA9 , 0xFF )
205
+ # 例外は透明色
210
206
# 255 255 255
211
- return (0xFF , 0xFF , 0xFF )
207
+ return (0xFF , 0xFF , 0xFF , 0x00 )
0 commit comments