@@ -319,35 +319,43 @@ def generate_graph(
319319 )
320320 else :
321321 raise ValueError (f"Unknown source '{ source } '. Use 'random' or 'osm'." )
322-
323- def export_graph (self , G : nx .Graph = None , path : str = "graph.graphml" ):
322+
323+ def export_graph (self , G : nx .Graph = None , source : str = None , path : str = "graph.graphml" ):
324324 """
325- Export the graph to a GraphML format.
325+ Export the graph to GraphML format.
326326
327- args:
328- G: Optional Networkx graph. If None, uses self.G.
327+ Args:
328+ G: Optional NetworkX graph. If None, uses self.G.
329+ source: Optional string, if "osm" uses osmnx for export.
329330 path: Destination file path for .graphml output.
330-
331+
331332 Notes:
332- GraphML is useful for visualization in tools like G.V(), Gephi or Cytoscape.
333- It supports node and edge attributes but may not handle complex types like tuples .
333+ GraphML is useful for visualization in tools like Gephi or Cytoscape.
334+ Node/ edge attributes should be simple types (str, int, float) .
334335 """
335336 import os
336337
338+ abs_path = os .path .abspath (path )
339+ os .makedirs (os .path .dirname (abs_path ) or "." , exist_ok = True )
340+
337341 if G is None :
338342 G = self .G
339343 if G is None :
340344 raise ValueError ("No graph available to export." )
341-
345+
342346 # Sanitize attributes that are not GraphML-friendly
343347 for _ , data in G .nodes (data = True ):
344348 if 'coordinates' in data and isinstance (data ['coordinates' ], tuple ):
345349 lat , lon = data ['coordinates' ]
346350 data ['coordinates' ] = f"{ lat } ,{ lon } "
347351
348- # Ensure directory exists
349- abs_path = os .path .abspath (path )
350- os .makedirs (os .path .dirname (abs_path ) or "." , exist_ok = True )
352+ if source == "osm" :
353+ try :
354+ import osmnx as ox
355+ ox .io .save_graphml (G , filepath = abs_path )
356+ except ImportError :
357+ raise ImportError ("osmnx is required to export OSM graphs." )
358+ else :
359+ nx .write_graphml (G , abs_path )
351360
352- nx .write_graphml (G , path )
353- print (f"✅ Graph exported to: { path } " )
361+ print (f"✅ Graph exported to: { abs_path } " )
0 commit comments