基于Dijkstra算法的郑州地铁路径规划
需要引入geopy库
pip install geopy 安装即可
import requests
from bs4 import BeautifulSoup
import pandas as pd
import json
import os
from tqdm import tqdm
from collections import defaultdict
import pickle
import itertools
from geopy.distance import geodesic
#爬取郑州地铁数据
def spyder():
#获得郑州的地铁信息
print('正在爬取郑州地铁信息...')
url='http://zz.bendibao.com/ditie/linemap.shtml'
user_agent='Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11'
headers = {'User-Agent': user_agent}
r = requests.get(url, headers=headers)
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text, 'lxml')
all_info = soup.find_all('div', class_='line-list')
df=pd.DataFrame(columns=['name','site'])
for info in tqdm(all_info):
title=info.find_all('div',class_='wrap')[0].get_text().split()[0].replace('线路图','')
station_all=info.find_all('a',class_='link')
for station in station_all:
station_name=station.get_text()
longitude,latitude=get_location(station_name,'郑州')
temp={'name':station_name,'site':title,'longitude':longitude,'latitude':latitude}
df =df.append(temp,ignore_index=True)
df.to_excel('./subway.xlsx',index=False)
def get_location(keyword,city):
#获得经纬度
user_agent='Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'
headers = {'User-Agent': user_agent}
url='http://restapi.amap.com/v3/place/text?key='+keynum+'&keywords='+keyword+'&types=&city='+city+'&children=1&offset=1&page=1&extensions=all'
data = requests.get(url, headers=headers)
data.encoding='utf-8'
data=json.loads(data.text)
result=data['pois'][0]['location'].split(',')
return result[0],result[1]
def compute_distance(longitude1,latitude1,longitude2,latitude2):
#计算2点之间的距离
user_agent='Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'
headers = {'User-Agent': user_agent}
url='http://restapi.amap.com/v3/distance?key='+keynum+'&origins='+str(longitude1)+','+str(latitude1)+'&destination='+str(longitude2)+','+str(latitude2)+'&type=1'
data=requests.get(url,headers=headers)
data.encoding='utf-8'
data=json.loads(data.text)
result=data['results'][0]['distance']
return result
def get_graph():
print('正在创建pickle文件...')
data=pd.read_excel('./subway.xlsx')
#创建点之间的距离
graph=defaultdict(dict)
for i in range(data.shape[0]):
site1=data.iloc[i]['site']
if i<data.shape[0]-1:
site2=data.iloc[i+1]['site']
#如果是共一条线
if site1==site2:
longitude1,latitude1=data.iloc[i]['longitude'],data.iloc[i]['latitude']
longitude2,latitude2=data.iloc[i+1]['longitude'],data.iloc[i+1]['latitude']
name1=data.iloc[i]['name']
name2=data.iloc[i+1]['name']
distance=compute_distance(longitude1,latitude1,longitude2,latitude2)
graph[name1][name2]=distance
graph[name2][name1]=distance
output=open('graph.pkl','wb')
pickle.dump(graph,output) #找到开销最小的节点
def find_lowest_cost_node(costs,processed):
#初始化数据
lowest_cost=float('inf') #初始化最小值为无穷大
lowest_cost_node=None
#遍历所有节点
for node in costs:
#如果该节点没有被处理
if not node in processed:
#如果当前的节点的开销比已经存在的开销小,那么久更新该节点为最小开销的节点
if costs[node]<lowest_cost:
lowest_cost=costs[node]
lowest_cost_node=node
return lowest_cost_node #找到最短路径
def find_shortest_path(start,end,parents):
# print(end)
node=end
# print(parents)
shortest_path=[end]
# print(parents[node])
#最终的根节点为start
while parents[node] !=start:
shortest_path.append(parents[node])
node=parents[node]
shortest_path.append(start)
return shortest_path
#计算图中从start到end的最短路径
def dijkstra(start,end,graph,costs,processed,parents):
#查询到目前开销最小的节点
node=find_lowest_cost_node(costs,processed)
#使用找到的开销最小节点,计算它的邻居是否可以通过它进行更新
#如果所有的节点都在processed里面 就结束
while node is not None:
#获取节点的cost
cost=costs[node] #cost 是从node 到start的距离
#获取节点的邻居
neighbors=graph[node]
#遍历所有的邻居,看是否可以通过它进行更新
for neighbor in neighbors.keys():
#计算邻居到当前节点+当前节点的开销
new_cost=cost+float(neighbors[neighbor])
if neighbor not in costs or new_cost<costs[neighbor]:
costs[neighbor]=new_cost
#经过node到邻居的节点,cost最少
parents[neighbor]=node
#将当前节点标记为已处理
processed.append(node)
#下一步继续找U中最短距离的节点 costs=U,processed=S
node=find_lowest_cost_node(costs,processed) #循环完成 说明所有节点已经处理完
shortest_path=find_shortest_path(start,end,parents)
shortest_path.reverse()
return shortest_path def subway_line(start,end):
file=open('graph.pkl','rb')
graph=pickle.load(file)
#创建点之间的距离
#现在我们有了各个地铁站之间的距离存储在graph
#创建节点的开销表,cost是指从start到该节点的距离
costs={}
parents={}
parents[end]=None
for node in graph[start].keys():
costs[node]=float(graph[start][node])
parents[node]=start
#终点到起始点距离为无穷大
costs[end]=float('inf')
#记录处理过的节点list
processed=[]
# print(parents)
shortest_path=dijkstra(start,end,graph,costs,processed,parents)
return shortest_path def get_nearest_subway(data,longitude1,latitude1):
#找最近的地铁站
longitude1=float(longitude1)
latitude1=float(latitude1)
distance=float('inf')
nearest_subway=None
for i in range(data.shape[0]):
site1=data.iloc[i]['name']
longitude=float(data.iloc[i]['longitude'])
latitude=float(data.iloc[i]['latitude'])
temp=geodesic((latitude1,longitude1), (latitude,longitude)).m
if temp<distance:
distance=temp
nearest_subway=site1
return nearest_subway def main(site1,site2):
if not os.path.exists('./subway.xlsx'):
spyder()
if not os.path.exists('./graph.pkl'):
get_graph()
longitude1,latitude1=get_location(site1,'郑州')
longitude2,latitude2=get_location(site2,'郑州')
data=pd.read_excel('./subway.xlsx')
#求最近的地铁站
start=get_nearest_subway(data,longitude1,latitude1)
end=get_nearest_subway(data,longitude2,latitude2)
shortest_path=subway_line(start,end)
if site1 !=start:
shortest_path.insert(0,site1)
if site2 !=end:
shortest_path.append(site2)
print('路线规划为:','-->'.join(shortest_path))
if __name__ == '__main__':
global keynum
keynum='706594012b3c9c7ce3e46ac2a04057e1' #key
main('郑州大学','中原福塔')
效果展示:

