From e767750a295faa321635e035fdd78c190fd977bb Mon Sep 17 00:00:00 2001 From: Natalie Schultz <90212258+nataliejschultz@users.noreply.github.com> Date: Mon, 12 Aug 2024 17:35:32 -0600 Subject: [PATCH 1/4] Add departure headings Add departure headings calculation and output departure headings as a separate column in edges-headings-enumerated.csv. --- .../routee/compass/io/generate_dataset.py | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/python/nrel/routee/compass/io/generate_dataset.py b/python/nrel/routee/compass/io/generate_dataset.py index a3d415bd..9d6b9c59 100644 --- a/python/nrel/routee/compass/io/generate_dataset.py +++ b/python/nrel/routee/compass/io/generate_dataset.py @@ -5,6 +5,8 @@ import importlib.resources import logging import shutil +import math +import pandas as pd from nrel.routee.compass.io.utils import add_grade_to_graph @@ -154,13 +156,8 @@ def replace_id(vertex_uuid): header=False, ) - headings = e.bearing.fillna(0).apply(lambda x: int(round(x))) - headings_df = headings.to_frame(name="arrival_heading") - - # We could get more sophisticated and compute the end heading - # for links that might have some significant curvature, but - # for now we'll just use the start heading. - headings_df["departure_heading"] = None + headings = [calculate_bearings(i) for i in e.geometry.values] + headings_df = pd.DataFrame(headings, columns = ["arrival_heading", "departure_heading"]) headings_df.to_csv( output_directory / "edges-headings-enumerated.csv.gz", index=False, @@ -209,3 +206,38 @@ def replace_id(vertex_uuid): with importlib.resources.as_file(model_file) as model_path: model_dst = model_output_directory / model_path.name shutil.copy(model_path, model_dst) + +#Function written by Nick Reinicke +def compass_heading(point1, point2): + lon1, lat1 = point1 + lon2, lat2 = point2 + + lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) + + dlon = lon2 - lon1 + + x = math.sin(dlon) * math.cos(lat2) + y = math.cos(lat1) * math.sin(lat2) - ( + math.sin(lat1) * math.cos(lat2) * math.cos(dlon) + ) + + initial_bearing = math.atan2(x, y) + + initial_bearing = math.degrees(initial_bearing) + compass_bearing = (initial_bearing + 360) % 360 + + return compass_bearing + +#Function written by Nick Reinicke +def calculate_bearings(geom): + if len(geom.coords) < 2: + raise ValueError("Geometry must have at least two points") + if len(geom.coords) == 2: + # start and end heading is equal + heading = int(compass_heading(geom.coords[0], geom.coords[1])) + return (heading, heading) + else: + start_heading = int(compass_heading(geom.coords[0], geom.coords[1])) + end_heading = int(compass_heading(geom.coords[-2], geom.coords[-1])) + #returns headings as a list of tuples + return (start_heading, end_heading) \ No newline at end of file From 7d91706e4670e12d0e34033e3241910feabd30ab Mon Sep 17 00:00:00 2001 From: Natalie Schultz <90212258+nataliejschultz@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:37:09 -0600 Subject: [PATCH 2/4] Removing functions + updating headings command Removed the two functions for calculating bearings and put them into utils.py. Changed the calculate_bearings call accordingly. --- .../routee/compass/io/generate_dataset.py | 39 +------------------ 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/python/nrel/routee/compass/io/generate_dataset.py b/python/nrel/routee/compass/io/generate_dataset.py index 9d6b9c59..f4c05b81 100644 --- a/python/nrel/routee/compass/io/generate_dataset.py +++ b/python/nrel/routee/compass/io/generate_dataset.py @@ -5,8 +5,8 @@ import importlib.resources import logging import shutil -import math import pandas as pd +import utils from nrel.routee.compass.io.utils import add_grade_to_graph @@ -156,7 +156,7 @@ def replace_id(vertex_uuid): header=False, ) - headings = [calculate_bearings(i) for i in e.geometry.values] + headings = [utils.calculate_bearings(i) for i in e.geometry.values] headings_df = pd.DataFrame(headings, columns = ["arrival_heading", "departure_heading"]) headings_df.to_csv( output_directory / "edges-headings-enumerated.csv.gz", @@ -206,38 +206,3 @@ def replace_id(vertex_uuid): with importlib.resources.as_file(model_file) as model_path: model_dst = model_output_directory / model_path.name shutil.copy(model_path, model_dst) - -#Function written by Nick Reinicke -def compass_heading(point1, point2): - lon1, lat1 = point1 - lon2, lat2 = point2 - - lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) - - dlon = lon2 - lon1 - - x = math.sin(dlon) * math.cos(lat2) - y = math.cos(lat1) * math.sin(lat2) - ( - math.sin(lat1) * math.cos(lat2) * math.cos(dlon) - ) - - initial_bearing = math.atan2(x, y) - - initial_bearing = math.degrees(initial_bearing) - compass_bearing = (initial_bearing + 360) % 360 - - return compass_bearing - -#Function written by Nick Reinicke -def calculate_bearings(geom): - if len(geom.coords) < 2: - raise ValueError("Geometry must have at least two points") - if len(geom.coords) == 2: - # start and end heading is equal - heading = int(compass_heading(geom.coords[0], geom.coords[1])) - return (heading, heading) - else: - start_heading = int(compass_heading(geom.coords[0], geom.coords[1])) - end_heading = int(compass_heading(geom.coords[-2], geom.coords[-1])) - #returns headings as a list of tuples - return (start_heading, end_heading) \ No newline at end of file From b9f77261ebed959dd01386be240f4b2ee06d9537 Mon Sep 17 00:00:00 2001 From: Natalie Schultz <90212258+nataliejschultz@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:38:03 -0600 Subject: [PATCH 3/4] Adding compass_heading and calculate_bearings functions Moving compass_heading and calculate_bearings functions over from generate_dataset.py --- python/nrel/routee/compass/io/utils.py | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/python/nrel/routee/compass/io/utils.py b/python/nrel/routee/compass/io/utils.py index 6cbab26a..7077737d 100644 --- a/python/nrel/routee/compass/io/utils.py +++ b/python/nrel/routee/compass/io/utils.py @@ -5,6 +5,7 @@ import logging from typing import Union, Optional +import math log = logging.getLogger(__name__) @@ -173,3 +174,37 @@ def add_grade_to_graph( g = ox.add_edge_grades(g) return g + +def compass_heading(point1, point2): + lon1, lat1 = point1 + lon2, lat2 = point2 + + lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) + + dlon = lon2 - lon1 + + x = math.sin(dlon) * math.cos(lat2) + y = math.cos(lat1) * math.sin(lat2) - ( + math.sin(lat1) * math.cos(lat2) * math.cos(dlon) + ) + + initial_bearing = math.atan2(x, y) + + initial_bearing = math.degrees(initial_bearing) + compass_bearing = (initial_bearing + 360) % 360 + + return compass_bearing + +def calculate_bearings(geom): + if len(geom.coords) < 2: + raise ValueError("Geometry must have at least two points") + if len(geom.coords) == 2: + # start and end heading is equal + heading = int(compass_heading(geom.coords[0], geom.coords[1])) + return (heading, heading) + else: + start_heading = int(compass_heading(geom.coords[0], geom.coords[1])) + end_heading = int(compass_heading(geom.coords[-2], geom.coords[-1])) + #returns headings as a list of tuples + return (start_heading, end_heading) + \ No newline at end of file From b6cbd623f0a46db11478fa61bc9262735dcd1658 Mon Sep 17 00:00:00 2001 From: Natalie Schultz <90212258+nataliejschultz@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:58:20 -0600 Subject: [PATCH 4/4] Updating utils import Fixing utils import --- python/nrel/routee/compass/io/generate_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/nrel/routee/compass/io/generate_dataset.py b/python/nrel/routee/compass/io/generate_dataset.py index f4c05b81..01316128 100644 --- a/python/nrel/routee/compass/io/generate_dataset.py +++ b/python/nrel/routee/compass/io/generate_dataset.py @@ -6,8 +6,8 @@ import logging import shutil import pandas as pd -import utils +from nrel.routee.compass.io import utils from nrel.routee.compass.io.utils import add_grade_to_graph log = logging.getLogger(__name__)