rafa.pereira
rafa.pereira

Reputation: 13817

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

I'm trying to write a Python script to build an Origin Destination matrix using Open Trip Planner (OTP) but I'm very new to Python and OTP.

I am trying to use OTP scripting in Jython / Python to build an origin-destination matrix with travel times between pairs of locations. In short, the idea is to launch a Jython jar file to call the test.py python script but I'm struggling to get the the python script to do what I want.

A light and simple reproducible example is provided here And here is the python Script I've tried.

Python Script

#!/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')

The output should look something like this:

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. I've tried to create a new tag opentripplanner in this question but I don't have enough reputation for doing that.

Upvotes: 2

Views: 2147

Answers (1)

rafa.pereira
rafa.pereira

Reputation: 13817

Laurent Grégoire has kindly answered the quesiton on Github, so I reproduce here his solution.

This code works but still it would take a long time to compute large OD matrices (say more than 1 million pairs). Hence, any alternative answers that improve the speed/efficiency of the code would be welcomed!

#!/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))

Upvotes: 2

Related Questions