File size: 2,395 Bytes
25edf60
 
800c95b
2bcb413
800c95b
 
 
 
 
bb5ad16
800c95b
 
 
 
 
 
 
 
bb5ad16
800c95b
 
 
 
 
 
bb5ad16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
800c95b
 
 
 
bb5ad16
 
 
800c95b
 
 
 
 
 
 
 
 
1
2
3
4
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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