diff --git a/scripts/01_intersection_rivers_comarques.R b/scripts/01_intersection_rivers_comarques.R index 4a0ef8d..ff2c327 100644 --- a/scripts/01_intersection_rivers_comarques.R +++ b/scripts/01_intersection_rivers_comarques.R @@ -2,19 +2,23 @@ library(sf) library(tidyverse) # clean and prepare data -# data was in my home directory, can be provided by request, not uploading because it would weigh down the repo too badly. -water <- st_read("~/aigua_icgc.gpkg") # downloaded w/ qgis from https://sig.gencat.cat/ows/AIGUA/wfs, layer AIGUA_XARXA_RIUS_ARC +# data was in my home directory, can be provided by request, +# not uploading because it would weigh down the repo too badly. +water <- st_read("~/aigua_icgc.gpkg") +# downloaded w/ qgis +# from https://sig.gencat.cat/ows/AIGUA/wfs, layer AIGUA_XARXA_RIUS_ARC comarques <- st_read("~/Baixades/divisions-administratives-v2r1-comarques-100000-20240705.json") -comarques <- comarques[,c("NOMCOMAR")] +comarques <- comarques[, c("NOMCOMAR")] comarques <- st_transform(comarques, st_crs(water)) -water_comarques <- st_intersection(st_make_valid(water), st_make_valid(comarques)) +water_comarques <- st_intersection(st_make_valid(water), + st_make_valid(comarques)) - -water_comarques <- st_transform(water_comarques, "EPSG:4326") # back transform to make it compatible w/ josm +# transform it to be compatible w/ josm +water_comarques <- st_transform(water_comarques, "EPSG:4326") water_comarques$geometry |> st_geometry_type() |> unique() @@ -23,8 +27,8 @@ nomcoms <- unique(comarques$NOMCOMAR) # save water courses by comarca -for(i in 1:length(nomcoms)){ - water_comarques_i <- water_comarques[water_comarques$NOMCOMAR == nomcoms[i],] +for (i in seq_along(nomcoms)){ + water_comarques_i <- water_comarques[water_comarques$NOMCOMAR == nomcoms[i], ] st_write(water_comarques_i, paste0("data_osm/water_courses_", nomcoms[i], ".geojson")) } @@ -35,9 +39,9 @@ for(i in 1:length(nomcoms)){ # get sum of water course distances by comarca for app -water_comarques_distance <- water_comarques |> - mutate(dist = as.numeric(st_length(geometry))) |> - st_drop_geometry() |> +water_comarques_distance <- water_comarques |> + mutate(dist = as.numeric(st_length(geometry))) |> + st_drop_geometry() |> summarise(icgc_distance = sum(dist), .by = NOMCOMAR) save(water_comarques_distance, file = "data_web/distances_igcg.rda") diff --git a/scripts/02_download_osm_data.R b/scripts/02_download_osm_data.R index 852a74b..ed79a95 100644 --- a/scripts/02_download_osm_data.R +++ b/scripts/02_download_osm_data.R @@ -1,39 +1,56 @@ library(osmdata) library(sf) - comarques <- st_read("data_web/comarques.json") comarques <- st_transform(comarques, "EPSG:25831") nomcoms <- unique(comarques$NOMCOMAR) +nomcoms <- sort(nomcoms) +# download osm ids for all comarques + +cids <- getbb("Catalonia", format_out = "osm_type_id") |> + opq(osm_types = "rel", + out = "tags") |> + add_osm_feature("admin_level", "7") |> + osmdata_data_frame() + +cids$name[cids$name == "la Selva"] <- "Selva" +cids <- cids[cids$name %in% nomcoms, c("osm_id", "name")] -get_length_osm <- function(comarca){ - water_i <- getbb(comarca, format_out = "osm_type_id", viewbox = c(0.16, 40.53, 3.4, 42.9)) |> - opq() |> +cids <- cids[order(cids$name), ] + +get_length_osm <- function(comarca, id) { + water_i <- opq(paste0("relation(id:", id, ")")) |> add_osm_features(list("waterway" = "river", "waterway" = "ditch", "waterway" = "stream", "waterway" = "drain", - "waterway" = "canal")) |> + "waterway" = "canal")) |> osmdata_sf() - - - water_i <- rbind(water_i$osm_lines[,"osm_id"], water_i$osm_multilines[,"osm_id"]) - - water_i <- st_transform(water_i, "EPSG:25831") - - water_i <- st_intersection(st_make_valid(water_i), st_make_valid(comarques[comarques$NOMCOMAR == comarca,c("NOMCOMAR")])) - + + + water_i <- rbind(water_i$osm_lines[, "osm_id"], + water_i$osm_multilines[, "osm_id"]) + + water_i <- st_make_valid(st_transform(water_i, "EPSG:25831")) + + com <- st_make_valid(comarques[comarques$NOMCOMAR == comarca, + c("NOMCOMAR")]) + water_i <- st_intersection(water_i, + com) + ls <- as.numeric(st_length(water_i$geometry)) return(sum(ls)) } -lengths_osm <- lapply(nomcoms, get_length_osm) +lengths_osm <- mapply(get_length_osm, + cids$name, cids$osm_id) -lengths_osm <- data.frame("NOMCOMAR" = nomcoms, + +lengths_osm <- data.frame("NOMCOMAR" = cids$name, "length_osm" = as.numeric(lengths_osm)) @@ -42,14 +59,12 @@ lengths_osm <- data.frame("NOMCOMAR" = nomcoms, load("data_web/distances_igcg.rda") -comarques_web <- comarques[,"NOMCOMAR"] |> - merge(lengths_osm) |> - merge(water_comarques_distance) |> +comarques_web <- comarques[, "NOMCOMAR"] |> + merge(lengths_osm) |> + merge(water_comarques_distance) |> st_as_sf() colnames(comarques_web) <- c("comarca", "length_osm", "length_icgc", "geometry") -comarques_web$ratio <- comarques_web$length_osm/comarques_web$length_icgc +comarques_web$ratio <- comarques_web$length_osm / comarques_web$length_icgc st_write(comarques_web, "data_web/data_quarto.gpkg", append = FALSE) - - diff --git a/web/dashboard.qmd b/web/dashboard.qmd index dd2dbd3..591069d 100644 --- a/web/dashboard.qmd +++ b/web/dashboard.qmd @@ -11,8 +11,9 @@ echo: false ```{r} library(tinytable) library(sf) - -comarques <- st_read("https://github.com/OSM-Catalan/caminsdaigua/raw/main/data_web/data_quarto.gpkg",quiet = TRUE) +url <- "https://github.com/OSM-Catalan/caminsdaigua/raw/main/data_web/data_quarto.gpkg" +comarques <- st_read(url, + quiet = TRUE) ``` @@ -36,14 +37,17 @@ comarques_nogeom <- st_drop_geometry(comarques) ```{r} -comarques_nogeom$length_osm <- scales::comma(comarques_nogeom$length_osm/1000, - big.mark = ".", - decimal.mark = ",") -comarques_nogeom$length_icgc <- scales::comma(comarques_nogeom$length_icgc/1000, - big.mark = ".", - decimal.mark = ",") -comarques_nogeom$ratio <- scales::percent(comarques_nogeom$ratio, decimal.mark = ",", accuracy = .01) +comarques_nogeom$length_osm <- comarques_nogeom$length_osm / 1000 |> + scales::comma(big.mark = ".", + decimal.mark = ",") + +comarques_nogeom$length_icgc <- comarques_nogeom$length_icgc / 1000 |> + scales::comma(big.mark = ".", + decimal.mark = ",") + +comarques_nogeom$ratio <- scales::percent(comarques_nogeom$ratio, + decimal.mark = ",", accuracy = .01) tt(comarques_nogeom) ``` @@ -57,19 +61,27 @@ tt(comarques_nogeom) ::: {.column width="20%"} ```{r} #| output: asis -cat(paste0("Longitud cursos d'aigua ICGC: ", paste(scales::comma(round(sum(comarques$length_icgc)/1000, 2), - big.mark = ".", - decimal.mark = ","), " km"))) + +licgc <- round(sum(comarques$length_icgc) / 1000, 2) +cat("Longitud cursos d'aigua ICGC: ", paste(scales::comma(licgc, + big.mark = ".", + decimal.mark = ","), + " km")) ``` ```{r} #| output: asis -cat("Longitud cursos d'aigua OSM: ", paste(scales::comma(round(sum(comarques$length_osm)/1000, 2), - big.mark = ".", - decimal.mark = ","), " km")) + +losm <- round(sum(comarques$length_osm) / 1000, 2) +cat("Longitud cursos d'aigua OSM: ", paste(scales::comma(losm, + big.mark = ".", + decimal.mark = ","), + " km")) ``` ```{r} #| output: asis -cat("Percentatge de cursos d'aigua a OSM: ", scales::percent(sum(comarques$length_osm)/sum(comarques$length_icgc), .01)) + +perc <- sum(comarques$length_osm) / sum(comarques$length_icgc) +cat("Percentatge de cursos d'aigua a OSM: ", scales::percent(perc, .01)) ``` ::: @@ -83,9 +95,9 @@ cat("Percentatge de cursos d'aigua a OSM: ", scales::percent(sum(comarques$lengt ```{r} #| layout-nrow: 2 #| layout-ncol: 2 -par(mfrow = c(2,2)) -comarques$length_icgc <- comarques$length_icgc/1000 -comarques$length_osm <- comarques$length_osm/1000 +par(mfrow = c(2, 2)) +comarques$length_icgc <- comarques$length_icgc / 1000 +comarques$length_osm <- comarques$length_osm / 1000 plot(comarques["length_icgc"], breaks = "equal",