The most recent version of the code used in this page (which might be a little different from the one here) can be found here.

The city of Winnipeg has some interesting data available online as part of its open data initiative. The main entry point into that data is this page.

One city service that contributes to this system is Winnipeg Transit. They have several types of data. In a later entry, I will discuss the use of online queries; here, I use some static data that is available here. A description of the different files in the archive can be found here.

As a regular bus user, public transit afficionado and someone interested in population movement, I was curious to use this data to study some ideas about population movement. The first step was to do a simple, somewhat fun representation of the information.

We will use three libraries: lubridate, which helps with date manipulation, OpenStreetMap for getting maps and converting coordinates and animation, as a way to create a movie. So make sure these are installed. You will also need the program convert, part of the ImageMagick suite. Installing the latter under Linux is easy; for Windows users, the situation is a bit more tricky, although you may want to consider the Linux subsystem. More on that later.

To keep things easy, let us change the directory to the one where the code lies and below which both the data and the figures directories will be located. I am assuming that you downloaded the file from the link above and unzipped it in a subdirectory called staticSchedule. If generating a gif image, you will also need to create a subdirectory called tmpFig.

We set the day of the query (updated recently). Because we want one “day” worth of transport, from start of movement around 5:00 to end of movement around 2:00 next day, it will be useful to have the next day as well.

We now load and process calendar.txt, one of the components of the static schedule. This is a short file, giving the code of the type of operation (weekday, Saturday, Sunday), which is then used in the main stop schedule to single out the stop schedule for that type of operation. Note that when determining day of the week, lubridate starts with 1 on Sundays.

At this point, calendar should be reduced to a single line. We now load the remaining files that are needed for the plot. (Note that during Fall and Winter terms for universities, not all weekdays are the same, so the tests above should actually differentiate between Monday, Wednesday, Friday on the one hand and Tuesday, Thursday on the other. This was written when school was not in session..)

The files are loaded, we use merge (i.e., JOIN in the SQL world) to make a data frame containing all the required information. Note that this step is not necessary, it just makes plotting much easier.

Now we have a data frame with many columns. We select the rows (over 300,000 of them) corresponding to the chosen type of service selected in calendar. We then order the entries and make explicit the hour and minutes.

Finally, we add latitude and longitude in Mercator format, which is used for plotting.

Note that since the list is sorted by time, this allows for much faster processing during plotting. Now prepare the plots: download the map from OpenStreetMap.

First animation method - using convert

Finally, the plot itself. We plot minute by minute, generating one image for each.

Last piece: make an external call to convert (from ImageMagick) to create a gif file with all the individual, minute by minute plots.

It is not unlikely that you will get an error when executing this last command. This is due to the default policy of ImageMagick in terms of memory allocation. In this case, under Linux, you need to edit /etc/ImageMagick-6/policy.xml. Here is what the relevant lines read on my machine:

The result is not the movement of buses themselves, but the activity of bus stops along the route.

The problem here is file size (so much so that I could not post it on github). Also, the conversion itself can be quite time consuming. The next method addresses some of these issues.

Second animation method - using animation

The animation library uses ffmpeg, so this should be installed on your machine. (As often, this is a trivial task under Linux, it might be a bit more of a headache under Windows.)