Skip to content

Commit

Permalink
chore: Corrected logic that was preventing us from exporting the inde…
Browse files Browse the repository at this point in the history
…x in the geodataframes
  • Loading branch information
Carsopre committed Dec 11, 2024
1 parent b709e5d commit 4c06436
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 11 additions & 6 deletions ra2ce/network/exporters/multi_graph_network_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from pathlib import Path
from typing import Optional

from geopandas import GeoDataFrame

from ra2ce.network.exporters.geodataframe_network_exporter import (
GeoDataFrameNetworkExporter,
)
Expand All @@ -43,13 +45,16 @@ def export_to_gpkg(self, output_dir: Path, export_data: MULTIGRAPH_TYPE) -> None

_nodes_graph, _edges_graph = get_nodes_and_edges_from_origin_graph(export_data)

# Export through the single gdf exporter
_gdf_exporter = GeoDataFrameNetworkExporter(basename=self.basename)
_gdf_exporter.basename = self.basename + "_edges"
_gdf_exporter.export_to_gpkg(output_dir, _edges_graph)
def export_gdf(gdf_data: GeoDataFrame, suffix: str):
"""
Different from `GeoDataFrameNetworkExporter` at `index=True`.
"""
_export_file = output_dir.joinpath(self.basename + suffix + ".gpkg")
gdf_data.to_file(_export_file, index=True, driver="GPKG", encoding="utf-8")
logging.info("Saved %s in %s.", _export_file.stem, output_dir)

_gdf_exporter.basename = self.basename + "_nodes"
_gdf_exporter.export_to_gpkg(output_dir, _nodes_graph)
export_gdf(_edges_graph, "_edges")
export_gdf(_nodes_graph, "_nodes")

def export_to_pickle(self, output_dir: Path, export_data: MULTIGRAPH_TYPE) -> None:
self.pickle_path = output_dir.joinpath(self.basename + ".p")
Expand Down
3 changes: 2 additions & 1 deletion ra2ce/network/networks_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,8 @@ def get_nodes_and_edges_from_origin_graph(
resulting tuple of formatted `gpd.GeoDataFrame` for nodes and edges.
"""
# now only multidigraphs and graphs are used
if isinstance(origin_graph, nx.Graph):
if type(origin_graph) == nx.Graph:
# isinstance / issubclass will not work as nx.MultiGraph would return True
origin_graph = nx.MultiGraph(origin_graph)

# The nodes should have a geometry attribute (perhaps on top of the x and y attributes)
Expand Down

0 comments on commit 4c06436

Please sign in to comment.