Spaces:
Runtime error
Runtime error
Update services/map_service.py
Browse files- services/map_service.py +34 -30
services/map_service.py
CHANGED
@@ -1,37 +1,41 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import imgkit
|
4 |
-
import uuid
|
5 |
|
6 |
def generate_map(gps_coordinates, items):
|
7 |
-
if not gps_coordinates:
|
8 |
return None
|
9 |
-
avg_lat = sum(coord[0] for coord in gps_coordinates) / len(gps_coordinates)
|
10 |
-
avg_lon = sum(coord[1] for coord in gps_coordinates) / len(gps_coordinates)
|
11 |
-
m = folium.Map(location=[avg_lat, avg_lon], zoom_start=15)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
for coord, item in zip(gps_coordinates[-len(items):], items):
|
14 |
if item['type'] == 'crack':
|
15 |
-
color = 'red' if item['severity'] == '
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
map_path =
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
try:
|
31 |
-
img_path = map_path.replace('.html', '.png')
|
32 |
-
imgkit.from_file(map_path, img_path)
|
33 |
-
os.remove(map_path) # Clean up HTML file
|
34 |
-
return img_path
|
35 |
-
except Exception as e:
|
36 |
-
print(f"Error converting map to PNG: {str(e)}")
|
37 |
-
return None
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
import numpy as np
|
|
|
|
|
3 |
|
4 |
def generate_map(gps_coordinates, items):
|
5 |
+
if not gps_coordinates or not items:
|
6 |
return None
|
|
|
|
|
|
|
7 |
|
8 |
+
# Extract latitude and longitude
|
9 |
+
lats = [coord[0] for coord in gps_coordinates]
|
10 |
+
lons = [coord[1] for coord in gps_coordinates]
|
11 |
+
|
12 |
+
# Create a simple plot
|
13 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
14 |
+
ax.scatter(lons, lats, c='blue', label='Path', alpha=0.5)
|
15 |
+
|
16 |
+
# Plot cracks and holes
|
17 |
for coord, item in zip(gps_coordinates[-len(items):], items):
|
18 |
if item['type'] == 'crack':
|
19 |
+
color = 'red' if item['severity'] == 'Severe' else 'orange' if item['severity'] == 'Moderate' else 'green' if item['severity'] == 'Minor' else 'purple'
|
20 |
+
marker = 'x'
|
21 |
+
label = f"Crack: {item['severity']}"
|
22 |
+
else: # Hole
|
23 |
+
color = 'darkred' if item['severity'] == 'Severe' else 'yellow' if item['severity'] == 'Moderate' else 'darkgreen'
|
24 |
+
marker = 'o'
|
25 |
+
label = f"Hole: {item['severity']}"
|
26 |
+
ax.scatter(coord[1], coord[0], c=color, marker=marker, s=100, label=label)
|
27 |
+
|
28 |
+
ax.set_xlabel("Longitude")
|
29 |
+
ax.set_ylabel("Latitude")
|
30 |
+
ax.set_title("Crack/Hole Locations Map")
|
31 |
+
ax.grid(True)
|
32 |
+
|
33 |
+
# Remove duplicate labels in legend
|
34 |
+
handles, labels = ax.get_legend_handles_labels()
|
35 |
+
by_label = dict(zip(labels, handles))
|
36 |
+
ax.legend(by_label.values(), by_label.keys())
|
37 |
|
38 |
+
map_path = "map_temp.png"
|
39 |
+
plt.savefig(map_path)
|
40 |
+
plt.close(fig)
|
41 |
+
return map_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|