パイクラおじさんの日記

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

マイクラ内での読み込み(4)

3次メッシュの読み込みが出来たので、2次メッシュ(5340-22)を読み込んでみることにした。

f:id:pycra:20170918022853p:plain
2次メッシュ(5340-22)

まずは5x5は面倒なので、高さを1/5にして.npy形式に出力して、読み込みを実行した。

読み込みには小1時間かかった。

f:id:pycra:20170919025630p:plain
大網駅

f:id:pycra:20170919025726p:plain
小中池

f:id:pycra:20170919025800p:plain
千葉市昭和の森

f:id:pycra:20170919025842p:plain
土気駅周辺

f:id:pycra:20170919025917p:plain
外房有料道路(トンネルから出たところ)

f:id:pycra:20170919030005p:plain
東急セブンハンドレッドクラブ(ゴルフ場)

なかなか、いい感じに再現されている。

ただし、他の地域(左側)は平坦で池や川以外に地域を判別するのが難しい。

1/5のサイズはミニチュア感があって面白いけど、これを5倍して等倍にしたい。

.npyデータの作成プログラム(npyall.py)

# coding: utf-8

import xml.etree.ElementTree as ET
import numpy as np
import matplotlib.pyplot as plot
import sys

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

NPY_DIR = "/Users/pycra/Desktop"
NPY_FILE = "npydata-{0:04d}-{1:02d}.npy"

WATER_LEVEL = -2.0
SEA_LEVEL = -3.0

def xml2array(mesh1, mesh2, mesh3):
    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:
        raise Exception("{http://www.opengis.net/gml/3.2}tupleList is not found")

    sp = 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}coverageFunction/{http://www.opengis.net/gml/3.2}GridFunction/{http://www.opengis.net/gml/3.2}startPoint')
    if sp is None:
        raise Exception("{http://www.opengis.net/gml/3.2}startPoint is not found")

    (spx, spy) = sp.text.split()

    lines = tl.text.split()
    array = np.zeros(33750)
    array.fill(WATER_LEVEL)
    i = int(spx) + int(spy) * 225
    for l in lines:
        (t, h) = l.split(",")
        hval = float(h)
        if hval == -9999:
            array[i] = WATER_LEVEL
        else:
            array[i] = hval * 0.2
        i += 1

    return array.reshape((150, 225))


# ここからスタート

if len(sys.argv) < 3:
    print("npyall.py mesh1 mesh2")
    exit(-1)

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

yarray = None
for y in range(10):
    xarray = None
    for x in range(10):
        try:
            mesh3 = y * 10 + x
            array = xml2array(mesh1, mesh2, mesh3)
        except Exception as err:
            print(err)
            array = np.zeros((150, 225))
            array.fill(SEA_LEVEL)
        finally:
            if xarray is None:
                xarray = array
            else:
                xarray = np.concatenate((xarray, array), axis=1)
        
    if yarray is None:
        yarray = xarray
    else:
        yarray = np.concatenate((xarray, yarray), axis=0)

print(yarray.shape)

np.save(NPY_DIR+"/"+NPY_FILE.format(mesh1, mesh2), yarray)

#plot.imshow(yarray)
#plot.colorbar()
#plot.show()

マイクラ側の読み込みプログラム(loadnpy2.py)

# coding: utf-8

import mcpi.minecraft as minecraft
import mcpi.block as block
import numpy as np
import sys
from time import sleep

NPY_DIR = "/Users/pycra/Desktop"
NPY_FILE = "npydata-{0:04d}-{1:02d}.npy"

mc = minecraft.Minecraft()
mc.postToChat("Loadnpy2.py Start!")

if len(sys.argv) < 3:
    print("loadnpy2.py mesh1 mesh2")
    exit(-1)

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

array = np.load(NPY_DIR+"/"+NPY_FILE.format(mesh1, mesh2))
(zshape, xshape) = array.shape

for z in range(zshape):
    sleep(0.01)
    for x in range(xshape):
        hval = array[z][x]
        mc.setBlocks(x, hval-1, z, x, hval-7, z, block.DIRT)
        mc.setBlock(x, hval, z, block.GRASS)
        sleep(0.001)

mc.postToChat("Loadnpy2.py Finish!!!")