简单的经纬度点聚类
import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s:%(message)s', datefmt='%Y-%m-%d %H:%M:%S') from geopy.distance import geodesic def stop2label(pnts, near=300): # 基于300m的半径对位置进行合并 # pnts [(lng,lat), (), *] res = [] pfun = lambda p1, p2: round(geodesic(p1[::-1], p2[::-1]).m, 1) count = 1 while pnts.__len__(): ipnt = pnts[0] flags = [pfun(ipnt, _) < near for _ in pnts] for idx, flag in enumerate(flags): if flag: res.append((pnts[idx], count)) logging.info((flags, len(pnts))) pnts = [pnts[_] for _, flag in enumerate(flags) if not flag] count += 1 return res