44import networkx as nx
55from shapely .geometry import shape , mapping
66
7+ SCHEMA = 'https://sidewalks.washington.edu/opensidewalks/0.2/schema.json'
78
89class OSMGraph :
910 def __init__ (self , G = None ):
@@ -30,14 +31,16 @@ def from_geojson(cls, nodes_path, edges_path):
3031 props ['geometry' ] = shape (node_feature ['geometry' ])
3132 G .add_node (n , ** props )
3233
34+ del nodes_fc
35+ gc .collect ()
36+
3337 for edge_feature in edges_fc ['features' ]:
3438 props = edge_feature ['properties' ]
3539 u = props .pop ('_u_id' )
3640 v = props .pop ('_v_id' )
3741 props ['geometry' ] = shape (edge_feature ['geometry' ])
3842 G .add_edge (u , v , ** props )
3943
40- del nodes_fc
4144 del edges_fc
4245 gc .collect ()
4346
@@ -46,15 +49,6 @@ def from_geojson(cls, nodes_path, edges_path):
4649 def to_geojson (self , * args ):
4750 nodes_path = args [0 ]
4851 edges_path = args [1 ]
49-
50- # Load the original files to retain the original top-level keys
51- with open (nodes_path ) as f :
52- original_nodes_fc = json .load (f )
53-
54- with open (edges_path ) as f :
55- original_edges_fc = json .load (f )
56-
57- # Process the edges
5852 edge_features = []
5953 for u , v , d in self .G .edges (data = True ):
6054 d_copy = {** d }
@@ -72,11 +66,19 @@ def to_geojson(self, *args):
7266 'geometry' : geometry ,
7367 'properties' : d_copy
7468 })
69+ edges_fc = {
70+ 'type' : 'FeatureCollection' ,
71+ 'features' : edge_features ,
72+ '$schema' : SCHEMA
73+ }
7574
76- # Update the original edges feature collection
77- original_edges_fc ['features' ] = edge_features
75+ with open (edges_path , 'w' ) as f :
76+ json .dump (edges_fc , f )
77+
78+ # Delete edge_features and force garbage collection
79+ del edge_features , edges_fc
80+ gc .collect ()
7881
79- # Process the nodes
8082 node_features = []
8183 for n , d in self .G .nodes (data = True ):
8284 d_copy = {** d }
@@ -99,18 +101,19 @@ def to_geojson(self, *args):
99101 'geometry' : geometry ,
100102 'properties' : d_copy
101103 })
102-
103- # Update the original nodes feature collection
104- original_nodes_fc ['features' ] = node_features
105-
106- # Write the updated nodes and edges to the files
107- with open (edges_path , 'w' ) as f :
108- json .dump (original_edges_fc , f )
104+ nodes_fc = {
105+ 'type' : 'FeatureCollection' ,
106+ 'features' : node_features ,
107+ '$schema' : SCHEMA
108+ }
109109
110110 with open (nodes_path , 'w' ) as f :
111- json .dump (original_nodes_fc , f )
111+ json .dump (nodes_fc , f )
112+
113+ # Delete node_features and force garbage collection
114+ del node_features , nodes_fc
115+ gc .collect ()
112116
113- # Handle points if the third argument (points_path) is provided
114117 if len (args ) == 3 :
115118 points_path = args [2 ]
116119 point_features = []
@@ -137,10 +140,15 @@ def to_geojson(self, *args):
137140 'geometry' : geometry ,
138141 'properties' : d_copy
139142 })
143+ points_fc = {
144+ 'type' : 'FeatureCollection' ,
145+ 'features' : point_features ,
146+ '$schema' : SCHEMA
147+ }
140148
141149 with open (points_path , 'w' ) as f :
142- json .dump ({ 'type' : 'FeatureCollection' , 'features' : point_features } , f )
150+ json .dump (points_fc , f )
143151
144- del original_nodes_fc
145- del original_edges_fc
146- gc .collect ()
152+ # Delete point_features and force garbage collection
153+ del point_features , points_fc
154+ gc .collect ()
0 commit comments