基于Dijkstra算法的郑州地铁路径规划的更多相关文章
- Dijkstra算法_北京地铁换乘_android实现-附带源码.apk
Dijkstra算法_北京地铁换乘_android实现 android 2.2+ 源码下载 apk下载 直接上图片 如下: Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计 ...
- 基于dijkstra算法求地铁站最短路径以及打印出所有的路径
拓展dijkstra算法,实现利用vector存储多条路径: #include <iostream> #include <vector> #include <stack& ...
- [最短路径问题]Dijkstra算法(含还原具体路径)
前言 在本篇文章中,我将介绍 Dijkstra 算法解决 单源最短路径问题 ,同时还包含了具体路径的还原.以下是我自己的全部学习过程与思考,参考书籍为 <数据结构>(C++语言版) 邓俊辉 ...
- AI贪吃蛇前瞻——基于Dijkstra算法的最短路径问题
在贪吃蛇流程结构优化之后,我又不满足于亲自操刀控制这条蠢蠢的蛇,干脆就让它升级成AI,我来看程序自己玩,哈哈. 一.Dijkstra算法原理 作为一种广为人知的单源最短路径算法,Dijkstra用于求 ...
- 山东省第七届ACM竞赛 C题 Proxy (Dijkstra算法,单源路径最短问题)
题意:给定0-n+1个点,和m条边,让你找到一条从0到n+1的最短路,输出与0相连的结点... 析:很明显么,是Dijkstra算法,不过特殊的是要输出与0相连的边,所以我们倒着搜,也是从n+1找到0 ...
- 全局路径规划算法Dijkstra(迪杰斯特拉算法)- matlab
参考博客链接:https://www.cnblogs.com/kex1n/p/4178782.html Dijkstra是常用的全局路径规划算法,其本质上是一个最短路径寻优算法.算法的详细介绍参考上述 ...
- DWA局部路径规划算法论文阅读:The Dynamic Window Approach to Collision Avoidance。
DWA(动态窗口)算法是用于局部路径规划的算法,已经在ROS中实现,在move_base堆栈中:http://wiki.ros.org/dwa_local_planner DWA算法第一次提出应该是1 ...
- move_base的全局路径规划代码研究
algorithmn parameter code 主要是以下三个函数 计算所有的可行点 怎么计算一个点的可行点 从可行点中计算路径path todo algorithmn 算法的解释 Dijkstr ...
- 『算法设计_伪代码』贪心算法_最短路径Dijkstra算法
Dijkstra算法实际上是一个贪婪算法(Greedy algorithm).因为该算法总是试图优先访问每一步循环中距离起始点最近的下一个结点.Dijkstra算法的过程如下图所示. 初始化 给定图中 ...
- 基于谷歌地图的Dijkstra算法水路路径规划
最终效果图如下: 还是图.邻接表,可以模拟出几个对象=>节点.边.路径.三个类分别如下: Node 节点: using System; using System.Collections.Gene ...
随机推荐
- Redis缓存中的数据和数据库不一致
首先关于两者数据的一致性包含有两种情况: (1)缓存中有数据时,那数据库中的数据要和缓存中的数据相同: (2)缓存中没有数据时,数据库中的数据必须是最新的. 如果不符合以上两种情况,就属于缓存和数据库 ...
- PyTables文件格式、PyTables 文件支持的数据类型
翻译自 https://www.pytables.org/usersguide/file_format.html,http://www.pytables.org/usersguide/datatype ...
- 使用python做基本的数据处理
使用python做基本的数据处理 1.常用的基本数据结构 元组.列表.字典.集合.常用的序列函数 1.1基本操作 1.1.1 元组:逗号分开的序列值. tup = tuple (4,5,6) tup ...
- 如何从相机拍照建立的Fileprovider中获取绝对路径
我们通过高版本获取的fileprovider,然后拍了个照片,如下 imageUri = FileProvider.getUriForFile 但是我们发现当我们 File file = new Fi ...
- vue-图书管理系统
/* 路由模块 */ const express = require('express'); const router = express.Router(); const service = requ ...
- 【STM32】SYSCLK配置|学习笔记
一.RCC原理 所有stm32的外设都是挂载在相应的时钟上的,如下 挂载在AHB上的外设 挂载在APB1上的 APB2上的 所以RCC很重要,特别像TIM这种对时钟特别敏感的外设就必须把SYSCLK确 ...
- 学术主页——朱青橙(Qingcheng Zhu)
个人简介 来自北京邮电大学 教育经历 毕业于苏州大学 奖励荣誉 学术论文 发明专利 国际标准 科研项目
- 查询openmp的版本
vim test.cpp #include <unordered_map>#include <cstdio>#include <omp.h> int main(in ...
- Twitter Bootstrap Grid System
Bootstrap是什么?Bootstrap是一个用来快速并且容易的开发web页面,强大的前端框架.它包含html和css基本设计模板,包含通用用户界面组件,如:排版,形式,按钮,表格,导航,下拉框, ...
- mysql实训
MYSQL You have an error in your SQL syntax; check the manual that corresponds to your MySQL server v ...