Lab – OpenStreetMap & neighbourhood effects

TN Square

Author

Maël Lecoursonnais |
Maria Brandén |

Published

October 28, 2026

In this lab, we will reproduce the analysis of Lecoursonnais et al. (2025) on the association between the distance to universities and the choice of an academic track in upper secondary school.

Using R and the sf and osmdata packages, we will:

  1. Retrieve the universities in Stockholm County from OpenStreetMap (OSM).
  2. Map them and compute the distance to the nearest university for each DeSO.
  3. Merge the distance with the DeSO data set and describe the association with academic track choice.

The focus is more on retrieving and describing data than on interpreting them, even more so because we will observe association at the level of neighbourhoods, not individuals.

The goal is to learn how to work with OSM and spatial data in R, and to think about the choices involved in cleaning and merging data.

If you are new to R, work with someone who is not ;)

Packages

You will need the following packages. Install them once:

install.packages("tidyverse")
install.packages("sf")
install.packages("osmdata")
install.packages("tmap")

Then load them at the beginning of every session:

library(tidyverse)
library(sf)
library(osmdata)
library(tmap)

We use tmap (version 4) for all maps: tmap_mode("view") for interactive maps, tmap_mode("plot") for static ones. The same code works in both modes.

Data

Download the data folder and put it in your project folder. It contains:

  • deso_stockholm.gpkg: the DeSO 2018 polygons of Stockholm County, clipped to land so that maps do not cover lakes and the sea.
  • deso_stockholm_data.csv: the data set for the lab. One row per DeSO in the municipality of Stockholm (see the codebook in Section 5).
  • kommun_stockholm.gpkg: the borders of the municipalities of Stockholm County.
  • uni_osm.rds: a copy of the OSM query of question 1, in case the server does not answer.
  • uniPoly.RData: the universities and campuses in Sweden, cleaned and classified by hand (type, year of creation), from Lecoursonnais et al. (2025). Use it to check your results.

1. Universities in Stockholm

Querying OpenStreetMap

Recall the three steps of an OSM query: (1) define a bounding box (using getbb and opq), (2) add the features you want (add_osm_feature, with a key and a value), and (3) convert the result to the sf format (osmdata_sf).

  1. Retrieve all the features tagged amenity = university in Stockholm County (Stockholms län). Here, amenity is the key and university is the value.

    Note: this operation may take a few minutes, and the server may not answer if too many people are querying it at the same time. If that happens, see the hint below.

    • What does the object contain? How many points, polygons and multipolygons are there?
    • How many of them have a name?
Hint
uni_osm <- getbb("Stockholms län", featuretype = "state") |>
  opq(timeout = 300) |>
  add_osm_feature(key = "amenity", value = "university") |>
  osmdata_sf()

uni_osm
The server does not answer / HTTP 429 Too Many Requests

The Overpass server limits the number of queries coming from the same place, and we are many in the same room! You can either switch to another server:

set_overpass_url("https://overpass.kumi.systems/api/interpreter")

or, if nothing works, load a copy of the result of this query, saved before the lab, and go on with question 2:

uni_osm <- readRDS("data/uni_osm.rds")
  1. Use unique_osmdata to clean the output of your request. Then, keep only the points, polygons and multipolygons that have a name, and put them in a single sf object called uni_raw. Inspect the columns and only keep osm_id, name, name:en, and geometry. Finally, create a new column geometry_type with the geometry type of each feature using st_geometry_type. This will tell you whether the feature is a point, polygon or multipolygon.
Hint
uni_raw <- 
  unique_osmdata(uni_osm)[c("osm_points", "osm_polygons", "osm_multipolygons")] |>
  bind_rows() |>
  filter(!is.na(name)) |>
  select(osm_id, name, name_en = `name:en`, geometry) |>
  st_as_sf() |>
  st_make_valid() |> # Some OSM polygons are invalid (self-intersecting). st_make_valid() fixes them.
  mutate(geometry = unname(geometry), # drop names or tmap fails
         geometry_type = st_geometry_type(geometry)) 

