Skip to content
abyrd edited this page Oct 18, 2012 · 6 revisions

This page provides some scripts and commands that serve as starting points for making animated visualizations of OTP Analyst travel time maps.

Generating images (shell scripts)

First we want to generate a series of images covering the same geographic area, modifying one search parameter by a small amount from one image to the next. We can accomplish this by writing a shell script that uses the wget tool in a loop. If we request geotiffs from OTP, we will get 8-bit images with pixel values are in minutes. Specifying the color30 style will include a color map with 30-minute bands. The (nonstandard) 'timestamp' WMS query parameter will overlay the time, date, latitude, and longitude of the origin point on the image, as well as a legend for the chosen color map.

Varying time:

#!/bin/bash
DATE="2012-11-10"

# Brooklyn
#BBOX="-74.02587890625,40.650038657421916,-73.751220703125,40.769491796481404"
# Regional
BBOX="-74.26277,40.47254,-73.16413,40.97471"

# OpenPlans
ORIGIN="40.719518,-73.9997"

i=0
for h in {0..12}; do
for m in {0..59}; do
for s in {0..59..20}; do
    TIME=`printf "%02d:%02d:%02d" ${h} ${m} ${s}`
    URL="http://localhost:8080/opentripplanner-api-webapp/ws/wms?layers=traveltime&styles=color30&batch=true&mode=TRANSIT%2CWALK&maxWalkDistance=4000&date=${DATE}&time=${TIME}&fromPlace=${ORIGIN}&toPlace=${ORIGIN}&format=image/geotiff&srs=EPSG:3857&width=1280&height=720&bbox=${BBOX}&timestamp=true"
    echo $URL
    wget $URL -O "$i.tif"
    i=$((i+1))
done; done; done

Varying position (floating point math is not straightforward in shell scripts, but you can use the 'bc' calculator language):

#!/bin/bash
LAT0="40.685"
LON0="-73.95"
STEP="-0.0001"
TIME="08:00:00"
DATE="2012-11-10"

WIDTH=1280
HEIGHT=720

# regional
BBOX="-74.26277160644531,40.47254674190491,-73.16413879394531,40.974714106324654"

# alternate loop option:
# for i in $(seq 1 0.05 3); do

LON=$LON0
for c in {0..1000}; do
    LAT=`bc <<< $LAT0+$STEP*$c`
    URL="http://localhost:8080/opentripplanner-api-webapp/ws/wms?layers=traveltime&styles=color30&batch=true&mode=TRANSIT%2CWALK&maxWalkDistance=10000&date=${DATE}&time=${TIME}&fromPlace=${LAT},${LON}&toPlace=${LAT},${LON}&format=image/geotiff&srs=EPSG:3857&width=${WIDTH}&height=${HEIGHT}&bbox=${BBOX}&timestamp=true&maxTransfers=3"
    echo $URL
    wget $URL -O "$c.tif"
done

When varying position instead of time you may get very jerky animation because the departure point snaps to a point on the nearest edge. The off-street distance is not taken into account so you only get smooth variations in travel time when motion is parallel to the nearest street.

One way to get a rough bounding box to work with is using the image download tool in the analyst client. In the webapp's index.html, uncomment the section called Download Image Tool (it's commented out by default because it can place a heavy load on the server). Frame up your area of interest and use the URL for the downloaded image as a model for your shell script.

Encoding the images into video

Now we want to take the sequence of TIFF images and combine them into an animation.

Some popular image hosting sites suggest that you upload videos in h.264 format. This standard does provide very good quality in smaller files than the previous generation of codecs, and using it may minimize the loss of quality due to transcoding on some video hosting sites. The package x264, an open source implementation of h.264, has command line utilities that will handle encoding a sequence of images. However, while the source code of x264 is released under the GPL license, use of the code is governed by the MPEG-LA patent portfolio. Since h264 is encumbered with patents, you may want to consider other options such as Ogg Theora (distributed freely following an open standards philosophy) or Google's V8 (hopefully free of patent issues).

Here is an example x264 command line:

x264 "./%d.tif" --crf 25 --fps 25 --vf crop:129,100,120,103 -o output.mp4

The --crf parameter sets the video quality. Higher numbers give lower quality but smaller file sizes; 0 is lossless. The --fps parameter sets the frame rate. 25 and 30 are standard rates. --vf specifies video filters, the most common of which are crop and resize. Besides fitting somewhat odd-sized images into standard resolutions, there is a technical reason for resizing with h.264: the input to h.264 encoding is always in YUV format. Instead of red, green, and blue values it has one brightness channel and two color channels. This more closely matches human visual perception and contains less redundant information. We are specifically working with the i420 format, where the color channels are sampled at half the frequency of the luminance channel. The input images' width and height must be multiples of 2, and x264 will not automatically crop or resize on principle. x264 will automatically convert our indexed color input images into the YUV color representation as long as there are an even number of rows and columns.

The crop filter parameters are left,top,right,bottom and the values in the command above were simply chosen to get a 1280x720 (720p) video.

If you prefer to use Theora, the ffmpeg2theora package will do the job with a command like:

ffmpeg2theora %d.tif -o theora.ogv

The documentation on this wiki is outdated and should not be used

unless you are intentionally working with legacy versions of OpenTripPlanner. Please consult the current documentation at readthedocs

Clone this wiki locally