surveillance143 / services /map_service.py
lokesh341's picture
Update services/map_service.py
bb5ad16 verified
import matplotlib.pyplot as plt
import numpy as np
from typing import List, Dict, Any, Optional
def generate_map(gps_coordinates: List[List[float]], map_items: List[Dict[str, Any]]) -> Optional[str]:
"""
Generate a map showing issue locations based on GPS coordinates.
Args:
gps_coordinates: List of [latitude, longitude] coordinates.
map_items: List of detected items (cracks, holes, potholes, missing patches) to plot.
Returns:
Path to the generated map image, or None if generation fails.
"""
try:
fig, ax = plt.subplots(figsize=(5, 3))
if gps_coordinates:
lats, longs = zip(*gps_coordinates)
ax.plot(longs, lats, 'b-', label='Path') # Blue line for path
for item in map_items:
gps = item.get('gps', [0, 0])
lat, lon = gps
# Use a default severity if the key is missing
severity = item.get('severity', 'Moderate') # Default to 'Moderate'
dtype = item.get('type', '')
# Custom colors for holes as per requirement
if dtype in ['hole', 'pothole']:
if severity == 'Mild':
color = 'darkgreen' # Green dot for minor holes
elif severity == 'Moderate':
color = 'yellow' # Yellow dot for moderate holes
else:
color = 'darkred' # Severe holes remain dark red
else:
# Existing logic for other types
color = 'darkred' if severity == 'Severe' else 'yellow' if severity == 'Moderate' else 'darkgreen'
marker = 'x' if dtype == 'crack' else 'o' if dtype in ['hole', 'pothole'] else '^'
ax.scatter(lon, lat, c=color, marker=marker, label=dtype.capitalize(), s=100)
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_title('Issue Locations')
handles, labels = ax.get_legend_handles_labels()
by_label = dict(zip(labels, handles))
ax.legend(by_label.values(), by_label.keys())
ax.grid(True)
map_path = "map_temp.png"
fig.savefig(map_path, bbox_inches='tight')
plt.close(fig)
return map_path
except Exception as e:
print(f"Error generating map: {str(e)}")
return None