Bizkaia

Author

Diego Hernangómez

Published

November 29, 2023

Bizkaia

Servicio en https://www.bizkaia.eus/es/inspirebizkaia

Ejemplo de llamada

Parámetros mínimos de entrada

x <- c(-2.9432804686, 43.2588395267, -2.9253418547, 43.2694336275) # En 4326
count <- 3000 # Numero maximo a retornar, default en el server 1000
verbose <- FALSE

Producto mínimo viable

# El bbox necesitamos que esté en 25830
x_mod <- x
class(x_mod) <- "bbox"
x_mod <- sf::st_as_sfc(x_mod)
sf::st_crs(x_mod) <- sf::st_crs(4326)
x_poly_25830 <- sf::st_transform(x_mod, 25830)
bbox_q <- sf::st_bbox(x_poly_25830)

# Construimos la query

api_entry <- "https://geo.bizkaia.eus/arcgisserverinspire/rest/services/Catastro/Buildings/MapServer/exts/InspireFeatureDownload/service?"

query_params <- list(
  version = "2.0.0",
  request = "GetFeature",
  service = "WFS",
  typeNames = "bu-ext2d:Building",
  count = count,
  SRSname = "EPSG:25830",
  BBOX = paste0(bbox_q, collapse = ",")
)

# Construye la query final
q_text <- paste0(names(query_params), "=", query_params, collapse = "&")

api_end <- paste0(api_entry, q_text, ",urn:ogc:def:crs:EPSG::25830")

# Output temporal
out_temp <- tempfile(fileext = ".gml")

download.file(api_end, out_temp, quiet = !verbose)

Resultado

x_sf <- sf::read_sf(out_temp, quiet = TRUE) # OK
x_sf <- sf::st_transform(x_sf, 25830)

class(x_sf)
#> [1] "sf"         "tbl_df"     "tbl"        "data.frame"

Comprobación

library(ggplot2)

identical(sf::st_crs(x_poly_25830), sf::st_crs(x_sf))
#> [1] TRUE

ggplot() +
  geom_sf(data = x_poly_25830, aes(color = "BBOX"), fill = NA, linewidth = 2) +
  geom_sf(data = x_sf, aes(fill = "API Result")) +
  scale_colour_manual(values = "black") +
  labs(
    fill = "",
    color = ""
  )

Visualización

library(tidyverse)
buf <- sf::st_centroid(x_poly_25830) %>%
  sf::st_buffer(750)

dataviz <- sf::st_intersection(x_sf, buf)

# Extract 4 initial positions
year <- substr(dataviz$end, 1, 4)

# Replace all that doesn't look as a number with 0000
year[!(year %in% 0:2500)] <- "0000"

# To numeric
year <- pmax(1900, as.integer(year))


dataviz <- dataviz %>%
  mutate(
    year = year,
    year_cat = ggplot2::cut_width(year, width = 10)
  )

# Paleta
dataviz_pal <- hcl.colors(
  length(levels(dataviz$year_cat)),
  "Spectral"
)

ggplot(dataviz) +
  geom_sf(aes(fill = year_cat), color = NA) +
  scale_fill_manual(values = dataviz_pal) +
  theme_void() +
  labs(title = "Bilbao", fill = "") +
  theme(
    panel.background = element_rect(fill = "black"),
    plot.background = element_rect(fill = "black"),
    legend.justification = .5,
    legend.text = element_text(
      colour = "white",
      size = 12
    ),
    plot.title = element_text(
      colour = "white", hjust = .5,
      margin = margin(t = 30),
      size = 30
    ),
    plot.caption = element_text(
      colour = "white",
      margin = margin(b = 20), hjust = .5
    ),
    plot.margin = margin(r = 40, l = 40)
  )