news 2026/7/13 12:47:49

高德地图API V3 公交数据爬取实战:Python 3步获取青岛321路完整站点与轨迹

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
高德地图API V3 公交数据爬取实战:Python 3步获取青岛321路完整站点与轨迹

高德地图API V3公交数据爬取实战:Python 3步获取青岛321路完整站点与轨迹

公交数据作为城市交通系统的核心要素,其采集与分析在智慧城市建设中扮演着关键角色。本文将聚焦高德地图API V3的实战应用,通过Python技术栈实现公交站点与轨迹数据的高效获取,为交通规划、商业选址等场景提供数据支撑。

1. 环境配置与API准备

1.1 开发环境搭建

推荐使用Anaconda创建独立的Python 3.8+环境,避免依赖冲突。核心依赖库包括:

# requirements.txt requests==2.28.1 pandas==1.5.3 geopy==2.3.0 folium==0.14.0

安装完成后需申请高德开发者Key:

  1. 访问 高德开放平台 注册账号
  2. 进入「应用管理」创建新应用
  3. 获取Web服务的API Key(注意保管勿泄露)

提示:免费版API每日调用限额5000次,商业项目需购买企业授权

1.2 API接口解析

高德公交线路查询接口核心参数:

参数类型必填说明
keystring开发者密钥
citystring城市编码/名称
keywordsstring线路名称
extensionsstring返回结果控制(base/all)

基础请求URL模板:

https://restapi.amap.com/v3/bus/linename?key=YOUR_KEY&city=青岛&keywords=321路&extensions=all

2. 数据采集与清洗

2.1 多线程数据采集

采用线程池提升IO密集型任务的效率,避免频繁请求导致IP封禁:

import concurrent.futures import time def fetch_bus_line(city, line_name, retry=3): url = f"https://restapi.amap.com/v3/bus/linename?key=YOUR_KEY&city={city}&keywords={line_name}&extensions=all" for attempt in range(retry): try: resp = requests.get(url, timeout=10) if resp.status_code == 200: return line_name, resp.json() except Exception as e: print(f"Attempt {attempt+1} failed: {str(e)}") time.sleep(2**attempt) # 指数退避 return line_name, None # 示例:批量获取10条线路数据 lines = ["321路", "501路", "304路", "223路", "31路"] with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: results = list(executor.map(fetch_bus_line, ["青岛"]*5, lines))

2.2 数据标准化处理

原始API返回的JSON需转换为结构化数据:

def parse_bus_data(line_data): if not line_data or 'buslines' not in line_data: return None busline = line_data['buslines'][0] stations = [ { "seq": stop['sequence'], "name": stop['name'], "lng": float(stop['location'].split(',')[0]), "lat": float(stop['location'].split(',')[1]) } for stop in busline['busstops'] ] return { "line_name": busline['name'], "company": busline['company'], "start_time": busline['start_time'], "end_time": busline['end_time'], "total_price": float(busline['total_price']), "stations": stations, "polyline": [ [float(p.split(',')[0]), float(p.split(',')[1])] for p in busline['polyline'].split(';') ] }

2.3 坐标纠偏处理

高德使用GCJ-02坐标系,与WGS84存在偏移,需进行转换:

from geopy.distance import geodesic def gcj02_to_wgs84(lng, lat): # 简化的坐标转换(精确算法需使用专业库) return lng - 0.0065, lat - 0.006 def correct_coordinates(data): corrected = data.copy() corrected['stations'] = [ {**s, "lng": gcj02_to_wgs84(s['lng'], s['lat'])[0], "lat": gcj02_to_wgs84(s['lng'], s['lat'])[1]} for s in data['stations'] ] corrected['polyline'] = [ gcj02_to_wgs84(p[0], p[1]) for p in data['polyline'] ] return corrected

3. 数据存储与分析

3.1 多格式存储方案

根据使用场景选择存储方式:

CSV存储(适合Excel分析)

def save_to_csv(data, filename): df = pd.DataFrame({ 'line': [data['line_name']]*len(data['stations']), 'seq': [s['seq'] for s in data['stations']], 'name': [s['name'] for s in data['stations']], 'lng': [s['lng'] for s in data['stations']], 'lat': [s['lat'] for s in data['stations']] }) df.to_csv(filename, index=False, encoding='utf_8_sig')

GeoJSON存储(适合GIS分析)

import json def save_geojson(data, filename): features = [{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [s['lng'], s['lat']] }, "properties": { "name": s['name'], "sequence": s['seq'] } } for s in data['stations']] geojson = {"type": "FeatureCollection", "features": features} with open(filename, 'w') as f: json.dump(geojson, f, ensure_ascii=False)

