Skip to content

Commit

Permalink
refactored function to download osm data. Cleaned up everything
Browse files Browse the repository at this point in the history
  • Loading branch information
marcboschmatas committed Nov 10, 2024
1 parent 6d506a4 commit 50f3aee
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 51 deletions.
26 changes: 15 additions & 11 deletions scripts/01_intersection_rivers_comarques.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"))
}
Expand All @@ -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")
57 changes: 36 additions & 21 deletions scripts/02_download_osm_data.R
Original file line number Diff line number Diff line change
@@ -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))


Expand All @@ -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)


50 changes: 31 additions & 19 deletions web/dashboard.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```


Expand All @@ -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)
```
Expand All @@ -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))
```

:::
Expand All @@ -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",
Expand Down

0 comments on commit 50f3aee

Please sign in to comment.