Mapping and cleaning

This is the tricky part. OSM data is collaborative and messy, and cleaning it involves choices that change the results: what counts as a university, what counts as one campus, where a campus “is”. Take the time to look at your data, and write down what you did and why.

  1. Make an interactive map of uni_raw with tmap. Hover over the features to see their names.
    • Are all the features in Stockholm County? Why?
    • Are they all universities?
    • Are some universities counted several times?
Hint
tmap_mode("view")

tm_shape(uni_raw) +
    tm_polygons(fill = "blue", fill_alpha = 0.3, id = "name") +
    tm_dots(fill = "blue", fill_alpha = 0.3, size = 0.5, id = "name")

Run the code below. It loads the DeSO and municipality polygons of Stockholm County (both in SWEREF99 TM, EPSG 3006), puts uni_raw in the same CRS as deso, and keeps only the features inside the county:

deso <- read_sf("data/deso_stockholm.gpkg")     # DeSOs of Stockholm County
kommun <- read_sf("data/kommun_stockholm.gpkg") # Municipalities of Stockholm County
county <- st_union(kommun)                      # The county, as one polygon

uni <- uni_raw |>
  st_transform(st_crs(deso)) |> # Same CRS for both layers
  st_filter(county)             # Keep the features inside the county
  1. Remove the features that are not higher-education institutions. List the remaining names (sort(unique(uni$name))) and decide which to drop.

    • What counts as a “university”? A campus of a university based elsewhere? A small art school? A research station?
Hint
# One possible choice: adapt it!
uni <- uni |>
  filter(!grepl("folk|Studenthälsan|tentamen|skrivsal|Climate Action",
                name, ignore.case = TRUE))
  1. One university is often drawn as many buildings. Create one feature per institution by merging the features that have the same name, and call the result uni_campus. Do not take centroids: keep the merged geometries (polygons and multipolygons, or points when OSM only has a point).

    • Before merging, check that features with the same name are not too far from each other: what is the largest distance between two of them? Should some stay separate campuses?
    • How many features are left?
Hint
# Check: largest distance between two features with the same name
uni |>
  group_by(name) |>
  summarise(n_features = n(),
            max_dist_km = as.numeric(max(st_distance(geometry))) / 1000) |>
  st_drop_geometry() |>
  filter(n_features > 1) |>
  arrange(desc(max_dist_km))

uni_campus <- uni |>
  group_by(name) |>
  summarise(osm_id = first(osm_id),
            name_en = first(name_en[!is.na(name_en)])) |> # summarise() also merges the geometries
  mutate(geometry_type = st_geometry_type(geometry))
Optional: merging by distance only

Merging by name relies on names, which are not always consistent (e.g., “Stockholms konstnärliga högskola” vs. “Stockholms Konstnärliga Högskola”). An alternative is to merge all the features less than 1 km apart, whatever their name, as we did for the whole of Sweden:

clusters <- uni |> st_buffer(500) |> st_union() |> st_cast("POLYGON")

uni$cluster <- unlist(st_intersects(uni, clusters))

uni_campus <- uni |>
  group_by(cluster) |>
  summarise(name = first(name), n_features = n())

What are the pros and cons of each approach?

  1. Compare your list with the one we cleaned by hand for the paper (uniPoly.RData, which also has the type and year of creation of each institution). Names differ between the two lists, so match the campuses by location: which ones have no counterpart within 1 km in the other list?

    • Which institutions are in your list but not in ours, and vice versa? Who is “right”?
    • What does this tell you about using OSM for research?
Hint
load("data/uniPoly.RData")
uni_paper <- uniPoly |> st_filter(st_union(kommun))

uni_paper |> st_drop_geometry() |> select(name, Type, Creation)

# Is there a campus of the other list less than 1 km away?
in_paper <- lengths(st_is_within_distance(uni_campus, uni_paper, dist = 500)) > 0
in_osm   <- lengths(st_is_within_distance(uni_paper, uni_campus, dist = 500)) > 0

