Шейп-файл содержит такие элементы:
edgeUID
osmId
геометрия
325297217
305009381
LINESTRING (-46.80244 -23.91205, -46.80294 -23...
< /table>
В CSV-файле
edge_uid
тип_активности
час
total_trip_count
325624632
Бег, прогулка, поход
2019-01-31T07
5
< /tr>
Я хотел построить карту листа, где я бы прочитал все строки из шейп-файла и покажите их цветом, меняющимся от самого слабого к самому сильному в зависимости от количества поездок, образуя таким образом своего рода тепловую карту.
Мне удалось построить ее, но линии выглядят неправильно:
Код: Выделить всё
import folium
import pandas as pd
import geopandas as gpd
from folium.plugins import HeatMap
from shapely.geometry import LineString
import branca.colormap as cm
# Function to read and plot shapefile and CSV data
def plot_map(shapefile_path, csv_path, output_html='map.html'):
# Load shapefile data
gdf = gpd.read_file(shapefile_path)
# Load CSV data
df = pd.read_csv(csv_path)
# Determine the center of the map
center = [gdf.geometry.centroid.y.mean(), gdf.geometry.centroid.x.mean()]
# Create the map
m = folium.Map(location=center, zoom_start=12)
# Add shapefile layer to the map using polylines with a color gradient
colormap = cm.LinearColormap(colors=['blue', 'red'], vmin=0, vmax=gdf.shape[0], caption='Number of trips')
for i, geom in enumerate(gdf.geometry):
if isinstance(geom, LineString):
folium.PolyLine(
locations=[(coord[1], coord[0]) for coord in geom.coords],
color=colormap(i),
weight=3,
opacity=0.7
).add_to(m)
colormap.add_to(m)
# Add points from the CSV as a heatmap layer
if {'latitude', 'longitude'}.issubset(df.columns):
heat_data = df[['latitude', 'longitude']].dropna().values.tolist()
HeatMap(heat_data).add_to(m)
else:
print("The CSV file must contain 'latitude' and 'longitude' columns.")
# Save the map as an HTML file
m.save(output_html)
print(f"Map saved as {output_html}")
# File paths
shapefile_path = '804387637e9638887b3bb625354623c5c83526532fac778ce885584328e169e4-1719798137525.shp'
csv_path = '804387637e9638887b3bb625354623c5c83526532fac778ce885584328e169e4-1719798137525.csv'
# Call the function to plot the map
plot_map(shapefile_path, csv_path)
Проблема в том, что кажется, что линии неправильно рисуются.
Объяснение. лучше было бы, чтобы линии казались слишком большими, как бы поверх друг друга, и цвет казался неправильным. Есть часть прямой улицы с красным цветом, который затем меняется на синий. А еще код очень медленный.
Подробнее здесь: https://stackoverflow.com/questions/790 ... linestring