This page is part of a series of blog posts written in preparation of a presentation at the FOSS4G Conference in Brussels (25 October 2018).

Exercises with Belgian data

And because we will be in the capital of Belgium we have compiled a special dataset with Belgian municipalities and their population numbers in 2018: Belgium2018.json. The content of this file is based on data from official sources, both Statbel and NGI-IGN - see Belgium2018_README.txt.

The GeoJSON file Belgium2018.json is hosted on this website (in a zip file). Please proceed to the R code below to download and unzip these data to be able to explore them with R.

Download and unzip the GeoJSON file

# Store the URL to the file to download in a variable
URL2zip <- "http://www.twiav.nl/files/Belgium2018.zip"

# Create a temporary file 
zip_file <- tempfile(fileext = ".zip")

# Download the file
download.file(URL2zip, destfile = zip_file, mode = "wb")

# Create a subfolder in your working directory to store the unzipped data
dir.create("./Data", showWarnings = FALSE)

# Unzip the file
unzip(zip_file, exdir = "./Data")
      
# After unzipping you can delete (i.e. unlink) the file
unlink(zip_file)

# Remove variables you do not longer need
rm(URL2zip, zip_file)

The package sf (Simple Features for R)

To read spatial data into R you will use the library sf, a relativle new addition to the R universe. The package was released on CRAN in January 2017.

This package provides support for simple features, which is a standardized way to encode spatial vector data.

sf links directly to three important geospatial libraries, to unlock their power for use in R:

The real geospatial powers behind sf Website
GDAL: the Geospatial Data Abstraction Library is a translator library for raster and vector geospatial data formats. http://www.gdal.org/
GEOS: the Geometry Engine, Open Source contains the complete functionality of the OpenGIS Simple Features for SQL spatial predicate functions and spatial operators. https://trac.osgeo.org/geos
Proj.4: PROJ is a generic coordinate transformation software, that transforms coordinates from one coordinate reference system (CRS) to another. This includes cartographic projections as well as geodetic transformations. http://proj4.org/

The package sf on CRAN:
https://cran.r-project.org/package=sf

If the package is not yet installed, you can install it with the following command:

install.packages("sf")

Load the package sf

To be able to read your spatial data you will first have to load the package sf:

library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3

And now you can load and plot the spatial data:

Load the GeoJSON file

BE_Municipalities2018 <- st_read("./Data/Belgium2018.json")
## Reading layer `Belgium2018' from data source `C:\Belgium\Presentation\FOSS4G\Data\Belgium2018.json' using driver `GeoJSON'
## Simple feature collection with 589 features and 22 fields
## geometry type:  MULTIPOLYGON
## dimension:      XYZ
## bbox:           xmin: 521989.4 ymin: 521165.1 xmax: 795171.9 ymax: 744030.5
## epsg (SRID):    3812
## proj4string:    +proj=lcc +lat_1=49.83333333333334 +lat_2=51.16666666666666 +lat_0=50.797815 +lon_0=4.359215833333333 +x_0=649328 +y_0=665262 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
plot(st_geometry(BE_Municipalities2018), col = "darkgreen", border = "lightgray" )