From c258938cb2fa7c0bfab51cf87f95ec7ebe04ace3 Mon Sep 17 00:00:00 2001 From: ren Date: Tue, 19 Nov 2024 15:03:03 +0200 Subject: [PATCH] Still needs some work, functional. --- .gitignore | 2 ++ visualizer.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 visualizer.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2552b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.html +gps/*.txt \ No newline at end of file diff --git a/visualizer.py b/visualizer.py new file mode 100644 index 0000000..094e0c8 --- /dev/null +++ b/visualizer.py @@ -0,0 +1,80 @@ +import pandas as pd +import glob +import folium +from folium.plugins import HeatMap +import matplotlib.pyplot as plt +import base64 +from io import BytesIO + +file_location = "gps/*.txt" + +files = glob.glob(file_location) # Combine all files +data_list = [] + +for file in files: + df = pd.read_csv( + file, + header=None, + names=["type", "line", "long", "lat", "ignore1", "ignore2", "tak", "ignore3", "ignore4", "destination"], + ) + data_list.append(df) + +data = pd.concat(data_list) + +data['type'] = data['type'].astype(int) +data['long'] = data['long'] / 1e6 # Convert to decimal degrees +data['lat'] = data['lat'] / 1e6 + +data = data.dropna(subset=['long', 'lat']) # Drop rows with invalid coordinates + +vehicle_types = {1: "Trolleybus", 2: "Bus", 3: "Tram", 7: "Nightbus"} # Vehicle types + +# Create heatmaps for each vehicle type +for v_type, v_name in vehicle_types.items(): + # Filter data for the current vehicle type + v_data = data[data['type'] == v_type] + + # Skip if no data for the vehicle type + if v_data.empty: + print(f"No data found for {v_name}. Skipping...") + continue + + # Create the heatmap + print(f"Creating heatmap for {v_name}...") + map_center = [v_data['lat'].mean(), v_data['long'].mean()] + heatmap_map = folium.Map(location=map_center, zoom_start=12) + + # Don't change these values. + HeatMap( + data=v_data[['lat', 'long']].values, + radius=10, + blur=15, + min_opacity=1, + max_zoom=15, + gradient={0.2: 'blue', 0.4: 'green', 0.6: 'yellow', 0.8: 'orange', 1.0: 'red'}, # Gradient colors + ).add_to(heatmap_map) + + # Generate destination graph + dest_counts = v_data['destination'].value_counts() + plt.figure(figsize=(6, 4)) + dest_counts.plot(kind='bar', color='red') + plt.title(f"Destinations for {v_name}") + plt.xlabel("Destination") + plt.ylabel("Count") + plt.tight_layout() + + # Save graph to a base64-encoded image + img = BytesIO() + plt.savefig(img, format='png', bbox_inches='tight') + img.seek(0) + encoded_img = base64.b64encode(img.getvalue()).decode('utf-8') + plt.close() + + # Add the image to the map + html = f'' + popup = folium.Popup(html, max_width=500) + folium.Marker(location=map_center, popup=popup).add_to(heatmap_map) + + # Save the heatmap + heatmap_map.save(f"{v_name}_heatmap.html") + print(f"{v_name} heatmap saved as {v_name}_heatmap.html.")