Skip to content

Commit 760f65e

Browse files
authored
Merge pull request #39 from eukarya-inc/dev-38
#38 read zip file directly
2 parents 85125a6 + 3282a1d commit 760f65e

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

Diff for: plateauutils/parser/city_gml_parser.py

+17-21
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,16 @@ def parse(self, target_path: str = "") -> list:
5050
for target in self.targets:
5151
path = os.path.join("udx", "bldg", target + "_bldg")
5252
if name.find(path) >= 0:
53-
hit_targets.append(target)
53+
hit_targets.append(name)
5454
if len(hit_targets) == 0:
5555
raise ValueError(f"target_path: {target_path} is not target")
56-
# zipファイルを解凍する
57-
unarchived_dir = target_path.replace(".zip", "")
58-
shutil.unpack_archive(target_path, unarchived_dir)
5956
# 返り値を作成
6057
return_list = []
61-
# 解凍したファイルをパースする
62-
for target in hit_targets:
63-
# ファイルパスを作成
64-
target_file_path = os.path.join(
65-
unarchived_dir, "*", "udx", "bldg", target + "_bldg_*.gml"
66-
)
67-
# ファイルが二つ以上ある場合に対応させる
68-
for file_path in glob.glob(target_file_path):
58+
with zipfile.ZipFile(target_path) as zip_file:
59+
# 解凍したファイルをパースする
60+
for target in hit_targets:
6961
# XMLのオブジェクトとして読み込む
70-
tree = ET.parse(file_path)
71-
root = tree.getroot()
62+
root = ET.fromstring(zip_file.read(target))
7263
# namespaceを作成
7364
ns = {
7465
"core": "http://www.opengis.net/citygml/2.0",
@@ -127,7 +118,7 @@ def parse(self, target_path: str = "") -> list:
127118
if version is None:
128119
raise ValueError("version is None")
129120
# パース処理を実施する
130-
ret = self._parse(root, file_path, ns, version)
121+
ret = self._parse(root, target, zip_file, ns, version)
131122
# 返り値に追加
132123
return_list.extend(ret)
133124
# 返り値を返却
@@ -175,7 +166,14 @@ def _target_list(self, polygon: Polygon = None) -> list:
175166
# 返り値を返す
176167
return return_list
177168

178-
def _parse(self, root: ET.Element, file_path: str, ns: dict, version: int) -> list:
169+
def _parse(
170+
self,
171+
root: ET.Element,
172+
target: str,
173+
zip_file: zipfile.ZipFile,
174+
ns: dict,
175+
version: int,
176+
) -> list:
179177
# 返り値を作成
180178
return_list = []
181179
# core:cityObjectMemberの一覧を取得
@@ -206,12 +204,10 @@ def _parse(self, root: ET.Element, file_path: str, ns: dict, version: int) -> li
206204
# uro:codeSpaceを取得
207205
code_space = building_structure_type.get("codeSpace")
208206
# codeSpaceから値を取得
209-
code_space_root = ET.parse(
210-
os.path.abspath(os.path.join(file_path, "..", code_space))
211-
)
212-
code_space_root_root = code_space_root.getroot()
207+
code_space_path = os.path.normpath(os.path.join(target, "..", code_space))
208+
code_space_root = ET.fromstring(zip_file.read(code_space_path))
213209
building_structure_type_text = None
214-
for code_space_root_root_child in code_space_root_root.findall(
210+
for code_space_root_root_child in code_space_root.findall(
215211
".//gml:dictionaryEntry", ns
216212
):
217213
gml_name = code_space_root_root_child.find(".//gml:name", ns)

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.6"
14+
version = "0.0.7"
1515
dependencies = [
1616
"click",
1717
"numpy",

0 commit comments

Comments
 (0)