Spatial statistics at TWIST2018

Work with the spatially most granular statistical (open) data on the canton of Zurich at the TWIST-Hackdays!

Have you ever wondered in which areas of the canton of Zurich the population is the youngest? Or where the density of employees in a certain industry is the highest? How many people live and work in your neighbourhood? The spatial statistics layers in the cantonal GIS-Browser bear the answers.

Soon, they will be made available as open data via API. As a premiere, for the occasion of the TWIST-Hackdays, preview datasets are made available! Participants will have the chance to work with this data and to combine it with other geodata in unprecedented manner.

first time publication of spatial statistics GIS-layers as open data

In anticipation of the integration into the GIS-ZH webservice, we are publishing test-datasets to be used at the TWIST-Hackdays. We encourage participants with interest in geodata and spatial statistics to explore this new datasets by performing spatial analyses and produce visualizations. The feedback received by participants working with the data during the Hackdays will help us to define the final form of the data-export implemented in the GIS-ZH-webservice.

This data allows for very detailed cartographic explorations of population and employment structures in the canton of Zurich. We want to provide some ideas to get started with at TWIST. However feel free to give us your inputs and submit further proposals:

join our Slack-group!

… or get in touch with us via Twitter

Apart from analyzing the spatial statistics layers themselfes, it could be particularly interesting to combine the spatial statistics layers with other data sources. This allows to go beyond visualizing the spatial distributions of population and enterprises singularly, by setting them into relation with other spatial datasets.

Spatial analyses and cartographic Explorations with OpenStreetMap

For instance, by recurring to the superb openstreetmap-database, one might explore density-patterns of specific data points - such as bars, shops, supermarkes, pharmacies, fitness centres, playgrounds and so on - when set into relation with population and employment. As the spatial statistics layers do not only contain totals, but detailed structure data in form of population age categories as well as industry information in the case of employment, they allow for adressing a myriad of questions with spatial character.

SO… If you’re a spatially oriented mind and want to be among the first to get your hands on the spatially most granular statistical (open) data on the canton of Zurich, we expect you at TWIST!

However, even if you’re not into coding and data, but just looking for a spatial ‘niche’ where to open up a new bar or some other activity in an area with numberous potential customers but with not-so numberous competitors in proximity, join us at the TWIST-Hackdays to see whether we might help you finding it.

This all sounds so wonderful, but where do I start?

Followingly, we provide example code to load the rasterdata into R and to take a first glimpse at it. The data-files are available for download here:

1. Population

2. Employment

statistics layers

So let’s load the data into R…


#load packages

#install & load dev-version of tmap

# population data - values for raster-cells with population totals < 10 have been excluded
pop <- read.csv("")

# employment data - values for raster-cells with a number of enterprises <= 3 have been excluded
empl <- read.csv("", sep=";")

Load Population & Employment Raster Data

The raster package is the reference R package for raster processing. We can use it to transform the data into R-friendly raster objects. Subsequently, we can use the tmap-package to take a first glimpse at the data.

# Population raster ---------

# create spatial points data frame
coordinates(pop) <- ~ GKODE_HEKTARMP + GKODN_HEKTARMP
# coerce to SpatialPixelsDataFrame
gridded(pop) <- TRUE

# plot(pop)

# Generate RasterBrick object containing all the layers
pop_layers <- brick(pop)

#define crs & projection, set to swiss projection
crs(pop_layers) <- "+init=epsg:2056" 

#transform to WGS84
pop_layers2 <- projectRaster(pop_layers, crs="+init=epsg:4326 +proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +units=m +no_defs")

#plot the Layer "Population Totals"
# plot(pop_layers[["TOT"]])

#visualize raster

#configure tmap to display data as interactive leaflet map

#visualize population raster
tm_shape(pop_layers) +
    tm_raster("TOT", palette = "Greys", title = "Inhabitants per 100m2")

For the emplyoment raster, we can just repeat the same steps.

# Employment raster --------

coordinates(empl) <- ~ ha_x + ha_y

gridded(empl) <- TRUE

empl_layers <- raster(empl)

crs(empl_layers) <- "+init=epsg:2056" 

empl_layers <- projectRaster(empl_layers, crs="+init=epsg:4326 +proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +units=m +no_defs")

tm_shape(empl_layers) +
tm_raster(palette = "Oranges", title = "Employees per 100m2")

This is how the data can be imported and quickly visualized… lets move on to getting the openstreetmap-data into R.

Getting Openstreetmap-data straight into R

OpenStreetMap is a collective map of the world. It contains information about towns’ limits, roads, placenames, but also tags of everything. For those who wish to combine the rasters with information coming from openstreetmap, the OSM-Database can be easily accessed via API (Overpass API). With the osmdata-package the data can be loaded directly into R. Some example R-Code to get the data on the locations of bars in Zurich via OSM is provided below.

The OSM-layers can be imported into R with the osmdata-package. In combination with the sf-package, R’s (new) workhorse for spatial data, we can extract the layers of interest handily.


#install tmap for visualization purpouses

#get BoundingBox of the canton of Zurich
# getbb("Kanton Zürich")

q0 <- opq(bbox = c(8.35768, 47.15944,8.984941,47.694472))

# extract   boundaries of the Canton of Zurich via wikidati URI
q1 <- add_osm_feature(opq = q0, key = 'wikidata', value = "Q11943")
# retrieve the data
res1 <- osmdata_sf(q1)
#get geometry
zh <- st_geometry(res1$osm_multipolygons)

# extract Bars & Pubs
q6 <- add_osm_feature(opq = q0, key = 'amenity', value = "bar")
bars <- osmdata_sf(q6)
bars_zh<- st_geometry(bars$osm_points)

# plot the bars in the canton of Zurich


That’s all it takes to load the data itself and some additional OSM-data into R and to get ready for further analysis!


The following two blogposts contain detailed examples of how openstreetmap-data can be loaded into R and what can be done with:

Alternatively ‘bulks’ of OSM-data are available for download here:


Register Now!