uni_campus$name[!in_paper] # In your list, not in ours
uni_paper$name[!in_osm]    # In ours, not in yours

2. Map the universities

  1. Make a static map of the university locations with the municipality borders. Layers are added with +: each tm_shape() starts a new layer.
Hint
tmap_mode("plot")

tm_shape(kommun) +
  tm_borders(col = "grey40", lwd = 0.8) +
  tm_shape(uni_campus) +
  tm_dots(fill = "darkblue", size = 0.2)

3. Merging by boundaries

So far, we have only combined layers by filtering (st_filter). We can also merge them: attach to each university the DeSO and the municipality it lies in, as left_join does with a common key, but using the boundaries instead of a key.

  1. Use st_join to add the DeSO code (desokod) and the municipality (KnNamn, in kommun) to each university.

    • In which municipalities are the universities? How many municipalities of Stockholm County have no university?
    • How many DeSOs have at least one university? Use st_intersects and lengths to count the universities in each DeSO.
    • A campus polygon can overlap several DeSOs, or even two municipalities. What does st_join do then?
Hint
# A new object: a campus over several DeSOs gets one row per DeSO
uni_where <- uni_campus |>
  st_join(select(deso, desokod)) |>            # Which DeSO(s) is each university in?
  st_join(select(kommun, kommun = KnNamn))     # ... and which municipality?

uni_where |> st_drop_geometry() |> distinct(name, kommun) |> count(kommun, sort = TRUE)

# The other way around: how many universities in each DeSO?
deso$n_uni <- lengths(st_intersects(deso, uni_campus))
count(st_drop_geometry(deso), n_uni)

4. Distance to universities

  1. For each DeSO, compute the distance (in km) to the nearest university. Steps:

    • Get one point per DeSO with st_point_on_surface (why not st_centroid?).
    • Find the nearest university with st_nearest_feature. It returns, for each DeSO, the row number of the closest university in uni_campus.
    • Compute the distance between each DeSO and its nearest university with st_distance(..., by_element = TRUE).

    Think about which distance you are measuring.

    • From where? The DeSO point is a proxy for where people live. Is it a good one in a large rural DeSO?
    • To where? For polygons, st_distance measures to the closest edge of the campus (0 if the DeSO point lies inside it). Would the centroid of the campus, or its main entrance, be better?
    • How? st_distance gives the straight line (“as the crow flies”). Across the water in Stockholm, the distance by road or public transport can be very different.
    • Which one fits the mechanism you have in mind (seeing the university every day, the cost of commuting…)?
Hint
deso_pts <- st_point_on_surface(deso)
nearest <- st_nearest_feature(deso_pts, uni_campus)

deso <- deso |>
  mutate(nearest_uni = uni_campus$name[nearest],
         dist_km = as.numeric(st_distance(deso_pts, uni_campus[nearest, ],
                                          by_element = TRUE)) / 1000)
What does st_distance return?

Without by_element = TRUE, st_distance(x, y) returns a matrix with the distance between every feature of x and every feature of y: here, one row per DeSO and one column per university.

dist_all <- st_distance(deso_pts, uni_campus)
dim(dist_all)      # DeSOs x universities
dist_all[1:5, 1:3] # In metres (the unit of the CRS)

# The nearest university is the smallest value of each row:
all.equal(apply(dist_all, 1, min), as.numeric(st_distance(deso_pts, uni_campus[nearest, ],
                                                          by_element = TRUE)))

With by_element = TRUE, it computes the distance between the 1st feature of x and the 1st of y, the 2nd and the 2nd, etc.: much faster when you already know which pairs you want.

  1. Describe the distribution of dist_km, and map it.

    • Which DeSOs are the most remote?
    • Which university is the “nearest” for most DeSOs? Is the nearest university the one people actually go to?
Hint
summary(deso$dist_km)
count(st_drop_geometry(deso), nearest_uni, sort = TRUE)


