lokesh341 commited on
Commit
800c95b
·
verified ·
1 Parent(s): 0068c73

Update services/map_service.py

Browse files
Files changed (1) hide show
  1. 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, 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
 
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