Skip to content
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

Elevation script fixes #170

Merged
merged 4 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions genet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
ServiceInitialisationError # noqa: F401
from genet.utils import graph_operations # noqa: F401
from genet.utils import google_directions # noqa: F401
from genet.utils import elevation # noqa: F401
3 changes: 2 additions & 1 deletion genet/utils/elevation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
from lxml import etree
import os
import logging


def get_elevation_image(elevation_tif):
Expand Down Expand Up @@ -72,7 +73,7 @@ def write_slope_xml(link_slope_dictionary, output_dir):
:param output_dir: directory where the XML file will be written to
"""
fname = os.path.join(output_dir, 'link_slopes.xml')
print('Writing {}'.format(fname))
logging.info(f'Writing {fname}')

with open(fname, "wb") as f, etree.xmlfile(f, encoding='UTF-8') as xf:
xf.write_declaration(
Expand Down
33 changes: 17 additions & 16 deletions scripts/add_elevation_to_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from genet.utils.persistence import ensure_dir
import genet.output.sanitiser as sanitiser
from genet.output.geojson import save_geodataframe
import genet.utils.elevation as elevation

if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -42,19 +41,22 @@

arg_parser.add_argument('-we',
'--write_elevation_to_network',
help='Whether node elevation data should be written as attribute to the network; defaults to True',
help='Whether node elevation data should be written as attribute to the network; '
'defaults to True',
default=True,
type=bool)

arg_parser.add_argument('-ws',
arg_parser.add_argument('-wsn',
'--write_slope_to_network',
help='Whether link slope data should be written as attribute to the network; defaults to True',
help='Whether link slope data should be written as attribute to the network; '
'defaults to True',
default=True,
type=bool)

arg_parser.add_argument('-ws',
arg_parser.add_argument('-wsoa',
'--write_slope_to_object_attribute_file',
help='Whether link slope data should be written to object attribute file; defaults to True',
help='Whether link slope data should be written to object attribute file; '
'defaults to True',
default=True,
type=bool)

Expand All @@ -74,8 +76,7 @@
write_slope_to_network = args['write_slope_to_network']
write_slope_to_object_attribute_file = args['write_slope_to_object_attribute_file']
save_dict_to_json = args['save_jsons']
elevation_output_dir = os.path.join(output_dir, 'elevation')
ensure_dir(elevation_output_dir)
ensure_dir(output_dir)

logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.WARNING)

Expand All @@ -90,16 +91,16 @@
elevation_dictionary = n.get_node_elevation_dictionary(elevation_tif_file_path=elevation, null_value=tif_null_value)

if save_dict_to_json:
with open(os.path.join(elevation_output_dir, 'node_elevation_dictionary.json'), 'w',
with open(os.path.join(output_dir, 'node_elevation_dictionary.json'), 'w',
encoding='utf-8') as f:
json.dump(sanitiser.sanitise_dictionary(elevation_dictionary), f, ensure_ascii=False, indent=4)

logging.info('Validating the node elevation data')
report = genet.utils.elevation.validation_report_for_node_elevation(elevation_dictionary)
report = genet.elevation.validation_report_for_node_elevation(elevation_dictionary)
logging.info(report['summary'])

if save_dict_to_json:
with open(os.path.join(elevation_output_dir, 'validation_report_for_elevation.json'), 'w',
with open(os.path.join(output_dir, 'validation_report_for_elevation.json'), 'w',
encoding='utf-8') as f:
json.dump(sanitiser.sanitise_dictionary(report), f, ensure_ascii=False, indent=4)

Expand All @@ -113,13 +114,13 @@

gdf_nodes = n.to_geodataframe()['nodes']
gdf_nodes = gdf_nodes[['id', 'z', 'geometry']]
save_geodataframe(gdf_nodes.to_crs('epsg:4326'), 'node_elevation', elevation_output_dir)
save_geodataframe(gdf_nodes.to_crs('epsg:4326'), 'node_elevation', output_dir)

logging.info('Creating slope dictionary for network links')
slope_dictionary = n.get_link_slope_dictionary(elevation_dict=elevation_dictionary)

if save_dict_to_json:
with open(os.path.join(elevation_output_dir, 'link_slope_dictionary.json'), 'w',
with open(os.path.join(output_dir, 'link_slope_dictionary.json'), 'w',
encoding='utf-8') as f:
json.dump(sanitiser.sanitise_dictionary(slope_dictionary), f, ensure_ascii=False, indent=4)

Expand All @@ -137,10 +138,10 @@
df['slope'] = [x['slope'] for x in df['slope_tuple']]
df = df[['id', 'slope']]
gdf_links = pd.merge(gdf, df, on='id')
save_geodataframe(gdf_links.to_crs('epsg:4326'), 'link_slope', elevation_output_dir)
save_geodataframe(gdf_links.to_crs('epsg:4326'), 'link_slope', output_dir)

if write_slope_to_object_attribute_file:
elevation.write_slope_xml(slope_dictionary, elevation_output_dir)
genet.elevation.write_slope_xml(slope_dictionary, output_dir)

logging.info('Writing the updated network')
n.write_to_matsim(elevation_output_dir)
n.write_to_matsim(output_dir)