Extract GTFS colors for routes.txt

In a previous entry, I have written about the open data  GTFS files provided by the  RATP. The timetable information is very accurate. However, when drawing the bus lines on a map, the routes.txt GTFS file provides the same colors over and over:  black text over white background.  These are default values that must be adjusted for every bus line. The pair of colors  (FFFFFF/000000) from the open data files would give the following look and feel:

bus837

While there is no bus line in service that uses this color scheme, this gave me the idea of how to generate the valid (real) colors for all the bus lines. Later on, I will provide a tool that can update the colors in the GTFS files (routes.txt) The official open data page contains several data sets, among which a ZIP file with the colors of the bus lines (the “Indices des lignes de bus du réseau RATP” data set). There are several hundred image files (.png). They can be grouped in several categories:

  • regular bus lines (day+evening)
  • night bus lines
  • special bus lines.

bus838Day time buses

The regular buses use a solid color for background and another solid color for the foreground (text), or so it seems. Unfortunately, the .png files contain several dozen colors, some very close to each other. So, just picking up the top left pixel might not be such a good idea. And manually checking the color over the official palette (the PDF file) might take some time. And if the official colors change or new bus lines are added, the manual operation must be performed again. There must be another way. In fact, there is one I can think of.

bus838bThe image file here is a good example. The line 838 is not an official bus line. It just serves as an example. I have even exaggerated the colors. From a good hundred yards, the background color appears to be solid yellow, while in reality, it is far from solid.  What can we do? There is hope and the hope is called image processing. While there are many colors in such an image file, there are two who occur very often: the apparent background color and the apparent text color. This allows us to apply some filtering to  the real image file below. The closest friend in image processing is the histogram and as simple as it is, it suffices to  rank colors by their frequency. Not unsurprisingly, the #1 color is the background and the #2 color is the foreground.  Problem solved. A quick check  in the official palette has confirmed that the filtered colors. are the right ones.

nbus18The night bus lines

Here the plot thickens. The color of the night bus lines is not the text foreground, but that of the thin horizontal line. By the way, the bus line N18 used here is not an official line. As we already have a solution for the daylight buses, lets extend it for night lines.  The blue background is the top rank color, by frequency. The white text is the second in place. The  horizontal red line is #3 color (by frequency of use) and it is the right choice. Sometimes, the horizontal line is atop the blue rectangle. The histogram doesn’t care about the position and this is good news.

otherbusThe special bus lines

For a small minority of bus lines, the rectangle feature graphic elements. There might be  a good dozen colors with high frequencies. The background will be the #1 color by its frequency. In some rare cases, the #2 color is close to #1 color.   The histogram is still of great use.

What next

The procedure described here has been checked against several bus lines. Just to be sure, I will make a full check for all the bus lines. Also, once we have the real colors, they can be added to the GTFS files (routes.txt).

Advertisements

One thought on “Extract GTFS colors for routes.txt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s