From 4a06896ba40b4b08001292331422235457df2695 Mon Sep 17 00:00:00 2001 From: selfsryo <65118816+selfsryo@users.noreply.github.com> Date: Sat, 3 Jul 2021 18:00:38 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BE=8B=E5=A4=96=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Pipfile | 2 +- README.md | 2 +- src/convert_fgd_dem/converter.py | 2 +- src/convert_fgd_dem/dem.py | 33 ++++++++++++++++++-------------- src/convert_fgd_dem/geotiff.py | 20 +++++++++++-------- src/convert_fgd_dem/helpers.py | 10 ++++++++++ 6 files changed, 44 insertions(+), 25 deletions(-) diff --git a/Pipfile b/Pipfile index 7e9e642..5a2c173 100644 --- a/Pipfile +++ b/Pipfile @@ -9,7 +9,7 @@ flake8 = "*" isort = "*" [packages] -numpy = "==1.20.1" +numpy = ">=1.20.1" [requires] python_version = "3.9" diff --git a/README.md b/README.md index d62e1f8..6014c8d 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Run the tool with downloaded "xml" or "directory containing .xml" or ".zip conta ```python from pathlib import Path -from convert_fgd_dem import Dem +from src.convert_fgd_dem import Dem def main(): diff --git a/src/convert_fgd_dem/converter.py b/src/convert_fgd_dem/converter.py index f6516b1..9579676 100644 --- a/src/convert_fgd_dem/converter.py +++ b/src/convert_fgd_dem/converter.py @@ -171,7 +171,7 @@ def dem_to_geotiff(self): geotiff = Geotiff(*data_for_geotiff) if self.rgbify: - root, ext = os.path.splitext(self.file_name) + os.path.splitext(self.file_name) geotiff.create( 3, gdal.GDT_Byte, diff --git a/src/convert_fgd_dem/dem.py b/src/convert_fgd_dem/dem.py index c7af912..d674a37 100644 --- a/src/convert_fgd_dem/dem.py +++ b/src/convert_fgd_dem/dem.py @@ -6,6 +6,8 @@ import numpy as np +from .helpers import DemInputXmlException + class Dem: """Retrieve metadata from DEM xml""" @@ -77,7 +79,7 @@ def _get_xml_paths(self): xml_paths = [ xml_path for xml_path in self.import_path.glob("*.xml")] if xml_paths is None: - raise Exception("指定ディレクトリに.xmlが存在しません") + raise DemInputXmlException("指定ディレクトリに.xmlが存在しません") elif self.import_path.suffix == ".xml": xml_paths = [self.import_path] @@ -88,9 +90,9 @@ def _get_xml_paths(self): xml_paths = [ xml_path for xml_path in extract_dir.glob("*.xml")] if not xml_paths: - raise Exception("指定のパスにxmlファイルが存在しません") + raise DemInputXmlException("指定のパスにxmlファイルが存在しません") else: - raise Exception( + raise DemInputXmlException( "指定できる形式は「xml」「.xmlが格納されたディレクトリ」「.xmlが格納された.zip」のみです") return xml_paths @@ -149,20 +151,24 @@ def get_xml_content(self, xml_path): dict: A dictionary containing mesh code, metadata, and elevation values """ if not xml_path.suffix == ".xml": - raise Exception("指定できる形式は.xmlのみです") + raise DemInputXmlException("指定できる形式は.xmlのみです") name_space = { "dataset": "http://fgd.gsi.go.jp/spec/2008/FGD_GMLSchema", "gml": "http://www.opengis.net/gml/3.2", } - tree = et.parse(xml_path) - root = tree.getroot() - - mesh_code = int( - root.find( - "dataset:DEM//dataset:mesh", - name_space).text) + try: + tree = et.parse(xml_path) + root = tree.getroot() + mesh_code = int( + root.find( + "dataset:DEM//dataset:mesh", + name_space + ).text + ) + except et.ParseError: + raise DemInputXmlException("不正なxmlです") raw_metadata = { "mesh_code": mesh_code, @@ -225,11 +231,10 @@ def _check_mesh_codes(self): elif len(str_mesh) == 8: third_mesh_codes.append(mesh_code) else: - raise Exception(f"メッシュコードが不正です。mesh_code={mesh_code}") + raise DemInputXmlException(f"メッシュコードが不正です。mesh_code={mesh_code}") - # どちらもTrue、つまり要素が存在しているときにraise if all((third_mesh_codes, second_mesh_codes)): - raise Exception("2次メッシュと3次メッシュが混合しています。") + raise DemInputXmlException("2次メッシュと3次メッシュが混合しています。") def _get_xml_content_list(self): """Create a list of metadata and elevation values""" diff --git a/src/convert_fgd_dem/geotiff.py b/src/convert_fgd_dem/geotiff.py index 23a8439..b54789d 100644 --- a/src/convert_fgd_dem/geotiff.py +++ b/src/convert_fgd_dem/geotiff.py @@ -4,6 +4,7 @@ import numpy as np from .helpers import ( + DemOutputTiffException, convert_height_to_R, convert_height_to_G, convert_height_to_B, @@ -98,14 +99,17 @@ def create( created_tiff_path = self.output_path / file_name driver = gdal.GetDriverByName("GTiff") - dst_ds = driver.Create( - str(created_tiff_path.resolve()), - self.x_length, - self.y_length, - band_count, - dtype - ) - dst_ds.SetGeoTransform(self.geo_transform) + try: + dst_ds = driver.Create( + str(created_tiff_path.resolve()), + self.x_length, + self.y_length, + band_count, + dtype + ) + dst_ds.SetGeoTransform(self.geo_transform) + except AttributeError: + raise DemOutputTiffException("出力先パスに問題がある可能性があります") self.write_raster_bands( rgbify, diff --git a/src/convert_fgd_dem/helpers.py b/src/convert_fgd_dem/helpers.py index 9999f1f..592a04e 100644 --- a/src/convert_fgd_dem/helpers.py +++ b/src/convert_fgd_dem/helpers.py @@ -5,6 +5,16 @@ from osgeo import gdal +class DemInputXmlException(Exception): + """Exception class about input DEM xml""" + pass + + +class DemOutputTiffException(Exception): + """Exception class about input DEM xml""" + pass + + def warp( file_name, source_path=None,