tm_shape(deso) +
  tm_polygons(fill = "dist_km", col = NULL,
              fill.scale = tm_scale_intervals(breaks = c(0, 1, 2, 5, 10, 25, 100),
                                              values = "-plasma"),
              fill.legend = tm_legend(title = "Distance (km)")) +
  tm_shape(kommun) +
  tm_borders(col = "white", lwd = 0.5) +
  tm_shape(uni_campus) +
  tm_dots(fill = "white", size = 0.3)

5. Merge with the DeSO data set

The file deso_stockholm_data.csv has one row per DeSO in the municipality of Stockholm (Stockholms kommun, code 0180), not the whole county: from now on, we restrict the analysis to the municipality. All variables come from the Swedish registers (accessed through MONA), aggregated by DeSO. The pupils finished grade 9 between 2019 and 2023 (pooled); the neighbourhood characteristics describe the residents of the DeSO in 2022. DeSOs with too few pupils are not included.

Variable Description
BostDESO DeSO 2018 code (desokod in deso_stockholm.gpkg)
n_grade9 Number of pupils finishing grade 9 who lived in the DeSO the year before
n_academic … of which applied to an academic (högskoleförberedande) programme as their first choice
share_academic n_academic / n_grade9
mean_income_rank Mean income rank of the residents (0-1)
share_univ Share of 25-64 year olds with post-secondary education of 3 years or more
share_foreign Share with a foreign background (born abroad, or both parents born abroad)
  1. Load deso_stockholm_data.csv and look at it (glimpse, summary). Keep only the DeSOs of the municipality of Stockholm (kommunkod == "0180"), then merge them with the data using the DeSO code. Note that the code has a different name in each data set.

    • How many DeSOs have no data? Why?
Hint
deso_data <- read_csv("data/deso_stockholm_data.csv")[-1] # Drop the row numbers
glimpse(deso_data)

# left_join keeps all the DeSOs of the municipality, with or without data
deso <- deso |>
  filter(kommunkod == "0180") |>
  left_join(deso_data, by = c(desokod = "BostDESO"))
sum(is.na(deso$share_academic))

# Keep the DeSO points in line with deso, for the next steps
deso_pts <- st_point_on_surface(deso)
  1. Describe the neighbourhoods: map a few of the variables, and compare the DeSOs that are close to a university with those that are far.

    • Who lives close to universities?
Hint
tm_shape(deso) +
  tm_polygons(fill = c("mean_income_rank", "share_univ", "share_foreign"), col = NULL,
              fill.free = TRUE) + # One map per variable, each with its own legend
  tm_shape(uni_campus) +
  tm_dots(fill = "white", size = 0.3)

deso |>
  st_drop_geometry() |>
  mutate(dist_cat = cut(dist_km, breaks = c(0, 2, 5, 10, 25, Inf),
                        include.lowest = TRUE)) |> # Keep the DeSOs at 0 km
  group_by(dist_cat) |>
  summarise(n_deso = n(),
            across(c(mean_income_rank, share_univ, share_foreign),
                   ~ mean(.x, na.rm = TRUE)))

6. Distance and academic track choice

  1. Plot share_academic against dist_km, and map share_academic.

    • What do you see? Is the association linear? Would a log scale help? (Careful: DeSOs inside a campus are at 0 km, and log(0) = -Inf.)
    • Compare the map of distance with the map of academic track choice.
Hint
ggplot(deso, aes(x = dist_km, y = share_academic)) +
  geom_point(aes(size = n_grade9), alpha = 0.3) +
  geom_smooth() +
  scale_x_continuous(transform = "log1p") + # log(1 + x): keeps the 0 km DeSOs
  labs(x = "Distance to the nearest university (km, log(1 + x) scale)",
       y = "Share applying to an academic track", size = "Pupils")

tm_shape(deso) +
  tm_polygons(fill = "share_academic", col = NULL,
              fill.legend = tm_legend(title = "Academic track")) +
  tm_shape(uni_campus) +
  tm_dots(fill = "white", size = 0.3)
