パイクラおじさんの日記

MinecraftでPythonを勉強するおじさんの日記です。

標高データを配列に

基盤地図情報~数値標高モデルを利用する - Qiita

このページを参考にNumPyを使ってみた。

# coding: utf-8

import xml.etree.ElementTree as ET
import numpy as np
import sys

GEO_DIR = "FG-GML-{0:04d}-{1:02d}-DEM5A"
GEO_XML = "FG-GML-{0:04d}-{1:02d}-{2:02d}-DEM5A-20161001.xml"

if len(sys.argv) < 4:
    print("xmlnpy.py mesh1 mesh2 mesh3")
    exit(-1)

mesh1 = int(sys.argv[1])
mesh2 = int(sys.argv[2])
mesh3 = int(sys.argv[3])

dir = GEO_DIR.format(mesh1, mesh2)
fname = GEO_XML.format(mesh1, mesh2, mesh3)

tree = ET.parse(dir+"/"+fname)
root = tree.getroot()

tl = root.find('./{http://fgd.gsi.go.jp/spec/2008/FGD_GMLSchema}DEM/{http://fgd.gsi.go.jp/spec/2008/FGD_GMLSchema}coverage/{http://www.opengis.net/gml/3.2}rangeSet/{http://www.opengis.net/gml/3.2}DataBlock/{http://www.opengis.net/gml/3.2}tupleList')
if tl is None:
    print("{http://www.opengis.net/gml/3.2}tupleList is not found")
    exit(-1)

lines = tl.text.split()
array = np.zeros(len(lines))
i = 0
for l in lines:
    (t, h) = l.split(",")
    array[i] = float(h)
    i += 1

img_array = array.reshape((225, 150))
print(img_array)

実際にはもっとちゃんとデータを確認しないといけないけど。

実行すると、こんな感じ。

$ python xmlnpy.py 5340 22 3
[[   57.46    54.86    52.8  ...,    16.3     17.34    17.93]
 [   18.48    18.87    19.45 ...,    21.18    20.57    20.68]
 [   20.68    19.65    18.49 ...,    32.57    30.14    27.74]
 ..., 
 [   30.5     30.49    30.55 ..., -9999.   -9999.   -9999.  ]
 [-9999.   -9999.      13.9  ...,    16.69    16.57    16.39]
 [   16.5     17.01    17.74 ...,    11.57    11.41    10.99]]

NumPyの使い方

qiita.com