聚合国内IT技术精华文章,分享IT技术精华,帮助IT从业人士成长

Uber H3实战:英国交通事故地点聚类

2020-04-18 09:29 浏览: 592 次 我要评论(0 条) 字号:

主要流程:

  • 将交通事故的经纬度信息转化为Uber H3
  • 方案一:对交通事故所在的经纬度进行聚类,获取非-1类别的数据,统计在聚集点的每个Uber H3的交通数据数量,并在地图上呈现
  • 方案二:对交通事故所在的H3网格进行汇总统计,针对网格所在区域按照阈值筛选后在地图上进行呈现

具体代码:

import numpy as np
import pandas as pd
import folium
from h3 import h3
from sklearn.cluster import DBSCAN

def create_map(clusters):
    # Create the map object
    map = folium.Map(zoom_start=12)

    # Convert the clusters dictionary items to polygons and add them to the map
    for cluster in clusters.values():
        points = cluster['geom']
        # points = [p[::-1] for p in points]
        tooltip = "{0} accidents".format(cluster['count'])
        polygon = folium.vector_layers.Polygon(locations=points, tooltip=tooltip,
                                               fill=True, 
                                               color='#ff0000', 
                                               fill_color='#ff0000', 
                                               fill_opacity=0.4, weight=3, opacity=0.4)
        polygon.add_to(map)

    # Determine the map bounding box
    max_lat = df.Latitude.max()
    min_lat = df.Latitude.min()
    max_lon = df.Longitude.max()
    min_lon = df.Longitude.min()
    
    # Fit the map to the bounds
    map.fit_bounds([[min_lat, min_lon], [max_lat, max_lon]])
    
    return map

column_types = {'Accident_Index': np.string_, 'LSOA_of_Accident_Location': np.string_}
uk_acc = pd.read_csv("UK_Accidents_2015_2016.csv", dtype=column_types)

#将经纬度转化成H3
h3_level = 11
def lat_lng_to_h3(row):
    return h3.geo_to_h3(row['Latitude'], row['Longitude'], h3_level)
uk_acc['h3'] = uk_acc.apply(lat_lng_to_h3, axis=1)

#使用DBSCAN进行聚类
uk_acc['rad_lng'] = np.radians(uk_acc['Longitude'].to_numpy())
uk_acc['rad_lat'] = np.radians(uk_acc['Latitude'].to_numpy())

eps_in_meters = 50.0
num_samples = 10
EARTH_R = 6370996.8 # 地球半径
uk_acc['cluster'] = DBSCAN(eps=eps_in_meters/EARTH_R, min_samples=num_samples, metric='haversine').fit_predict(uk_acc[['rad_lat', 'rad_lng']])

df = uk_acc[uk_acc.cluster != -1].copy()
clusters = dict()

for index, row in df.iterrows():
    key = row['h3']
    if key in clusters:
        clusters[key]['count'] += 1
    else:
        clusters[key] = {"count": 1,"geom": h3.h3_to_geo_boundary(h3_address=key)}

create_map(clusters)    

# 直接H3代码进行分组
h3_clusters = dict()

for index, row in uk_acc.iterrows():
    key = row['h3']
    if key in h3_clusters:
        h3_clusters[key]["count"] += 1
    else:
        h3_clusters[key] = {"count": 1, "geom": h3.h3_to_geo_boundary(h3_address=key)}

relevant_clusters = { key:value for (key,value) in h3_clusters.items() if value['count'] >= 10}
create_map(relevant_clusters)

参考链接:



网友评论已有0条评论, 我也要评论

发表评论

*

* (保密)

Ctrl+Enter 快捷回复