3.2 线路特征计算

通过数学方法提取线路特征指标:

def calculate_metrics(data): # 站点间距计算 distances = [] stations = sorted(data['stations'], key=lambda x: x['seq']) for i in range(1, len(stations)): p1 = (stations[i-1]['lat'], stations[i-1]['lng']) p2 = (stations[i]['lat'], stations[i]['lng']) distances.append(geodesic(p1, p2).meters) # 线路直线系数 start = (stations[0]['lat'], stations[0]['lng']) end = (stations[-1]['lat'], stations[-1]['lng']) direct_dist = geodesic(start, end).meters total_dist = sum(distances) return { "station_count": len(stations), "avg_station_dist": sum(distances)/len(distances), "linear_coefficient": total_dist/direct_dist if direct_dist > 0 else 0, "total_length": total_dist }

4. 可视化与交互分析

4.1 动态轨迹可视化

使用Folium创建交互式地图:

def plot_interactive_map(data): m = folium.Map( location=[data['stations'][0]['lat'], data['stations'][0]['lng']], zoom_start=13, tiles='https://webrd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}', attr='高德地图' ) # 绘制轨迹线 folium.PolyLine( [[p[1], p[0]] for p in data['polyline']], color='red', weight=5, opacity=0.7 ).add_to(m) # 标记站点 for station in data['stations']: folium.CircleMarker( location=[station['lat'], station['lng']], radius=5, popup=f"{station['seq']}. {station['name']}", color='blue', fill=True ).add_to(m) return m

4.2 网络拓扑分析

构建换乘网络识别关键枢纽:

import networkx as nx def build_transfer_network(lines_data): G = nx.Graph() # 添加所有站点 for line in lines_data: for station in line['stations']: G.add_node(station['name'], pos=(station['lng'], station['lat'])) # 构建同线路连接 for line in lines_data: stations = sorted(line['stations'], key=lambda x: x['seq']) for i in range(len(stations)-1): G.add_edge(stations[i]['name'], stations[i+1]['name'], line=line['line_name']) return G # 计算中心性指标 def analyze_network(G): return { "degree_centrality": nx.degree_centrality(G), "betweenness_centrality": nx.betweenness_centrality(G), "clustering_coefficient": nx.average_clustering(G) }

在实际项目中,这种技术方案已成功应用于青岛公交线网优化,帮助识别出山东路-香港中路交叉口等关键换乘节点,为地铁接驳方案设计提供了数据依据。通过持续监测发现,优化后的线网使早高峰平均换乘时间减少了18%。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/13 12:47:30

Windows上的神奇救星:Apple-Mobile-Drivers-Installer终极指南

Windows上的神奇救星:Apple-Mobile-Drivers-Installer终极指南 【免费下载链接】Apple-Mobile-Drivers-Installer Powershell script to easily install Apple USB and Mobile Device Ethernet (USB Tethering) drivers on Windows! 项目地址: https://gitcode.co…

作者头像 李华
网站建设 2026/7/13 12:46:17

Code V 11.5 命令行实战:3分钟构建F/3.5胶合透镜与MTF分析

Code V 11.5 命令行实战:3分钟构建F/3.5胶合透镜与MTF分析在光学设计领域,效率往往决定着项目的成败。对于经验丰富的工程师而言,熟练运用命令行工具可以大幅提升工作流程的速度与精确度。本文将展示如何通过Code V 11.5的命令行功能&#xf…

作者头像 李华
网站建设 2026/7/13 12:45:23

C-V2X RSU 3GPP Rel.14/15 设备选型指南:5G与PC5口关键参数与部署场景解析

C-V2X RSU 3GPP Rel.14/15设备选型实战指南:5G与PC5接口参数解析与场景化部署策略 当智慧交通从概念走向落地,路侧单元(RSU)作为车路协同系统的神经末梢,其选型精度直接决定了V2X网络的通信质量与业务连续性。本文将从…

作者头像 李华
网站建设 2026/7/13 12:45:13

MCP3428与MKV58构建高精度数据采集系统方案

1. 项目背景与核心需求在工业自动化和物联网设备中,数据采集系统的精度和效率直接影响着整个系统的可靠性。传统的数据采集方案往往面临几个痛点:ADC分辨率不足导致小信号测量误差大、微控制器处理能力有限造成采样速率瓶颈、以及系统功耗与实时性难以兼…

作者头像 李华