Invalid Geometries in District Boundaries

spatial data
geometry
importance: medium
Author
Published

July 17, 2024

Modified

August 17, 2024


Status: ✅ resolved

Importance: 2 - medium

Summary: There are 3 invalid geometries in the census districts boundaries dataset.

Expected Results: The dataset should not contain any invalid geometries.


Resolution: resolved with https://github.com/Robinlovelace/spanishoddata/pull/25 in {spanishoddata} R package.

To get valid spatial data, use spanishoddata::spod_get_zones()

install.packages("spanishoddata",
  repos = c("https://e-kotov.r-universe.dev", "https://cloud.r-project.org"))

zones <- spanishoddata::spod_get_zones("distr", ver = 2)

Steps to Reproduce

  1. Load Data

Load libraries and define data files.

library(tidyverse)
library(sf)
library(here)
library(DT)


district_boundaries_data_file <- here("data/raw_data/v2/zonificacion/zonificacion_distritos/zonificacion_distritos.shp")
district_names_file <- here("data/raw_data/v2/zonificacion/zonificacion_distritos/nombres_distritos.csv")

Load the data and join the district names to the boundaries.

district_boundaries <- read_sf(district_boundaries_data_file)

district_names <- read_delim(district_names_file,
    delim = "|", show_col_types = FALSE, name_repair = "unique_quiet")

district_boundaries <- district_boundaries |>
    left_join(district_names |> select(ID, name), by = c("ID"))
  1. Identify Invalid Geometries
district_boundaries_invalid <- !st_is_valid(district_boundaries)

Results

  1. Number of Invalid Geometries
sum(district_boundaries_invalid)
[1] 3
  1. Names of Districts with Invalid Geometries
DT::datatable(district_boundaries[district_boundaries_invalid,] |> st_drop_geometry())