Skip to content

Commit

Permalink
Merge pull request #39 from eukarya-inc/dev-38
Browse files Browse the repository at this point in the history
#38 read zip file directly
  • Loading branch information
smellman authored Aug 29, 2023
2 parents 85125a6 + 3282a1d commit 760f65e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
38 changes: 17 additions & 21 deletions plateauutils/parser/city_gml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,16 @@ def parse(self, target_path: str = "") -> list:
for target in self.targets:
path = os.path.join("udx", "bldg", target + "_bldg")
if name.find(path) >= 0:
hit_targets.append(target)
hit_targets.append(name)
if len(hit_targets) == 0:
raise ValueError(f"target_path: {target_path} is not target")
# zipファイルを解凍する
unarchived_dir = target_path.replace(".zip", "")
shutil.unpack_archive(target_path, unarchived_dir)
# 返り値を作成
return_list = []
# 解凍したファイルをパースする
for target in hit_targets:
# ファイルパスを作成
target_file_path = os.path.join(
unarchived_dir, "*", "udx", "bldg", target + "_bldg_*.gml"
)
# ファイルが二つ以上ある場合に対応させる
for file_path in glob.glob(target_file_path):
with zipfile.ZipFile(target_path) as zip_file:
# 解凍したファイルをパースする
for target in hit_targets:
# XMLのオブジェクトとして読み込む
tree = ET.parse(file_path)
root = tree.getroot()
root = ET.fromstring(zip_file.read(target))
# namespaceを作成
ns = {
"core": "http://www.opengis.net/citygml/2.0",
Expand Down Expand Up @@ -127,7 +118,7 @@ def parse(self, target_path: str = "") -> list:
if version is None:
raise ValueError("version is None")
# パース処理を実施する
ret = self._parse(root, file_path, ns, version)
ret = self._parse(root, target, zip_file, ns, version)
# 返り値に追加
return_list.extend(ret)
# 返り値を返却
Expand Down Expand Up @@ -175,7 +166,14 @@ def _target_list(self, polygon: Polygon = None) -> list:
# 返り値を返す
return return_list

def _parse(self, root: ET.Element, file_path: str, ns: dict, version: int) -> list:
def _parse(
self,
root: ET.Element,
target: str,
zip_file: zipfile.ZipFile,
ns: dict,
version: int,
) -> list:
# 返り値を作成
return_list = []
# core:cityObjectMemberの一覧を取得
Expand Down Expand Up @@ -206,12 +204,10 @@ def _parse(self, root: ET.Element, file_path: str, ns: dict, version: int) -> li
# uro:codeSpaceを取得
code_space = building_structure_type.get("codeSpace")
# codeSpaceから値を取得
code_space_root = ET.parse(
os.path.abspath(os.path.join(file_path, "..", code_space))
)
code_space_root_root = code_space_root.getroot()
code_space_path = os.path.normpath(os.path.join(target, "..", code_space))
code_space_root = ET.fromstring(zip_file.read(code_space_path))
building_structure_type_text = None
for code_space_root_root_child in code_space_root_root.findall(
for code_space_root_root_child in code_space_root.findall(
".//gml:dictionaryEntry", ns
):
gml_name = code_space_root_root_child.find(".//gml:name", ns)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ classifiers = [
"Development Status :: 2 - Pre-Alpha",
"Programming Language :: Python :: 3",
]
version = "0.0.6"
version = "0.0.7"
dependencies = [
"click",
"numpy",
Expand Down

0 comments on commit 760f65e

Please sign in to comment.