Optional: a few regressions

Estimate a few (weighted) linear regressions of share_academic on the distance, then add the neighbourhood characteristics.

  • How does the coefficient of distance change when you control for income and education? What does that tell you?
  • Can we interpret the coefficient causally? Think of selection (who lives close to universities?) and of the mechanisms discussed this morning.
  • Why do we weight by n_grade9?
m1 <- lm(share_academic ~ log1p(dist_km), data = deso, weights = n_grade9)
m2 <- lm(share_academic ~ log1p(dist_km) + mean_income_rank + share_univ + share_foreign,
         data = deso, weights = n_grade9)
summary(m1)
summary(m2)

7. Own work (16.00-17.00)

In groups, extend the analysis in one direction (or come up with your own), and prepare a 5-minute presentation for 17.00. Keep it simple: one map, one figure or table, and one sentence on what it tells us about neighbourhood mechanisms.

Each idea below comes with a question and some code to get you started.

A. Other amenities: is distance to universities special?

Universities are one place among many. Proximity to libraries, upper secondary schools or museums might matter as much, or capture the same thing. Look up the key and value in the list of map features. For example:

Key Value What
amenity library Libraries
amenity school Schools (all levels!)
tourism museum Museums
leisure park Parks
amenity community_centre Community centres
libraries <- getbb("Stockholms län", featuretype = "state") |>
  opq(timeout = 300) |>
  add_osm_feature(key = "amenity", value = "library") |>
  osmdata_sf()

# Libraries are mostly points, but some are buildings (polygons)
lib_pts <- bind_rows(libraries$osm_points |> filter(!is.na(name)) |> select(name),
                     libraries$osm_polygons |> select(name) |> st_centroid()) |>
  st_transform(3006) |>
  st_filter(county)

deso$dist_lib_km <- as.numeric(
  st_distance(deso_pts, lib_pts[st_nearest_feature(deso_pts, lib_pts), ],
              by_element = TRUE)) / 1000
  • Is the distance to a library correlated with the distance to a university?
  • Does the association between distance to a university and academic track choice change when you add the distance to a library?
B. Other measures of access

The nearest university and the number within 5 km are two measures among many. You could:

  • vary the radius (1, 2, 5, 10 km) and see when the association is strongest;
  • weight the universities by size: are SU and KTH more “visible” than a small art school?
  • compute the distance to the second nearest university.
radii <- c(1, 2, 5, 10)

for (r in radii) {
  deso[[paste0("n_uni_", r, "km")]] <-
    lengths(st_is_within_distance(deso_pts, uni_campus, dist = r * 1000))
}

deso |>
  st_drop_geometry() |>
  summarise(across(starts_with("n_uni_"),
                   ~ cor(.x, share_academic, use = "complete.obs")))
C. Which universities?

Our list of universities is a choice. Do the results change if you only keep the large universities, or drop the art schools? Use the Type column of uniPoly (from the paper), or make your own list.

uni_paper |> st_drop_geometry() |> count(Type)

uni_large <- uni_paper |> filter(Type == "University")

nearest_large <- st_nearest_feature(deso_pts, uni_large)
deso$dist_large_km <- as.numeric(
  st_distance(deso_pts, uni_large[nearest_large, ], by_element = TRUE)) / 1000
  • Which DeSOs change the most?
  • Is the association with academic track choice stronger or weaker?
Mapping several layers interactively
tmap_mode("view")

tm_basemap("Esri.WorldGrayCanvas") +
  tm_shape(deso) +
  tm_polygons(fill = "share_academic", fill_alpha = 0.7, col = "white", lwd = 0.2,
              fill.scale = tm_scale_continuous(values = "plasma"),
              fill.legend = tm_legend(title = "Academic track"),
              id = "desokod", popup.vars = c("share_academic", "dist_km", "mean_income_rank")) +
  tm_shape(uni_campus) +
  tm_dots(fill = "black", size = 0.6, id = "name")