Spaces:
Runtime error
Runtime error
Update services/map_service.py
Browse files- services/map_service.py +39 -38
services/map_service.py
CHANGED
@@ -1,41 +1,42 @@
|
|
1 |
import matplotlib.pyplot as plt
|
2 |
import numpy as np
|
|
|
3 |
|
4 |
-
def generate_map(gps_coordinates,
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
1 |
import matplotlib.pyplot as plt
|
2 |
import numpy as np
|
3 |
+
from typing import List, Dict, Any, Optional
|
4 |
|
5 |
+
def generate_map(gps_coordinates: List[List[float]], map_items: List[Dict[str, Any]]) -> Optional[str]:
|
6 |
+
"""
|
7 |
+
Generate a map showing issue locations based on GPS coordinates.
|
8 |
+
Args:
|
9 |
+
gps_coordinates: List of [latitude, longitude] coordinates.
|
10 |
+
map_items: List of detected items (cracks, holes, missing patches) to plot.
|
11 |
+
Returns:
|
12 |
+
Path to the generated map image, or None if generation fails.
|
13 |
+
"""
|
14 |
+
try:
|
15 |
+
fig, ax = plt.subplots(figsize=(5, 3))
|
16 |
+
|
17 |
+
if gps_coordinates:
|
18 |
+
lats, longs = zip(*gps_coordinates)
|
19 |
+
ax.plot(longs, lats, 'b-', label='Path')
|
20 |
+
|
21 |
+
for item in map_items:
|
22 |
+
gps = item.get('gps', [0, 0])
|
23 |
+
lat, lon = gps
|
24 |
+
# Use a default severity if the key is missing
|
25 |
+
severity = item.get('severity', 'Moderate') # Default to 'Moderate'
|
26 |
+
color = 'darkred' if severity == 'Severe' else 'yellow' if severity == 'Moderate' else 'darkgreen'
|
27 |
+
marker = 'x' if item.get('type') == 'crack' else 'o' if item.get('type') == 'hole' else '^'
|
28 |
+
ax.scatter(lon, lat, c=color, marker=marker, label=item.get('type', 'Issue'), s=100)
|
29 |
+
|
30 |
+
ax.set_xlabel('Longitude')
|
31 |
+
ax.set_ylabel('Latitude')
|
32 |
+
ax.set_title('Issue Locations')
|
33 |
+
ax.legend()
|
34 |
+
ax.grid(True)
|
35 |
+
|
36 |
+
map_path = "map_temp.png"
|
37 |
+
fig.savefig(map_path, bbox_inches='tight')
|
38 |
+
plt.close(fig)
|
39 |
+
return map_path
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error generating map: {str(e)}")
|
42 |
+
return None
|