This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#with help from Gemini, Google's AI
#
# Mediterranian Sea - use panel background as blue
# province, brown fills land area around Barcelona
# bcn_outline is municipality
# "Besòs" river with some flecibility in spelling
# llobregat
library(mapSpain)
library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(knitr)
library(ggplot2)
filename <- "../data/bcnprovinceoutline.rda"
if(file.exists(filename)){
load(filename)
} else {
knit_print("file 1not found")
}
filename <- "../data/barcelonaoutline.rda"
if(file.exists(filename)){
load(filename)
} else {
knit_print("file 2 1not found")
}
bcn_bb <- getbb("Barcelona")
### sea
sea_bb <- bcn_bb
sea_bb[1] <- sea_bb[1] - 0.05 # West
sea_bb[2] <- sea_bb[2] - 0.05 # South
sea_bb[3] <- sea_bb[3] + 0.05 # East
sea_bb[4] <- sea_bb[4] + 0.05 # North
#######rivers
# 1. Query for the Besòs (Northeast)
filename <- "../data/besosriver.rda"
if(file.exists(filename)){
load(filename)
} else {
knit_print("file 3 1not found")
}
besos_line <- besos_data$osm_lines
names(besos_data)
## [1] "bbox" "overpass_call" "meta"
## [4] "osm_points" "osm_lines" "osm_polygons"
## [7] "osm_multilines" "osm_multipolygons"
(besos_line)
## Simple feature collection with 1 feature and 6 fields
## Geometry type: LINESTRING
## Dimension: XY
## Bounding box: xmin: 2.187395 ymin: 41.419 xmax: 2.251363 ymax: 41.54674
## Geodetic CRS: WGS 84
## osm_id name boat name:ca waterway width
## 4753746 4753746 Riu Besòs no Riu Besòs river 15
## geometry
## 4753746 LINESTRING (2.251363 41.546...
# 2. Query for the Llobregat (Southwest)
# Note: We use "El Llobregat" as it is often tagged with the article in OSM
filename <- "../data/llobregatriver.rda"
if(file.exists(filename)){
load(filename)
llobregat_long <- llobregat_data$osm_lines
(llobregat_long)
llobregat_long <- st_transform(llobregat_long, st_crs(province_outline))
llobregat_cropped <- st_crop(llobregat_long, province_outline)
sites <- st_point(c(2.16, 41.42 ))
sea <- st_point(c(2.114, 41.269033407881665))
riversB <- ggplot() +
geom_sf(data = province_outline, fill = "brown", color = "black") +
geom_sf(data = bcn_outline, fill = "grey95", color = "black") +
geom_sf(data = besos_line, color = "blue", size = 1) +
geom_sf(data = llobregat_cropped, color = "blue", size = 1.2) +
annotate("text",x=sites[1],y=sites[2],label="Barcelona",color="blue",fontface="italic")+
annotate("text",x=sea[1],y=sea[2],label="Mediterranean Sea",color="blue",fontface="italic")+
theme(panel.background=element_rect(fill='#a2d2ff',colour='red')) +
coord_sf(xlim = c(sea_bb[1], sea_bb[3]),
ylim = c(sea_bb[2], sea_bb[4]),
expand = FALSE) +
labs(title = "Barcelona between Besòs (north) and Llobregat (south)")
riversB
} else {
knit_print("file 4 1not found")
}
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries