
Codebook and cookbook for v1 (2020-2021) Spanish mobility data
Source:vignettes/v1-2020-2021-mitma-data-codebook.qmdYou can view this vignette any time by running:
spanishoddata::spod_codebook(ver=1)The mobility data v1 (2020-2021) was originally released by the Ministerio de Transportes, Movilidad y Agenda Urbana (MITMA) , nowMinisterio de Transportes y Movilidad Sostenible (MITMS)(Ministerio de Transportes, Movilidad y Agenda Urbana (MITMA) 2021).
The dataset is produced byNommon using the raw data fromOrange España. Even though the raw data is only from one mobile phone operator, the resulting flows and other counts of number of individuals in the data set are already resampled to be representative of the total population of Spain (see details in the official methodology).
The tables in the data set provide hourly flows between zones across Spain for every day of the observation period (2020-02-14 to 2021-05-09), and the number of individuals making trips for each zone. This document will introduce you to the available data and provide brief code snippets on how to access it using thespanishoddata R package.
Key sources for this codebook/cookbook include:
original data collection methodology in Spanish +automatically translated English version of methodology
original data codebook in Spanish +automatically translated English version of codebook
Note: Kindly consult the documents above for any specific details on the methodology. The codebook here is only a simplified summary.
To access the data we reference in this codebook, please follow these steps:
Install the package
Install from CRAN:
install.packages("spanishoddata")Alternative installation and developemnt
You can also install the latest development version of the package from rOpenSpain R universe:
install.packages("spanishoddata", repos=c("https://ropenspain.r-universe.dev","https://cloud.r-project.org"))Alternative way to install the development version from GitHub:
if(!require("remotes"))install.packages("remotes")remotes::install_github("rOpenSpain/spanishoddata", force=TRUE, dependencies=TRUE)For Developers
To load the package locally, clone it and navigate to the root of the package in the terminal, e.g. with the following:
gh repo clone rOpenSpain/spanishoddatacode spanishoddata# with rstudio:rstudio spanishoddata/spanishoddata.RprojThen run the following command from the R console:
devtools::load_all()You can also explore the package and the data in an interactive RStudio container right in your web browser thanks to Binder, just clickthe link or the button:. Note that the session will be limited by memory and you will only be able to work with one full day of data.
Load it as follows:
Using the instructions below, set the data folder for the package to download the files into. You may need up to 30 GB to download all data and another 30 GB if you would like to convert the downloaded data into analysis ready format (aDuckDB database file, or a folder ofparquet files). You can find more info on this conversion in theDownload and convert OD datasets vignette.
Set the data directory
Choose wherespanishoddata should download (and convert) the data by setting the data directory following command:
spod_set_data_dir(data_dir="~/spanish_od_data")The function above will also ensure that the directory is created and that you have sufficient permissions to write to it.
Setting data directory for advanced users
You can also set the data directory with an environment variable:
Sys.setenv(SPANISH_OD_DATA_DIR="~/spanish_od_data")The package will create this directory if it does not exist on the first run of any function that downloads the data.
To permanently set the directory for all projects, you can specify the data directory globally by setting theSPANISH_OD_DATA_DIR environment variable, e.g. with the following command:
usethis::edit_r_environ()# Then set the data directory globally, by typing this line in the file:SPANISH_OD_DATA_DIR="~/spanish_od_data"You can also set the data directory locally, just for the current project. Set the ‘envar’ in the working directory by editing.Renviron file in the root of the project:
file.edit(".Renviron")Overall approach to accessing the data
If you only need flows data aggregated by day at municipal level, you can use thespod_quick_get_od() function. This will download the data directly from the web API and let you analyse it in-memory. More on this in theQuickly get daily data vignette.
If you only want to analyse the data for a few days, you can use thespod_get() function. It will download the raw data in CSV format and let you analyse it in-memory. This is what we cover in the steps on this page.
If you need longer periods (several months or years), you should use thespod_convert() andspod_connect() functions, which will convert the data into special format which is much faster for analysis, for this see theDownload and convert OD datasets vignette.spod_get_zones() will give you spatial data with zones that can be matched with the origin-destination flows from the functions above using zones ’id’s. Please see a simple example below, and also consult the vignettes with detailed data description and instructions in the package vignettes withspod_codebook(ver = 1) andspod_codebook(ver = 2), or simply visit the package website athttps://ropenspain.github.io/spanishoddata/. TheFigure 1 presents the overall approach to accessing the data in thespanishoddata package.
1. Spatial data with zoning boundaries
The boundary data is provided at two geographic levels:Districts andMunicipalities. It’s important to note that these do not always align with the official Spanish census districts and municipalities. To comply with data protection regulations, certain aggregations had to be made to districts and municipalities”.
1.1Districts
Districts correspond to official census districts in cities; however, in those with lower population density, they are grouped together. In rural areas, one district is often equal to a municipality, but municipalities with low population are combined into larger units to preserve privacy of individuals in the dataset. Therefore, there are 2850 ‘districts’ compared to the 10494 official census districts on which they are based.
To access it:
districts_v1<-spod_get_zones("dist", ver=1)Thedistricts_v1 object is of classsf consisting of polygons.
Data structure:
| Variable Name | Description |
|---|---|
id | Districtid assigned by the data provider. Matches withid_origin,id_destination, andid in district levelorigin-destination data andnumber of trips data. |
census_districts | A string with semicolon-separated list of census district semicolon-separated identifiers as classified by the Spanish Statistical Office (INE) that are spatially bound within polygons withid above. |
municipalities_mitma | A string with semicolon-separated list of municipality identifiers as assigned by the data provider in municipality zones spatial dataset that correspond to a given districtid . |
municipalities | A string with semicolon-separated list of municipality identifiers as classified by the Spanish Statistical Office (INE) that correspond to polygons withid above. |
district_names_in_v2 | A string with semicolon-separated list of names of district polygons defined in thev2 version of this data that covers the year 2022 and onwards that correspond to polygons withid above. |
district_ids_in_v2 | A string with semicolon-separated list of identifiers of district polygons defined in thev2 version of this data that covers the year 2022 and onwards that correspond to polygons withid above. |
1.2Municipalities
Municipalities are made up of official municipalities in those of a certain size; however, they have also been aggregated in cases of lower population density. As a result, there are 2,205 municipalities compared to the 8,125 official municipalities on which they are based.
To access it:
municipalities_v1<-spod_get_zones("muni", ver=1)The resultingmunicipalities_v1 object is typesf consisting of polygons.
Data structure:
| Variable Name | Description |
|---|---|
id | Districtid assigned by the data provider. Matches withid_origin,id_destination, andid in municipality levelorigin-destination data andpopulation by trip count. |
municipalities | A list of municipality identifiers as classified by the Spanish Statistical Office (INE) that correspond to polygons withid above. |
districts_mitma | A list of district identifiers as assigned by the data provider in districts zones spatial dataset that correspond to a given municipalityid . |
census_districts | A list of census district identifiers as classified by the Spanish Statistical Office (INE) that are spatially bound within polygons withid above. |
municipality_names_in_v2 | A list of names of municipality polygons defined in thev2 version of this data that covers the year 2022 and onwards that correspond to polygons withid above. |
municipality_ids_in_v2 | A list of identifiers of municipality polygons defined in thev2 version of this data that covers the year 2022 and onwards that correspond to polygons withid above. |
The spatial data you get viaspanishoddata package is downloaded directly from the source, the geometries of polygons are automatically fixed if there are any invalid geometries. The zone identifiers are stored inid column. Apart from thatid column, the original zones files do not have any metadata. However, as seen above, using thespanishoddata package you get many additional columns that provide a semantic connection between official statistical zones used by the Spanish government and the zones you can get for the v2 data (for 2022 onward).
2. Mobility data
All mobility data is referenced viaid_origin,id_destination, or other location identifiers (mostly labelled asid) with the two sets of zones described above.
2.1. Origin-Destination data
The origin-destination data contain the number of trips and distance travelled betweendistricts ormunicipalities in Spain for every hour of every day between 2020-02-14 and 2021-05-09. Each flow also has attributes such as the trip purpose (composed of the type of activity (home/work_or_study/other) at both the origin and destination), province of residence of individuals making this trip, distance covered while making the trip. See the detailed attributes below in a table.Figure 2 shows an example of total flows in the province of Barcelona on Feb 14th, 2020.
Here are the variables you can find in both thedistrict andmunicipality level origin-destination data:
| English Variable Name | Original Variable Name | Type | Description |
|---|---|---|---|
date | fecha | Date | The date of the recorded data, formatted asYYYY-MM-DD. |
hour | periodo | integer | The time slot during which the trips occurred.Note: this column used to be calledtime_slot, it is still going to be available in the output of package functions until the end of 2025, but going forward please usehour instead. |
id_origin | origen | factor | The origin zoneid ofdistrict ormunicipalitity. |
id_destination | destino | factor | The destination zoneid ofdistrict ormunicipalitity. |
distance | distancia | factor | The distance range of the trip, categorized into specific intervals such as0005-002 (500 m to 2 km),002-005 (2-5 km),005-010 (5-10km),010-050 (10-50 km),050-100 (50-100 km), and100+ (more than 100 km). |
activity_origin | actividad_origen | factor | The type of activity at the origin zone, recoded fromcasa,otros,trabajo_estudio tohome,other,work_or_study respectively. |
activity_destination | actividad_destino | factor | The type of activity at the destination zone, similarly recoded as foractivity_origin above. |
residence_province_ine_code | residencia | factor | The province code of residence if individuals who were making the trips inn_trips, encoded as province codes as classified by the Spanish Statistical Office (INE). |
residence_province_name | Derived fromresidencia | factor | The full name of the residence province, derived from the province code above. |
n_trips | viajes | numeric | The number of trips for that specific origin-destination pair and time slot. |
trips_total_length_km | viajes_km | numeric | The total length of trips in kilometers, summing up all trips between the origin and destination zones. |
year | year | integer | The year of the recorded data, extracted from the date. |
month | month | integer | The month of the recorded data, extracted from the date. |
day | day | integer | The day of the recorded data, extracted from the date. |
Data transformation note
The original data is stored in themaestra-2 folder with suffixesdistritos (for district zoning) andmunicipios (for municipality zoning). We only use thedistrict level data because of several data issues with themunicipality data documentedhere andhere, but also because the distric level data contains more columns with useful origin-destination flow characteristics. As a result, you get both thedistrict level data and themunicipality level data with the same columns.Municipality level data is simply a re-aggregation ofdistrict level data using the official relations file wheredistrict identifiers are mapped tomunicipality identifiers (orginal file isrelaciones_distrito_mitma.csv).
Getting the data
To access the data, use thespod_get() function. In this example we will use a short interval of dates:
The data for the specified dates will be automatically downloaded and cached in theSPANISH_OD_DATA_DIR directory. Existing files will not be re-downloaded.
Working with the data
The resulting objectsod_dist andod_muni are of classtbl_duckdb_connection1. Basically, you can treat these as regulardata.frames ortibbles. One important difference is that the data is not actually loaded into memory, because if you requested more dates, e.g. a whole month or a year, all that data would most likely not fit into your computer’s memory. Atbl_duckdb_connection is mapped to the downloaded CSV files that are cached on disk and the data is only loaded in small chunks as needed at the time of computation. You can manipulateod_dist andod_muni usingdplyr functions such asselect(),filter(),mutate(),group_by(),summarise(), etc. In the end of any sequence of commands you will need to addcollect() to execute the whole chain of data manipulations and load the results into memory in an Rdata.frame/tibble like so:
# A tibble: 24 × 2 hour mean_hourly_trips <int> <dbl> 1 18 21.4 2 10 19.3 3 2 14.8 4 15 19.8 5 11 19.9 6 16 19.6 7 22 20.9 8 0 18.6 9 13 21.110 19 22.5# ℹ 14 more rows# ℹ Use `print(n = ...)` to see more rowsIn this example above, we calculated mean hourly flows over the 4 days of the requested period. The full data for all 4 days was probably never loaded into memory all at once. Rather the available memory of the computer was used up to its maximum limit to make that calculation happen, without ever exceeding the available memory limit. If you were doing the same opearation on 100 or even more days, it would work in the same way and would be possible even with limited memory. This is done transparantly to the user with the help ofDuckDB (specifically, with{duckdb} R packageMühleisen and Raasveldt (2024)).
The same summary operation as provided in the example above can be done with the entire dataset for the full 18 month on a regular laptop with 8-16 GB memory. It will take a bit of time to complete, but it will be done. To speed things up, please also see thevignette on converting the data into formats that will increase the analsysis performance.
Note
As long as you use a table connection object created with
spod_get()function, it is much quicker to filter the dates by theyear,monthanddayvariables, rather than by thedatevariable. This is because the data for each day is in a separate CSV file located in folders that look likeyear=2020/month=2/day=14. So when filtering by thedatefield, R will have to scan all CSV files comparing the specified date with what is stored inside each CSV file. However, if you query byyear,monthanddayvariables, R only needs to check these against the path to each CSV file, which is much quicker. This caveat is only relevant as long as you usespod_get(). If you convert (see therelevant vignette) the downloaded data to a format that it optimized for quick analysis, you can use whichever field you want, it should not affect the performance.
2.2. Population by trip count data
The population by trip count data shows the number of individuals in each district or municipality, categorized by the trips they make: 0, 1, 2, or more than 2.
| English Variable Name | Original Variable Name | Type | Description |
|---|---|---|---|
date | fecha | Date | The date of the recorded data, formatted asYYYY-MM-DD. |
id | distrito | factor | The identifier of thedistrict ormunicipality zone. |
n_trips | numero_viajes | factor | The number of individuals who made trips, categorized by0,1,2, or2+ trips. |
n_persons | personas | factor | The number of individuals making the trips fromdistrict ormunicipality with zoneid. |
year | year | integer | The year of the recorded data, extracted from the date. |
month | month | integer | The month of the recorded data, extracted from the date. |
day | day | integer | The day of the recorded data, extracted from the date. |
Data transformation note
The original data is stored in themaestra-2 folder with suffixesdistritos (for district zoning) andmunicipios (for municipality zoning). We only use thedistrict level data because of several data issues with themunicipality data documentedhere andhere, but also because the distric level data contains more columns with useful origin-destination flow characteristics. As a result, you get both thedistrict level data and themunicipality level data with the same columns.Municipality level data is simply a re-aggregation ofdistrict level data using the official relations file wheredistrict identifiers are mapped tomunicipality identifiers (orginal file isrelaciones_distrito_mitma.csv).
Getting the data
To access it usespod_get() withtype set to “number_of_trips”, or just “nt”. We can also setdates to the maximum possible date range2020-02-14 to2021-05-09 to get all the data, as this data is relatively small (under 200 Mb).
Because this data is small, we can actually load it completely into memory:
nt_dist_tbl<-nt_dist|>dplyr::collect()Advanced use
For more advanced use, especially for analysing longer periods (months or even years), please seeDownload and convert mobility datasets.