Skip to content

Commit

Permalink
Fix geojson export (#76)
Browse files Browse the repository at this point in the history
* Add command line programs to collect metadata and export geojson

* Add command line programs to collect metadata and export geojson

* Update tests

* Fix tests and some warnings

* Cleanup

* Change CLI for json export

* Fix merge conflicts
  • Loading branch information
wpreimes committed Feb 7, 2024
1 parent b84092d commit 57ec363
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
25 changes: 18 additions & 7 deletions src/ismn/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ def collect_metadata(data_path, meta_path, parallel):
"If the file already exists it will be overwritten. "
"If not specified this is a file called "
"`ismn_sensors.json` and stored in the DATA_PATH.")
@click.option('--markercolor', '-m',
type=click.STRING, default='"#00aa00"', show_default=True,
help='Hex color (USE QUOTES!, e.g. "#00aa00") to assign to '
'markers in json file. The default color is green.')
def export_geojson(data_path, file_out, markercolor):
@click.option('--field', '-f', multiple=True,
help="Fields to include. This option can be called multiple times"
"with different fields. Allowed are: "
"network, station, sensor, depth, timerange. \n "
"Or any sensor properties (also custom ones) that have a "
"value.")
def export_geojson(data_path, file_out, field):
"""
Calls
Command line program to initialise ISMN metadata collection. THIS WILL
Expand All @@ -64,16 +66,25 @@ def export_geojson(data_path, file_out, markercolor):
"""
# The docstring above is slightly different to the normal python one to
# display it properly on the command line.
markercolor = str(markercolor.replace('"', '').replace("'", ""))
if not os.path.exists(data_path):
raise ValueError("The passed DATA_PATH does not exist.")
ds = ISMN_Interface(data_path)
if file_out is None:
file_out = os.path.join(ds.root.root_dir, 'ismn_sensors.json')
os.makedirs(os.path.dirname(file_out), exist_ok=True)
print(f"Exporting geojson to: {file_out}")
ds.collection.export_geojson(file_out, markercolor=markercolor)

kwargs = {}
field = [f.lower() for f in field]
for opt in ['network', 'station', 'sensor', 'depth', 'timerange']:
if opt in field:
field.pop(field.index(opt))
kwargs[opt] = True
else:
kwargs[opt] = False

kwargs['extra_props'] = field if len(field) > 0 else None
ds.collection.export_geojson(file_out, **kwargs)

@click.group(short_help="ISMN Command Line Programs.")
def ismn():
Expand Down
26 changes: 20 additions & 6 deletions src/ismn/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,9 @@ def export_citations(self, out_file=None):

return refs

def export_geojson(self, path, markercolor="#00aa00",
network=True, station=True, sensor=False,
depth=True, extra_props=None, **filter_kwargs):
def export_geojson(self, path, network=True, station=True, sensor=False,
depth=True, timerange=True, extra_props=None,
**filter_kwargs):
"""
Filter sensors in collection and create geojson file containing all
features.
Expand All @@ -843,11 +843,13 @@ def export_geojson(self, path, markercolor="#00aa00",
If True, sensor names are included in geojson file
depth: bool, optional (default: True)
If True, depth_from and depth_to are included in geojson file
timerange: bool, optional (default: True)
If True, timerange_from and timerange_to are included in geojson
extra_props: list[str], optional (default: None)
List of extra properties from sensor metadata to include in
geojson file
By default only depth_from and depth_to are included
e.g. ['timerange_from', 'timerange_to', 'variable', 'frm_class']
e.g. ['variable', 'frm_class'] etc.
filter_kwargs:
Keyword arguments to filter sensors in collection
see :func:`ismn.components.Sensor.eval`
Expand All @@ -869,7 +871,6 @@ def export_geojson(self, path, markercolor="#00aa00",
],
},
"properties": {
"markerColor": markercolor,
"datasetProperties": []
}
}
Expand Down Expand Up @@ -905,8 +906,21 @@ def export_geojson(self, path, markercolor="#00aa00",
"propertyValue": str(sens.depth[1])
}
]

if timerange:
feature["properties"]["datasetProperties"] += [
{
"propertyName": "timerange_from",
"propertyValue": str(sens.metadata["timerange_from"].val)
},
{
"propertyName": "timerange_to",
"propertyValue": str(sens.metadata["timerange_to"].val)
}
]
for prop in extra_props:
if prop not in sens.metadata:
raise KeyError(f"No sensor property '{prop}' found. "
f"Choose one of {sens.metadata.keys()}.")
feature["properties"]["datasetProperties"] += [
{
"propertyName": prop,
Expand Down
9 changes: 7 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ def test_cli_export_geojson():
result = runner.invoke(export_geojson,
[data_path, "--file_out",
os.path.join(tempdir, "test.geojson"),
"-m", "testcolor"])
"-f", "network",
'-f', "timerange",
"-f", "lc_2010"])
assert result.exit_code == 0
assert os.path.isfile(os.path.join(tempdir, "test.geojson"))
with open(os.path.join(tempdir, "test.geojson"), "r") as f:
content = f.readlines()
assert "testcolor" in content[0]
assert "network" in content[0]
assert "timerange_from" in content[0]
assert "timerange_to" in content[0]
assert "lc_2010" in content[0]
2 changes: 1 addition & 1 deletion tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_references(self):
def test_json_dump(self):
with TemporaryDirectory() as temp:
self.netcol.export_geojson(os.path.join(temp, "meta.json"),
sensor=True)
sensor=True, timerange=False)

with open(os.path.join(temp, "meta.json")) as f:
meta_dict = json.load(f)
Expand Down

0 comments on commit 57ec363

Please sign in to comment.