如何通过对GTFS数据使用Cypher查询(neo4j)查找传输连接中的最短路径?

问题描述 投票:0回答:1

我是neo4j的新手,我根据GTFS的数据模型在此steps之后创建了一个图形。我想在图表中找到所有最短的间接路线(包含转移)。Data model of graph database包含4个实体:路线,跳闸,停止,停止时间。这是db.scheme()的屏幕截图。

基于写Bruggen的query,我对其进行了修改以供使用:

MATCH 
(from:Stop {code:'VBR'})--(st_from:Stoptime),
(to:Stop {code:'VIR'})--(st_to:Stoptime),
p1=((st_from)-[:PRECEDES*]->(st_midway_arr:Stoptime)),
(st_midway_arr)--(midway:Stop),
(midway)--(st_midway_dep:Stoptime),
p2=((st_midway_dep)-[:PRECEDES*]->(st_to))
WHERE
st_from.departure_time > '00:00'
AND st_from.departure_time < '23:00'
AND st_midway_arr.arrival_time > st_from.departure_time
AND st_midway_dep.departure_time > st_midway_arr.arrival_time
AND st_to.arrival_time > st_midway_dep.departure_time
RETURN
from,st_from,to,st_to,p1,p2,midway
order by (st_to.arrival_time_int-st_from.departure_time_int) ASC
limit 1;

此查询未使用最短路径,并且平均需要30秒才能找到路径,但查询的输出很好

因此,我尝试使用allshortestpaths方法编写另一个查询,它确实非常快(0,3s)。但它也使我跳闸,而跳闸的方向又不同(VIR-> VBR)……另一个问题是连接的时间安排。

[能帮我,当我使用allshortestpath方法时如何访问传输节点(Station)?我想编写一个计时和stop_sequence条件,以确保这是正确的方向。

match (from:Stop {code:'VBR'}),(to:Stop {code:'VIR'})
with from,to
match p = allshortestpaths((from)-[*]-(to))
where NONE (x in relationships(p) where type(x)="OPERATES")
return p
limit 10;
graph neo4j cypher shortest-path gtfs
1个回答
0
投票
match (from:Stop {code:'VBR'}),(to:Stop {code:'VIR'})
with from,to
match p = allshortestpaths((from)-[*]->(to)) // here you needed you give the direction to make sure paths are from 'VBR' to 'VIR'
where NONE (x in relationships(p) where type(x)="OPERATES")
return p
limit 10;

下一步,如果您想查看路径中的节点,则可以使用nodes(p)

match (from:Stop {code:'VBR'}),(to:Stop {code:'VIR'})
with from,to
match p = allshortestpaths((from)-[*]->(to)) 
where NONE (x in relationships(p) where type(x)="OPERATES")
WITH nodes(p) as nodes
return [n in nodes|n.anythingYouNeed] as requiredData
limit 10
© www.soinside.com 2019 - 2024. All rights reserved.