在 Jython 中使用 Open Trip Planner 脚本编写的起点和终点矩阵 / Python

Origin Destination matrix with Open Trip Planner scripting in Jython / Python

我正在尝试编写一个 Python 脚本来使用 Open Trip Planner (OTP) 构建一个 Origin-Destination 矩阵,但我对 Python 和 OTP 还很陌生。

我正在尝试使用 Jython 中的 OTP scripting / Python 来构建一个起点-终点矩阵,其中包含成对位置之间的行程时间。简而言之,我的想法是启动一个 Jython jar 文件来调用 test.py python 脚本,但我正在努力让 python 脚本执行我想要的操作。

轻巧简单 reproducible example is provided here 这是我试过的 python 脚本。

Python 脚本

#!/usr/bin/jython
from org.opentripplanner.scripting.api import *

# Instantiate an OtpsEntryPoint
otp = OtpsEntryPoint.fromArgs(['--graphs', 'C:/Users/rafa/Desktop/jython_portland',
                               '--router', 'portland'])

# Get the default router
router = otp.getRouter('portland')

# Create a default request for a given time
req = otp.createRequest()
req.setDateTime(2015, 9, 15, 10, 00, 00)
req.setMaxTimeSec(1800)

req.setModes('WALK,BUS,RAIL') 


# Read Points of Origin
points = otp.loadCSVPopulation('points.csv', 'Y', 'X')

# Read Points of Destination
dests = otp.loadCSVPopulation('points.csv', 'Y', 'X')


# Create a CSV output
matrixCsv = otp.createCSVOutput()
matrixCsv.setHeader([ 'Origin', 'Destination', 'min_time' ])

# Start Loop
for origin in points:
  print "Processing: ", origin
  req.setOrigin(origin)
  spt = router.plan(req)
  if spt is None:   continue

  # Evaluate the SPT for all points
  result = spt.eval(dests)

  # Find the time to other points
  if len(result) == 0:  minTime = -1
  else:         minTime = min([ r.getTime() for r in result ])

  # Add a new row of result in the CSV output
  matrixCsv.addRow([ origin.getStringData('GEOID'), r.getIndividual().getStringData('GEOID'), minTime ])

# Save the result
matrixCsv.save('traveltime_matrix.csv')

输出应如下所示:

GEOID    GEOID    travel_time
    1        1              0
    1        2              7
    1        3              6
    2        1             10
    2        2              0
    2        3             12
    3        1              5
    3        2             10
    3        3              0

ps。我试图在这个问题中创建一个新标签 opentripplanner,但我没有足够的声誉来这样做。

Laurent Grégoire 已经很好地回答了 Github 上的问题,所以我在这里复制他的解决方案。

此代码有效,但计算大型 OD 矩阵(比如超过 100 万对)仍然需要很长时间。因此,欢迎任何改进代码 speed/efficiency 的替代答案!

#!/usr/bin/jython
from org.opentripplanner.scripting.api import OtpsEntryPoint

# Instantiate an OtpsEntryPoint
otp = OtpsEntryPoint.fromArgs(['--graphs', '.',
                               '--router', 'portland'])

# Start timing the code
import time
start_time = time.time()

# Get the default router
# Could also be called: router = otp.getRouter('paris')
router = otp.getRouter('portland')

# Create a default request for a given time
req = otp.createRequest()
req.setDateTime(2015, 9, 15, 10, 00, 00)
req.setMaxTimeSec(7200)
req.setModes('WALK,BUS,RAIL') 

# The file points.csv contains the columns GEOID, X and Y.
points = otp.loadCSVPopulation('points.csv', 'Y', 'X')
dests = otp.loadCSVPopulation('points.csv', 'Y', 'X')

# Create a CSV output
matrixCsv = otp.createCSVOutput()
matrixCsv.setHeader([ 'Origin', 'Destination', 'Walk_distance', 'Travel_time' ])

# Start Loop
for origin in points:
  print "Processing origin: ", origin
  req.setOrigin(origin)
  spt = router.plan(req)
  if spt is None: continue

  # Evaluate the SPT for all points
  result = spt.eval(dests)

  # Add a new row of result in the CSV output
  for r in result:
    matrixCsv.addRow([ origin.getStringData('GEOID'), r.getIndividual().getStringData('GEOID'), r.getWalkDistance() , r.getTime()])

# Save the result
matrixCsv.save('traveltime_matrix.csv')

# Stop timing the code
print("Elapsed time was %g seconds" % (time.time() - start_time))