Skip to content

Commit aa962f6

Browse files
committed
Fixed Task - 1347
- Fixed [Task-1347](https://dev.azure.com/TDEI-UW/TDEI/_workitems/edit/1347/). - Fixed package to removing the additional keys from the geojson files. - Introduced garbage collection to free up memory.
1 parent 59c26f2 commit aa962f6

File tree

6 files changed

+49
-8
lines changed

6 files changed

+49
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### 0.0.2
2+
- Fixed [Task-1347](https://dev.azure.com/TDEI-UW/TDEI/_workitems/edit/1347/).
3+
- Fixed package to removing the additional keys from the geojson files.
4+
- Introduced garbage collection to free up memory.
5+
6+
17
### 0.0.1
28
- Introduces osw_inclination package which calculates the inclination of the sidewalk based on the DEM data.
39
- Added example.py file which demonstrates how to use the package.

src/osw_incline/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import time
23
from typing import List
34
from pathlib import Path
@@ -36,13 +37,17 @@ def calculate(self):
3637
)
3738
end_time = time.time()
3839
time_taken = end_time - start_time
40+
del osm_graph
41+
del dem_processor
3942
if self.debug:
4043
Logger.info(f'Entire processing took: {time_taken} seconds')
4144
return True
4245
except Exception as e:
4346
if self.debug:
4447
Logger.error(f'Error processing DEM files: {e}')
4548
raise Exception(f'Error processing DEM files: {e}')
49+
finally:
50+
gc.collect()
4651

4752

4853

src/osw_incline/dem_processor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import math
23
import pyproj
34
import rasterio
@@ -39,6 +40,7 @@ def process(self, nodes_path, edges_path):
3940
if self.debug:
4041
Logger.info(f'No geometry found for edge {u}-{v}')
4142

43+
dem.close()
4244
self.OG.to_geojson(nodes_path, edges_path)
4345
except rasterio.errors.RasterioIOError:
4446
if self.debug:
@@ -48,6 +50,11 @@ def process(self, nodes_path, edges_path):
4850
if self.debug:
4951
Logger.error(f'Error processing DEM file: {dem_file_path}, error: {e}')
5052
raise Exception(f'Error processing DEM file: {dem_file_path}, error: {e}')
53+
finally:
54+
gc.collect()
55+
56+
del self.OG
57+
gc.collect()
5158

5259
def infer_incline(self, linestring, dem, precision=3):
5360
first_point = linestring.coords[0]

src/osw_incline/osm_graph.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import gc
12
import json
23
import pyproj
34
import networkx as nx
@@ -36,11 +37,24 @@ def from_geojson(cls, nodes_path, edges_path):
3637
props['geometry'] = shape(edge_feature['geometry'])
3738
G.add_edge(u, v, **props)
3839

40+
del nodes_fc
41+
del edges_fc
42+
gc.collect()
43+
3944
return osm_graph
4045

4146
def to_geojson(self, *args):
4247
nodes_path = args[0]
4348
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
4458
edge_features = []
4559
for u, v, d in self.G.edges(data=True):
4660
d_copy = {**d}
@@ -58,8 +72,11 @@ def to_geojson(self, *args):
5872
'geometry': geometry,
5973
'properties': d_copy
6074
})
61-
edges_fc = {'type': 'FeatureCollection', 'features': edge_features}
6275

76+
# Update the original edges feature collection
77+
original_edges_fc['features'] = edge_features
78+
79+
# Process the nodes
6380
node_features = []
6481
for n, d in self.G.nodes(data=True):
6582
d_copy = {**d}
@@ -82,14 +99,18 @@ def to_geojson(self, *args):
8299
'geometry': geometry,
83100
'properties': d_copy
84101
})
85-
nodes_fc = {'type': 'FeatureCollection', 'features': node_features}
86102

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
87107
with open(edges_path, 'w') as f:
88-
json.dump(edges_fc, f)
108+
json.dump(original_edges_fc, f)
89109

90110
with open(nodes_path, 'w') as f:
91-
json.dump(nodes_fc, f)
111+
json.dump(original_nodes_fc, f)
92112

113+
# Handle points if the third argument (points_path) is provided
93114
if len(args) == 3:
94115
points_path = args[2]
95116
point_features = []
@@ -116,7 +137,10 @@ def to_geojson(self, *args):
116137
'geometry': geometry,
117138
'properties': d_copy
118139
})
119-
points_fc = {'type': 'FeatureCollection', 'features': point_features}
120140

121141
with open(points_path, 'w') as f:
122-
json.dump(points_fc, f)
142+
json.dump({'type': 'FeatureCollection', 'features': point_features}, f)
143+
144+
del original_nodes_fc
145+
del original_edges_fc
146+
gc.collect()

src/osw_incline/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.0.1'
1+
__version__ = '0.0.2'

tests/test_dem_processor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ def test_interpolated_value_return_scaled(self, mock_rasterio_open):
401401

402402
# Assert that the result is not None and that it is scaled properly
403403
self.assertIsNotNone(result, 'IDW interpolation should return a valid value')
404-
print(f"IDW Interpolated Value (scaled): {result}")
405404

406405
# You can adjust the expected result based on the IDW logic; here, just check that it's non-zero
407406
self.assertGreater(result, 0, 'The interpolated result should be greater than 0')

0 commit comments

Comments
 (0)