
Introduction
Understanding population distribution is crucial for urban planning, resource allocation, and policy-making. In this project, we will use R to visualize population data in Indonesia using an interactive 3D map powered by deck.gl. The data is sourced from the Kontur Population Dataset, which provides high-resolution global population estimates.
This project focuses on creating an interactive 3D population map using deck.gl, a high-performance WebGL-powered mapping library. The system retrieves high-resolution population data from the Kontur Population Dataset, applies color scaling based on density, and visualizes the results in an interactive 3D environment with extruded polygons.
Project Setup
To build this 3D Population Visualization, ensure you have the following tools and libraries installed:
Prerequisites
- R β A statistical computing environment.
- RStudio β A user-friendly IDE for R.
Required R Packages
The following R packages are needed for this project:
| Package |
Purpose |
sf |
Geospatial data processing |
deckgl |
WebGL-powered mapping visualization |
R.utils |
File handling and compression utilities |
scales |
Color scaling for data visualization |
htmlwidgets |
Exporting interactive maps to HTML |
Building Map
Below are the key steps and script components:
Downloading the Population Dataset
We will use Kontur Population Data and download it as a compressed .gpkg.gz file:
options(timeout = 300)
url <- "https://geodata-eu-central-1-kontur-public.s3.amazonaws.com/kontur_datasets/kontur_population_ID_20231101.gpkg.gz"
filename <- basename(url)
download.file(url, destfile = filename, mode = "wb")
R.utils::gunzip(filename, remove = FALSE)
We use the sf package to read and convert the geospatial data to EPSG:4326 (WGS 84 projection):
pop_df <- sf::st_read(
dsn = gsub(".gz", "", filename)
) |>
sf::st_transform(crs = "EPSG:4326")
});
This allows the data to be correctly projected for visualization.
Applying Color Scaling
To visually differentiate population densities, we define a color palette using the scales package:
pal <- scales::col_quantile("viridis", pop_df$population, n = 6)
pop_df$color <- pal(pop_df$population)
Each population polygon is assigned a color based on density.
Creating the 3D Interactive Map
Now, we use deck.gl to render an interactive extruded polygon layer:
properties <- list(
stroked = TRUE,
filled = TRUE,
extruded = TRUE,
wireframe = FALSE,
elevationScale = 1,
getFillColor = ~color,
getLineColor = ~color,
getElevation = ~population,
getPolygon = deckgl::JS("d => d.geom.coordinates"),
tooltip = "Population: ",
opacity = 0.25
)
map <- deckgl::deckgl(
latitude = -2.548926,
longitude = 117.894722,
zoom = 6, pitch = 45
) |>
deckgl::add_polygon_layer(data = pop_df, properties = properties) |>
deckgl::add_basemap(deckgl::use_carto_style())
This code:
- Extrudes polygons based on population values.
- Applies color-coded density representation.
- Enables interactive tooltips for population data.
Exporting the Map as an HTML File
Finally, we save the interactive map as an HTML file:
htmlwidgets::saveWidget(map, file = "map.html", selfcontained = FALSE)
After complete, you can open map.html in a browser to explore population density interactively!
Summary
In this project, we successfully built an interactive 3D population visualization using R and deck.gl. The key steps included:
- Downloading and extracting Kontur Population Data.
- Loading and transforming geospatial data into EPSG:4326 projection.
- Applying color scaling based on population density.
- Creating an interactive 3D map with extruded polygons using deck.gl.
- Exporting the map as an HTML file for easy sharing and visualization.
With this workflow, we can analyze population distribution visually and interactively, making it useful for urban planning, resource allocation, and demographic studies.
Happy mapping! πΊοΈ