Skip to content

Commit a26f92a

Browse files
authored
Merge pull request #30 from eukarya-inc/feature/dev-28
#28 add MvtTileParser and bump 0.0.5
2 parents 514311c + 235c640 commit a26f92a

File tree

5 files changed

+136
-2
lines changed

5 files changed

+136
-2
lines changed

Diff for: README.rst

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ This is a collection of utilities for the `Plateau <https://www.mlit.go.jp/plate
1818
>>> result = parser.download_and_parse("https://assets.cms.plateau.reearth.io/assets/d6/70821e-7f58-4f69-bc34-341875704e78/40203_kurume-shi_2020_citygml_3_op.zip", "/tmp")
1919
>>> result
2020
[{'gid': 'bldg_383f1804-aa34-4634-949f-f769e09fa92d', 'center': [130.41263587199947, 33.22489181671553], 'min_height': 3.805999994277954, 'measured_height': 9.3, 'building_structure_type': '非木造'}, {'gid': 'bldg_877dea60-35d0-4fd9-8b02-852e39c75d81', 'center': [130.41619367090038, 33.22492719812357], 'min_height': 4.454999923706055, 'measured_height': 3.0, 'building_structure_type': '非木造'},...]
21+
>>> from plateauutils.parser.mvt_tile_parser import MvtTileParser
22+
>>> target_polygon = from_wkt("POLYGON ((130.525689 33.323966, 130.522728 33.314069, 130.511441 33.308653, 130.501013 33.30937, 130.492516 33.318516, 130.493717 33.325831, 130.504618 33.332249, 130.512857 33.332213, 130.525689 33.323966))")
23+
>>> parser = MvtTileParser(target_polygon)
24+
>>> result = parser.download_and_parse("https://assets.cms.plateau.reearth.io/assets/43/53a0e1-cc14-4228-a5ef-19333a23596d/40203_kurume-shi_2020_3dtiles-mvt_3_op.zip", "/tmp")
25+
>>> result
26+
['/tmp/40203_kurume-shi_2020_3dtiles-mvt_3_op/luse/15/28254/13174.mvt']
2127
2228
How to develop
2329
--------------

Diff for: doc/index.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ Usage
4040
>>> result = parser.download_and_parse("https://assets.cms.plateau.reearth.io/assets/d6/70821e-7f58-4f69-bc34-341875704e78/40203_kurume-shi_2020_citygml_3_op.zip", "/tmp")
4141
>>> result
4242
[{'gid': 'bldg_383f1804-aa34-4634-949f-f769e09fa92d', 'center': [130.41263587199947, 33.22489181671553], 'min_height': 3.805999994277954, 'measured_height': 9.3, 'building_structure_type': '非木造'}, {'gid': 'bldg_877dea60-35d0-4fd9-8b02-852e39c75d81', 'center': [130.41619367090038, 33.22492719812357], 'min_height': 4.454999923706055, 'measured_height': 3.0, 'building_structure_type': '非木造'},...]
43-
43+
>>> from plateauutils.parser.mvt_tile_parser import MvtTileParser
44+
>>> parser = MvtTileParser(target_polygon)
45+
>>> result = parser.download_and_parse("https://assets.cms.plateau.reearth.io/assets/43/53a0e1-cc14-4228-a5ef-19333a23596d/40203_kurume-shi_2020_3dtiles-mvt_3_op.zip", "/tmp")
46+
>>> result
47+
['/tmp/40203_kurume-shi_2020_3dtiles-mvt_3_op/luse/15/28254/13174.mvt']
4448
4549
.. toctree::
4650
:maxdepth: 2

Diff for: plateauutils/parser/mvt_tile_parser.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import glob
2+
import os
3+
from plateauutils.abc.plateau_parser import PlateauParser
4+
from plateauutils.tile_list.polygon_to_tile_list import PolygonToTileList
5+
from shapely.geometry import Polygon
6+
import shutil
7+
import zipfile
8+
9+
10+
class MvtTileParser(PlateauParser):
11+
"""MVTタイルをパースするクラス
12+
13+
Parameters
14+
----------
15+
polygon : shapely.geometry.Polygon
16+
対象となるポリゴン
17+
zoom : int
18+
対象とするズームレベル
19+
"""
20+
21+
def __init__(self, polygon: Polygon = None, zoom: int = 15):
22+
self.zoom = zoom
23+
super().__init__(polygon)
24+
25+
def parse(self, target_path: str = "") -> list:
26+
"""MVTタイルをパースして、リストを返すメソッド
27+
28+
Parameters
29+
----------
30+
target_path : str
31+
MVTタイル(zip)のパス
32+
33+
Returns
34+
-------
35+
list
36+
タイルのパスのリスト
37+
"""
38+
# ファイルが存在しないならエラー
39+
if not os.path.exists(target_path):
40+
raise FileNotFoundError(f"target_path: {target_path} is not found")
41+
# zipファイルにターゲットのパスが存在するか確認
42+
hit_targets = []
43+
with zipfile.ZipFile(target_path) as zip_file:
44+
for name in zip_file.namelist():
45+
for target in self.targets:
46+
path = os.path.join("luse", target)
47+
if name.find(path) >= 0:
48+
hit_targets.append(target)
49+
if len(hit_targets) == 0:
50+
raise ValueError(f"target_path: {target_path} is not target")
51+
# zipファイルを解凍する
52+
unarchived_dir = target_path.replace(".zip", "")
53+
shutil.unpack_archive(target_path, unarchived_dir)
54+
# 返り値を作成
55+
return_list = []
56+
# 解凍したファイルをパースする
57+
for target in hit_targets:
58+
# ファイルパスを作成
59+
target_file_path = os.path.join(unarchived_dir, "luse", target)
60+
for file_path in glob.glob(target_file_path):
61+
return_list.append(file_path)
62+
return sorted(return_list)
63+
64+
def download_and_parse(self, url: str = "", target_dir: str = "") -> list:
65+
"""MVTタイルをダウンロードして、タイルのリストを返すメソッド
66+
67+
Parameters
68+
----------
69+
url : str
70+
MVTタイル(zip)のURL
71+
target_dir : str
72+
ファイルを展開する先のパス
73+
74+
Returns
75+
-------
76+
list
77+
タイルのパスのリスト
78+
"""
79+
saved_path = self._download(url, target_dir)
80+
return self.parse(saved_path)
81+
82+
def _target_list(self, polygon: Polygon = None) -> list:
83+
# PolygonがNoneならエラー
84+
if polygon is None:
85+
raise ValueError("polygon is None")
86+
# Polygonからタイルのリストを作成
87+
tile_list = PolygonToTileList(polygon, self.zoom)
88+
return tile_list.output()

Diff for: plateauutils/parser/tests/test_mvt_tile_parser.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
from plateauutils.parser.mvt_tile_parser import MvtTileParser
3+
from shapely import from_wkt
4+
import tempfile
5+
6+
7+
def test_mvt_tile_parser_z15():
8+
test_polygon = from_wkt(
9+
"POLYGON ((130.525689 33.323966, 130.522728 33.314069, 130.511441 33.308653, 130.501013 33.30937, 130.492516 33.318516, 130.493717 33.325831, 130.504618 33.332249, 130.512857 33.332213, 130.525689 33.323966))"
10+
)
11+
parser = MvtTileParser(test_polygon, 15)
12+
with tempfile.TemporaryDirectory() as tmpdir:
13+
result = parser.download_and_parse(
14+
"https://file.smellman.org/test_mvt_list.zip", tmpdir
15+
)
16+
assert len(result) == 13
17+
assert result[0] == os.path.join(
18+
tmpdir, "test_mvt_list/luse/15/28261/13163.mvt"
19+
)
20+
assert result[12] == os.path.join(
21+
tmpdir, "test_mvt_list/luse/15/28264/13165.mvt"
22+
)
23+
24+
25+
def test_mvt_tile_parser_z14():
26+
test_polygon = from_wkt(
27+
"POLYGON ((130.525689 33.323966, 130.522728 33.314069, 130.511441 33.308653, 130.501013 33.30937, 130.492516 33.318516, 130.493717 33.325831, 130.504618 33.332249, 130.512857 33.332213, 130.525689 33.323966))"
28+
)
29+
parser = MvtTileParser(test_polygon, 14)
30+
with tempfile.TemporaryDirectory() as tmpdir:
31+
result = parser.download_and_parse(
32+
"https://file.smellman.org/test_mvt_list.zip", tmpdir
33+
)
34+
assert len(result) == 6
35+
assert result[0] == os.path.join(tmpdir, "test_mvt_list/luse/14/14130/6581.mvt")
36+
assert result[5] == os.path.join(tmpdir, "test_mvt_list/luse/14/14132/6582.mvt")

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ classifiers = [
1111
"Development Status :: 2 - Pre-Alpha",
1212
"Programming Language :: Python :: 3",
1313
]
14-
version = "0.0.4"
14+
version = "0.0.5"
1515
dependencies = [
1616
"click",
1717
"numpy",

0 commit comments

Comments
 (0)