lokesh341 commited on
Commit
25edf60
·
1 Parent(s): 576718e

Update services/map_service.py

Browse files
Files changed (1) hide show
  1. services/map_service.py +34 -30
services/map_service.py CHANGED
@@ -1,37 +1,41 @@
1
- import folium
2
- import os
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'] == 'Large' else 'orange'
16
- popup = f"Crack: {item['severity']}, Confidence: {item['confidence']:.2f}"
17
- else:
18
- color = 'pink'
19
- popup = f"Object: {item['label']}, Confidence: {item['confidence']:.2f}"
20
- folium.Marker(
21
- location=coord,
22
- popup=popup,
23
- icon=folium.Icon(color=color)
24
- ).add_to(m)
 
 
 
 
 
 
 
 
25
 
26
- map_path = f"map_temp_{uuid.uuid4().hex}.html"
27
- m.save(map_path)
28
-
29
- # Convert HTML map to PNG
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