赞
踩
由于内网环境,缺少地图~所以就考虑了使用folium+geojson来做简单的可视化.
import json import folium with open('天津市.json', 'r', encoding='utf-8') as f: china_geojson = json.load(f) # 创建一个没有背景瓦片的地图对象 m = folium.Map(location=[35, 105], zoom_start=4, tiles=None) # 添加geojson数据到地图中 folium.GeoJson( china_geojson, style_function=lambda feature: { 'fillColor': '#ffff00', 'color': 'black', 'weight': 2, 'dashArray': '5, 5' } ).add_to(m) # 在notebook中展示地图 m
使用pandas读取位置数据,然后可视化.
def visualize_trajectory(data): # 创建一个Folium地图对象 with open('天津市.json', 'r', encoding='utf-8') as f: china_geojson = json.load(f) # 创建一个没有背景瓦片的地图对象 m = folium.Map(location=[35, 105], zoom_start=4, tiles=None) # 添加geojson数据到地图中 folium.GeoJson( china_geojson, style_function=lambda feature: { 'fillColor': '#ffff00', 'color': 'black', 'weight': 2, 'dashArray': '5, 5' } ).add_to(m) # 对于每个轨迹点,添加标记 for i in range(len(data)): folium.Marker(location=[data['Latitude'].iloc[i], data['Longitude'].iloc[i]], popup=data['User_ID'].iloc[i]).add_to(m) # 使用轨迹点创建一条线,表示移动路径 for i in range(len(data) - 1): folium.PolyLine([[data['Latitude'].iloc[i], data['Longitude'].iloc[i]], [data['Latitude'].iloc[i + 1], data['Longitude'].iloc[i + 1]]], color='blue', weight=2).add_to(m) # 返回可视化的地图 return m
模拟:
visualized_map = visualize_trajectory(example_df)
visualized_map
模拟数据生成脚本:
import pandas as pd import random import time def generate_user_ids(num_users): """生成指定数量的用户ID""" return [f"User_{i}" for i in range(num_users)] def generate_timestamps(start_time, num_points): """生成一系列连续的时间戳""" return [start_time + i for i in range(num_points)] def generate_random_point(center_lat, center_lon, max_distance): """生成在给定经纬度为中心,指定距离范围内的随机经纬度点""" # 将距离转换为角度 max_distance_angle = max_distance / 6371.0 # 生成随机角度 dlat = random.uniform(-max_distance_angle, max_distance_angle) dlon = random.uniform(-max_distance_angle, max_distance_angle) # 计算新的经纬度 new_lat = center_lat + dlat new_lon = center_lon + dlon return (new_lat, new_lon) def simulate_trajectory(num_users, start_time, num_points, center_lat, center_lon, max_distance): """生成轨迹数据""" user_ids = generate_user_ids(num_users) all_data = [] for user_id in user_ids: timestamps = generate_timestamps(start_time, num_points) current_point = (center_lat, center_lon) for timestamp in timestamps: current_point = generate_random_point(*current_point, max_distance) all_data.append([user_id, timestamp] + list(current_point)) # 创建DataFrame columns = ['User_ID', 'Timestamp', 'Latitude', 'Longitude'] df = pd.DataFrame(all_data, columns=columns) return df # ############# #example_df = simulate_trajectory(122, int(time.time()), 10, 39.129484,117.220074, 1000)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。