Skip to content

Commit

Permalink
Merge pull request #12 from MIERUNE/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
selfsryo authored Sep 8, 2021
2 parents 488af4c + 141edef commit 12b7146
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ flake8 = "*"
isort = "*"

[packages]
numpy = "==1.20.1"
numpy = ">=1.20.1"

[requires]
python_version = "3.9"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion src/convert_fgd_dem/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 19 additions & 14 deletions src/convert_fgd_dem/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import numpy as np

from .helpers import DemInputXmlException


class Dem:
"""Retrieve metadata from DEM xml"""
Expand Down Expand Up @@ -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]
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"""
Expand Down
20 changes: 12 additions & 8 deletions src/convert_fgd_dem/geotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

from .helpers import (
DemOutputTiffException,
convert_height_to_R,
convert_height_to_G,
convert_height_to_B,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/convert_fgd_dem/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 12b7146

Please sign in to comment.