A Raster-Based Game of Life Using Python in QGIS

Blog post authored by Richard Wen and Claus Rinner

A great way to demonstrate the manipulation of geospatial raster data is Conway’s Game of Life [1]. The “game” starts with a grid (“board”) of binary cells, which represent either alive (populated) or dead (empty) states. Each cell interacts with its eight adjacent neighbours to determine its next state. At each iteration of the game clock, the following rules are applied [1]:

  • A live cell with less than two or more than three live neighbours dies (under-population, overcrowding).
  • A live cell with two or three live neighbours continues to live.
  • A dead cell with three live neighbours becomes alive (reproduction).

The free and open-source Geographic Information System (GIS) software package QGIS [2] offers support for scripting with the Python programming language (pyQGIS module), which enables the use of powerful libraries such as NumPy and GDAL for dealing with raster data. Numerical Python (NumPy) [3] is a package developed for Python that is geared towards scientific computation with support for multi-dimensional arrays and matrices. The Geospatial Data Abstraction Library (GDAL) [4] is a library for translating raster and vector geospatial data formats available as a binding for Python.

Using NumPy, GDAL, and pyQGIS, we implemented the Game of Life, where NumPy manipulates the arrays, GDAL handles reading and writing of the raster data, and pyQGIS visualizes the rasters and their relative changes. The source code was written by Master of Spatial Analysis student Richard Wen with input from Dr. Claus Rinner and is available at https://github.com/rwenite/QGIS_RasterArray. The project was inspired by Anita Graser’s visit to Ryerson’s Lab for Geocomputation in October 2014, during which Anita developed a vector-based version of the Game of Life in QGIS (see http://anitagraser.com/2014/11/16/more-experiments-with-game-of-life/).

Our implementation takes an object-oriented approach, in which an object of a Game of Life class is instantiated and the gaming board is updated with the cycle() method using the QGIS python console. The core function is the manipulation of individual raster cells based on a coded algorithm – in this case, the rules defined by the Game of Life.

Let’s start by initializing and cycling a gaming board using default parameters:

# Instantiate a starting board
x = GameofLife()

game-of-life_fig1a

# Cycle the board twice
x.cycle(2)

game-of-life_fig1

The gaming board may be initialized with a random raster, a filled raster, a custom raster, or from a pre-defined raster file:

# The default is a random raster, we can set the width and height as well
x = GameofLife(width=3,height=5)
# Cycle the board
x.cycle()

game-of-life_fig2

# Fill a cells object with 1s
y = Cells(inRaster=1)
# Create a raster with the filled cells object in the directory
y.toRaster("path\\to\\filledraster\\file.tif")
# Instantiate a starting board with the filled raster
x = GameofLife(raster="path\\to\\filledraster\\file.tif")
# Cycle the board
x.cycle()

game-of-life_fig3

# Generate a raster from a list of tuples
y=Cells(inRaster=[
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0),
(0,0,1,0,0,1,0,0),
(0,0,0,0,0,0,0,0),
(0,0,1,0,0,1,0,0),
(0,0,0,1,1,0,0,0),
(0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0)])
# Create a raster with the custom cells object in the directory
y.toRaster("path\\to\\customraster\\file.tif")
# Instantiate a starting board with the custom raster
x = GameofLife(raster="path\\to\\customraster\\file.tif")

game-of-life_fig3b

# Instantiate a starting board with a raster
x = GameofLife(raster="path\\to\\raster\\file.tif")

game-of-life_fig4a

Date source: City of Toronto Open Data [5]

Some other interesting features include changing animation speed, jumping cycles, and applying customized layer styles:

# Adjust delay to 3 seconds
x.speed=3
# Cycle 10 times normally
x.cycle(10)
# Cycle 5 times and display every 2nd cycle
x.cycle(5,2)
# Set the style to the defined qml file
x.style = “path\\to\\qml\\style\\file.qml”

This post focuses on the functionality of the program, while its inner workings can be grasped from comments in the Python source code posted at https://github.com/rwenite/QGIS_RasterArray. The code was written and tested for QGIS 2.6; feedback on any issues is most welcome. The use of a NumPy array to iterate through the grid cells was found in an answer by user “gene” on GIS StackExchange [6]. Reading and processing raster data does have its challenges. When dealing with large grids, reading raster data in blocks rather than as a whole is advisable, because there may not be enough RAM to store an entire file at once [7].

The aim of implementing the Game of Life with Python and QGIS is to demonstrate some fundamental concepts of raster data analysis and cellular automata modeling, both of which have important applications in Geography and GIS. Existing QGIS functionality and scripts for raster processing seem to focus more on low-level input/output operations than higher-level analysis functions. For example, we did not find advanced local and focal raster operations in QGIS’ raster calculator. Thus, we envision that the RasterArray code could serve as a basis for expanding raster analysis in QGIS. The code will also be used in a yet-to-be-written lab assignment in GEO641 “GIS and Decision Support” in Ryerson’s BA in Geographic Analysis program.

 

References:

[1] Wikipedia, Conway’s Game of Life
http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

[2] QGIS
http://www2.qgis.org/en/site/

[3] NumPy, Numerical Python
http://www.numpy.org/

[4] GDAL, Geospatial Data Abstraction Library
http://trac.osgeo.org/gdal/wiki/GdalOgrInPython

[5] Toronto Open Data, Regional Municipal Boundary
http://www.toronto.ca/open

[6] How to do loops on raster cells with python console in QGIS?
http://gis.stackexchange.com/questions/107996/how-to-do-loops-on-raster-cells-with-python-console-in-qgis

[7] Chris Garrard, Utah State University, Reading Raster Data with GDAL
http://www.gis.usu.edu/~chrisg/python/2008/os5_slides.pdf

 

Thought Spot – Crowdmapping of Mental Health and Wellness Resources

Thought Spot is a project designed by post-secondary students to support mental health and wellbeing among Toronto-area youth. The main feature is the online map at http://mythoughtspot.ca/, which is based on the Ushahidi crowdsourced mapping platform. The Thought Spot project was initiated at the Centre for Addiction and Mental Health (CAMH), in collaboration with the University of Toronto, OCAD, and Ryerson. The map allows students to find mental health and wellness resources in ­their geographic area, without the need for an intermediary (parent, teacher, physician). The mapped information originates from ConnexOntario and Kids Help Phone data as well as data that were crowdsourced from members of the target audience.

thoughspot-screenshot

Ryerson Master of Spatial Analysis (MSA) candidate Heather Hart took a lead role in designing the Thought Spot map (shown above), bringing unique geospatial expertise to the table of the project’s student advisory board. Through her MSA practicum placement with a different research group at CAMH, Heather got in contact with the Thought Spot team and brought the funding for her own summer position to Ryerson, to devote half of her time to ensuring that the project’s crowdmapping would be successful. Heather’s involvement culminated in co-organizing a Thought Spot hackathon at Ryerson’s Digital Media Zone in October 2014, which led to the ongoing development of a mobile version of the Thought Spot map.

photo-thoughtspot-heather

This photo shows Heather at GIS Day at Ryerson on November 19th, 2014, presenting the Thought Spot project to an interested University audience. In collaboration with Environmental Applied Science and Management PhD candidate Victoria Fast, Heather has now also submitted a conference abstract about “Crowd mapping mental health promotion through the Thought Spot project”. The abstract brings together Victoria’s extensive expertise in volunteered geographic information systems and Heather’s on-the-ground experience with the Thought Spot project. Their presentation at the annual meeting of the Association of American Geographers in April 2015 is part of the “International Geospatial Health Research” theme.

It is wonderful to see two enterprising Geography graduate students contribute to supporting mental health and wellbeing on campus, a goal that the University is committed to. At the same time, the Thought Spot project informs Heather’s thesis research on the role of maps in evidence-based health care decision-making and Victoria’s dissertation on crowdmapping of local food resources.

Thirty-Two Thousand One Hundred Eighty-Nine Points and Counting

In another little mapping experiment with QGIS and open data from the City of Toronto, I visualized the 32,189 locations of [type-of-incident-withheld] that were recorded in Toronto from 1990 to 2013. I put out a little quiz about this map on Twitter, so I will only reveal what the points represent towards the end of this post. However, the dataset is readily available from Toronto’s open data catalog, both in tabular and GIS-ready Shapefile format.

According to a report by Global News, City crews on occasion have to deal with 20-25 of these incidents a day. As part of their data journalism, Global News created a hexagonal heatmap of the 1990-2013 data, see their article [type of incident will be disclosed].

In contrast, I mapped each point individually using lighter shades of blue for more recent years. While it is often recommended to use the darker and/or more saturated end of a colour scheme for the more important values (arguably the more recent incidents), with the ever more popular black map background, this approach is inverted: the lighter symbols will create the greater contrast, and thus appropriately represent the more important, often the larger, values. The boundaries shown in the background are City wards.

blue-dots-across-toronto_96dpi

As I finish teaching GEO241, our 2nd-year Cartography course in the BA in Geographic Analysis program, I am still having trouble identifying the thematic map type implemented here. It is not a dot density map, as a dot density map uses a unit value (could be seen as 1 dot = 1 incident) and places dots within the area for which the data were collected, but not at the exact location of occurrence. The same reasoning applies to Dr. John Snow’s map of cholera death in London 1854, which is not a dot (density) map either.

Instead, I think this map can be considered a proportional symbol map, where the point symbols at real point locations — not conceptual points such as Census tract centroids — are defined in proportion to a variable (BREAK_YEAR), yet not in terms of their size but in terms of their lightness. Clicking on the above teaser will open the full map with the title Water Main Breaks, City of Toronto, 1990-2013. So yes, there were a whopping 32,189 water main breaks in the City of Toronto during those 24 years! This situation is expected to worsen with the aging municipal infrastructure, see for example the Toronto Star’s 2010 article with a map showing downtown water mains built pre-1900. And it is not a new phenomenon either, as shown by this lovely photograph from the City of Toronto Archives (Fonds 200, Series 372, Subseries 72, Item 31), dated May 3, 1911:

Fonds 200, Series 372, Subseries 72 - Toronto Water Works photog

Toronto’s Traffic Lights Re-Visited and Animated

My map of Toronto’s traffic signals described in a post on April 4th, 2014, was recently published on the title page of Cartouche, the newsletter of the Canadian Cartographic Association (CCA). This is my first-ever published map that is stand-alone, not included in an article or other text document! Here is a screenshot of the newsletter title:

screenshot-cartouche-title-spring2014

Motivated by this unexpected outcome and using the occasion of the launch event of Maptime Toronto on May 29th, 2014, I wanted to try animating the dots representing the traffic signals. More precisely, each traffic light should iterate through a green-yellow-red sequence, and each mid-block pedestrian crossing should go through an off-blinking-off sequence. I was aiming for an animated GIF image with ten frames displayed in a continuous loop.

To create the colour sequences for each dot in QGIS, I copied the last digit of an existing  feature ID from the City of Toronto traffic signals data table into a new field to act as a random group assignment. Using a suggestion by Michael Markieta, I then created nine additional integer fields and cycled through the group numbers by adding 1. To keep these numbers in the 0…9 range, I used QGIS’ “modulo” function, e.g. Cycle1 = (“Cycle2” + 1) % 10. I then assigned the green, yellow, and red dot symbols from the static traffic lights map as a categorized “style” to different group numbers. Finally, I manually iterated the symbology through the ten group columns and took a screenshot each time. I put these together in the animated GIF shown below.

animation_25

I must admit that I am not super convinced of the outcome. Maybe, ten frames are not enough to overcome the clocked appearance of the traffic signal system. But at least, things are moving now :)

It is important to note that this animation does not show the real-time status of the traffic lights! In fact, there is only one dot for an entire intersection that would include two to four sets of vehicle traffic lights, plus pedestrian lights, etc. – all represented by the same green-yellow-red cycle on the map. I also made the assumption that the green and red phases are the same length (4 out of 10 ticks each, with the remaining 2 ticks used for the yellow phase). You will note that the mid-block crossings have an active phase with three on-off cycles followed by a longer off phase. In this case, it would be fancier to individually control each crossing and have it come on randomly.

 

Geographic Analysis student makes a splash with global flight maps

Ryerson University’s graduating Geographic Analysis and incoming Master of Spatial Analysis (MSA) student, Michael Markieta, is making a splash in the UK and North-American media with his beautiful maps of global flight connectivity.

On May 23rd, the UK Daily Mail asked “Can you spot your holiday? The incredible images that reveal exactly where we fly every day” (http://www.dailymail.co.uk/sciencetech/article-2329443/The-incredible-images-reveal-fly-New-images-worlds-flight-paths.html). The next day, the Toronto Star picked up the map images and wrote “Ryerson student produces stunning images of every flight in the world. Our highly interconnected world becomes apparent in a project mapping all the flight paths on the planet” (http://www.thestar.com/news/gta/2013/05/24/ryerson_student_produces_stunning_images_of_every_flight_in_the_world.html).  Metro News Toronto included one of the maps and a short text entitled “Have a visually nice flight” in their May 24th print edition. On May 27th, BBC News posted the series of nine global flight paths visualizations (http://www.bbc.co.uk/news/in-pictures-22657086), followed by a collection of “Five interpretations” by an art critic, environmentalist, aviation consultant, data visualization expert, and philosopher (http://www.bbc.co.uk/news/magazine-22690684). Fox News has published yet another perspective on “Amazing images show airline flight paths across the globe” (http://www.foxnews.com/travel/2013/05/29/amazing-images-show-airline-flight-paths-across-globe/).

Michael first created maps from the OpenFlights airport and airline route databases in September 2011 for his personal blog at http://www.spatialanalysis.ca/2011/global-connectivity-mapping-out-flight-routes/. On his parttime position with global transportation planning and engineering firm Arup’s Toronto office, he redesigned the maps, which were first posted on Arup’s online magazine at http://www.arupconnect.com/2012/12/14/global-flight-paths/.

It is no surprise that a graduate of Ryerson’s Geography department is able to produce intriguing visualizations of a large geospatial dataset. The same data have been mapped by others, and there are alternative ways to look at air traffic, including live mapping of actual flight routes and airplane positions. The journalists reporting on Michael’s maps have somewhat exaggerated the practical uses of such maps, e.g. for communicable disease tracking. However, the blog post by James Cheshire, a lecturer at University College London (UK), on “What’s so Great About a World Flight Paths Map?” at http://spatial.ly/2013/05/great-world-flight-paths-map/ explains three elements that make Michael Markieta’s work so appealing and popular (with the BBC news approaching 15,000 social media shares at the time of my last update, see screenshot below).

markieta-arup-maps_shared-on-BBC