Invalid Geometries in Municipality Boundaries

spatial data
geometry
importance: medium
Author
Published

July 16, 2024

Modified

August 17, 2024


Status: ✅ resolved

Importance: 2 - medium

Summary: There are 2 invalid geometries in the municipality 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("municip", ver = 2)

Steps to Reproduce

  1. Load Data

Load libraries and define data files.

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


municipal_boundaries_data_file <- here("data/raw_data/v2/zonificacion/zonificacion_municipios/zonificacion_municipios.shp")
municipality_names_file <- here("data/raw_data/v2/zonificacion/zonificacion_municipios/nombres_municipios.csv")

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

municipality_boundaries <- read_sf(municipal_boundaries_data_file)

municipality_names <- read_delim(municipality_names_file,
    delim = "|", show_col_types = FALSE, name_repair = "unique_quiet")

municipality_boundaries <- municipality_boundaries |>
    left_join(municipality_names |> select(ID, name), by = c("ID"))
  1. Identify Invalid Geometries
municipality_boundaries_invalid <- !st_is_valid(municipality_boundaries)

Results

  1. Number of Invalid Geometries
sum(municipality_boundaries_invalid)
[1] 2
  1. Names of Municipalities with Invalid Geometries
DT::datatable(municipality_boundaries[municipality_boundaries_invalid,] |> st_drop_geometry())