Skip to content

Instantly share code, notes, and snippets.

@filipwastberg
Created October 25, 2023 13:54
Show Gist options
  • Save filipwastberg/eb5b22ec75411babb987967c2cd57ac3 to your computer and use it in GitHub Desktop.
Save filipwastberg/eb5b22ec75411babb987967c2cd57ac3 to your computer and use it in GitHub Desktop.
Sveriges äldsta byggnader
library(sf)
library(dplyr)
library(stringr)
library(rvest)
download.file("https://gis-services.metria.se/raafeed/files/gpc/BU.BuildingsGPKG.zip",
destfile = "buildingsGPKG.zip")
unzip("BuildingsGPKG.zip", files = "BU.Buildings.gpkg")
buildings <- st_read("shiny-app/BU.Buildings.gpkg")
get_building_data <- function(url){
resp <- read_html(url)
url_id <- stringr::str_extract(url, "[:digit:]+$")
table <- resp |>
html_table()
if(length(table) < 2){
df <- tibble::tibble(
names = c("Namn",
"Nybyggnadsår",
"Län",
"Kommun",
"Landskap",
"Socken",
"Församling",
"Stift",
"Adress",
"Historisk kategori",
"Nuvarande kategori"),
values = NA,
id = url_id
)
} else {
df <- table[[2]]
df$id <- url_id
colnames(df) <- c("names", "values", "id")
}
df |>
tidyr::pivot_wider(
names_from = names, values_from = values
) |>
janitor::clean_names()
}
get_building_data(url)
library(purrr)
building_data <- map_df(buildings$url, get_building_data)
buildings_complete <- dplyr::bind_cols(
buildings,
building_data
)
#tranform from sweref to wsg84
buildings_complete <- buildings_complete |> sf::st_transform(crs = 4326)
coordinates <- buildings_complete |>
sf::st_coordinates() |>
as_tibble() |>
rename(
lng = X,
lat = Y
)
buildings <- buildings_complete |>
as_tibble() |>
select(-geom) |>
bind_cols(coordinates) |>
mutate(
byggnadsar_start = str_extract(nybyggnadsar, "[:digit:]+"),
sitename = if_else(str_detect(sitename, ";"), str_extract(sitename, ".*(?=;)"), sitename),
sitename = if_else(is.na(sitename), "Okänd", sitename),
byggnadsar_klart = str_extract(nybyggnadsar, " [:digit:]+"),
historisk_kategori_smal = str_extract(historisk_kategori, "[:alpha:]+"),
across(byggnadsar_start:byggnadsar_klart, as.numeric),
across(c("sitename", "namn"), ~stringr::str_trim(stringr::str_to_title(.)))
)
#buildings <- readr::read_csv("shiny-app/data/buildings_complete.csv")
#write to csv
#buildings_complete <- readr::read_rds("shiny-app/data/buildings_complete.RDS")
buildings$sitename <- buildings_complete$sitename
buildings <- buildings |>
mutate(
#across(c("sitename", "namn"), ~stringr::str_trim(stringr::str_to_title(.))),
sitename = stringr::str_trim(stringr::str_to_title(sitename)),
sitename = if_else(str_detect(sitename, ";"), str_extract(sitename, "[^;]*"), sitename),
sitename = if_else(is.na(sitename), "Okänd", sitename))
buildings |>
mutate(
#across(c("sitename", "namn"), ~stringr::str_trim(stringr::str_to_title(.))),
sitename = stringr::str_trim(stringr::str_to_title(sitename)),
sitename = if_else(str_detect(sitename, ";"), str_extract(sitename, "[^;]*"), sitename),
sitename = if_else(is.na(sitename), "Okänd", sitename)) |>
filter(str_detect(sitename, ";")) |>
pull(sitename)
readr::write_csv(buildings, "buildings_complete.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment