-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unnecessary try except #104
base: master
Are you sure you want to change the base?
Changes from all commits
503f432
a499553
9a06213
814ca05
88461d3
90f0581
b24da73
26710b8
c1cdcd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ | |
/build | ||
/documentation | ||
|
||
*.log | ||
*.log | ||
.coverage |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -20,68 +20,74 @@ | |||||||
Stichting Deltares and remain full property of Stichting Deltares at all times. | ||||||||
All rights reserved. | ||||||||
""" | ||||||||
|
||||||||
import os | ||||||||
|
||||||||
from pathlib import Path | ||||||||
from typing import Union, Optional | ||||||||
import geojson | ||||||||
|
||||||||
|
||||||||
|
||||||||
class MaskOutputFile: | ||||||||
@staticmethod | ||||||||
def create_mask_point(coords: geojson.coords, properties: dict): | ||||||||
def create_mask_point(coords: geojson.coords, properties: Optional[dict] = None) -> geojson.Feature: | ||||||||
"""Creates a Point based on the properties and coordinates given. | ||||||||
|
||||||||
Arguments: | ||||||||
coords {geojson.coords} -- | ||||||||
Coordinates tuple (x,y) for the mask point. | ||||||||
properties {dict} -- Dictionary of properties | ||||||||
""" | ||||||||
output_mask = geojson.Feature(geometry=geojson.Point(coords)) | ||||||||
if not coords: | ||||||||
raise ValueError("coords cannot be empty.") | ||||||||
output_mask = geojson.Feature(geometry=geojson.Point(coords)) | ||||||||
|
||||||||
if properties: | ||||||||
output_mask.properties = properties | ||||||||
return output_mask | ||||||||
|
||||||||
@staticmethod | ||||||||
def validate_extension(file_path: str): | ||||||||
if not file_path: | ||||||||
# Should not be evaluated | ||||||||
return | ||||||||
if not file_path.endswith(".json") and not file_path.endswith(".geojson"): | ||||||||
def validate_extension(file_path: Union[str, Path]) -> None: | ||||||||
if not isinstance(file_path, (str, Path)): | ||||||||
err_msg = f"file_path should be string or Path, not {type(file_path)}" | ||||||||
raise TypeError(err_msg) | ||||||||
if Path(file_path).suffix not in [".json", ".geojson"]: | ||||||||
raise IOError( | ||||||||
"Invalid file path extension, " + "should be .json or .geojson." | ||||||||
"Invalid file path extension, should be .json or .geojson." | ||||||||
) | ||||||||
|
||||||||
@staticmethod | ||||||||
def read_mask_output_file(file_path: str): | ||||||||
def read_mask_output_file(file_path: Union[str, Path]) -> dict: | ||||||||
"""Imports a GeoJson from a given json file path. | ||||||||
|
||||||||
Arguments: | ||||||||
file_path {str} -- Location of the json file | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
""" | ||||||||
geojson_data = geojson.FeatureCollection(None) | ||||||||
if not file_path or not os.path.exists(file_path): | ||||||||
return geojson_data | ||||||||
file_path = Path(file_path) | ||||||||
if not file_path.exists(): | ||||||||
err_msg = f"File path {file_path} not found." | ||||||||
raise FileNotFoundError(err_msg) | ||||||||
|
||||||||
MaskOutputFile.validate_extension(file_path) | ||||||||
with open(file_path) as geojson_file: | ||||||||
try: | ||||||||
geojson_data = geojson.load(geojson_file) | ||||||||
except: | ||||||||
return geojson_data | ||||||||
|
||||||||
with file_path.open("r") as geojson_file: | ||||||||
geojson_data = geojson.load(geojson_file) | ||||||||
if not isinstance(geojson_data, geojson.FeatureCollection): | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this check actually needed? Can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, geojson will load a json as a dict. If the file is a geojson it will return a geojson.FeatureCollection |
||||||||
raise IOError("File is empty or not a valid geojson file.") | ||||||||
return geojson_data | ||||||||
|
||||||||
@staticmethod | ||||||||
def write_mask_output_file(file_path: str, mask_points: list): | ||||||||
def write_mask_output_file(file_path: Union[Path, str], mask_points: list) -> None: | ||||||||
"""Writes a .geojson file with a Feature collection containing | ||||||||
the mask_points list given as input. | ||||||||
|
||||||||
Arguments: | ||||||||
file_path {str} -- file_path where to store the geojson. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
mask_points {list} -- List of features to output. | ||||||||
""" | ||||||||
if file_path: | ||||||||
MaskOutputFile.validate_extension(file_path) | ||||||||
feature_collection = geojson.FeatureCollection(mask_points) | ||||||||
with open(file_path, "w") as f: | ||||||||
geojson.dump(feature_collection, f, indent=4) | ||||||||
if not file_path: | ||||||||
raise ValueError("file_path is required.") | ||||||||
file_path = Path(file_path) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should first check if the given
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good suggestion, I added the check. |
||||||||
if not mask_points: | ||||||||
raise ValueError("mask_points cannot be empty.") | ||||||||
MaskOutputFile.validate_extension(file_path) | ||||||||
feature_collection = geojson.FeatureCollection(mask_points) | ||||||||
with file_path.open("w") as f: | ||||||||
geojson.dump(feature_collection, f, indent=4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The contributing page of the documentation suggets to use google style docstrings when writing new code. The docstrings throughout the code are not consistent at the moment. I'm working on a PR that will address this issue.