diff --git a/plateauutils/parser/city_gml_parser.py b/plateauutils/parser/city_gml_parser.py index 7aff0da..b04a296 100644 --- a/plateauutils/parser/city_gml_parser.py +++ b/plateauutils/parser/city_gml_parser.py @@ -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", @@ -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) # 返り値を返却 @@ -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の一覧を取得 @@ -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) diff --git a/pyproject.toml b/pyproject.toml index a522f30..f9407c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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",