diff --git a/.Rbuildignore b/.Rbuildignore index a6d0d39..7b7f376 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -18,3 +18,4 @@ ^pkgdown$ ^data-raw$ ^vignettes/ovcservice\.Rmd\.orig$ +^vignettes/CatastRo\.Rmd\.orig$ diff --git a/CITATION.cff b/CITATION.cff index 13b1d63..39f1207 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -92,7 +92,6 @@ references: given-names: Hadley email: hadley@rstudio.com year: '2022' - url: https://CRAN.R-project.org/package=httr - type: software title: lifecycle abstract: 'lifecycle: Manage the Life Cycle of your Package Functions' @@ -106,7 +105,6 @@ references: email: hadley@rstudio.com orcid: https://orcid.org/0000-0003-4757-117X year: '2022' - url: https://CRAN.R-project.org/package=lifecycle - type: software title: mapSpain abstract: 'mapSpain: Administrative Boundaries of Spain' @@ -118,7 +116,6 @@ references: orcid: https://orcid.org/0000-0001-8457-4658 affiliation: rOpenSpain year: '2022' - url: https://CRAN.R-project.org/package=mapSpain version: '>= 0.5.0' - type: software title: rappdirs @@ -133,7 +130,6 @@ references: - family-names: Davis given-names: Trevor year: '2022' - url: https://CRAN.R-project.org/package=rappdirs version: '>= 0.3.0' - type: software title: sf @@ -145,7 +141,6 @@ references: email: edzer.pebesma@uni-muenster.de orcid: https://orcid.org/0000-0001-8049-7069 year: '2022' - url: https://CRAN.R-project.org/package=sf version: '>= 1.0.0' - type: software title: terra @@ -157,7 +152,7 @@ references: email: r.hijmans@gmail.com orcid: https://orcid.org/0000-0001-5872-2872 year: '2022' - url: https://CRAN.R-project.org/package=terra + url: https://rspatial.org/terra/ - type: software title: tibble abstract: 'tibble: Simple Data Frames' @@ -170,7 +165,6 @@ references: given-names: Hadley email: hadley@rstudio.com year: '2022' - url: https://CRAN.R-project.org/package=tibble - type: software title: xml2 abstract: 'xml2: Parse XML' @@ -184,7 +178,6 @@ references: - family-names: Ooms given-names: Jeroen year: '2022' - url: https://CRAN.R-project.org/package=xml2 - type: software title: ggplot2 abstract: 'ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics' @@ -229,7 +222,6 @@ references: email: dewey@fishandwhistle.net orcid: https://orcid.org/0000-0002-9415-4582 year: '2022' - url: https://CRAN.R-project.org/package=ggspatial - type: software title: knitr abstract: 'knitr: A General-Purpose Package for Dynamic Report Generation in R' @@ -240,7 +232,7 @@ references: email: xie@yihui.name orcid: https://orcid.org/0000-0003-0645-5666 year: '2022' - url: https://CRAN.R-project.org/package=knitr + url: https://yihui.org/knitr/ - type: software title: png abstract: 'png: Read and write PNG images' @@ -250,7 +242,7 @@ references: given-names: Simon email: Simon.Urbanek@r-project.org year: '2022' - url: https://CRAN.R-project.org/package=png + url: http://www.rforge.net/png/ - type: software title: raster abstract: 'raster: Geographic Data Analysis and Modeling' @@ -261,7 +253,7 @@ references: email: r.hijmans@gmail.com orcid: https://orcid.org/0000-0001-5872-2872 year: '2022' - url: https://CRAN.R-project.org/package=raster + url: https://rspatial.org/raster - type: software title: rmarkdown abstract: 'rmarkdown: Dynamic Documents for R' @@ -314,7 +306,7 @@ references: given-names: Michael email: mdsumner@gmail.com year: '2022' - url: https://CRAN.R-project.org/package=slippymath + url: https://www.github.com/milesmcbain/slippymath - type: software title: testthat abstract: 'testthat: Unit Testing for R' @@ -324,5 +316,4 @@ references: given-names: Hadley email: hadley@rstudio.com year: '2022' - url: https://CRAN.R-project.org/package=testthat version: '>= 3.0.0' diff --git a/codemeta.json b/codemeta.json index cb23503..23ffa8f 100644 --- a/codemeta.json +++ b/codemeta.json @@ -276,7 +276,7 @@ }, "SystemRequirements": null }, - "fileSize": "677.429KB", + "fileSize": "1284.566KB", "citation": [ { "@type": "SoftwareSourceCode", diff --git a/data-raw/.gitignore b/data-raw/.gitignore new file mode 100644 index 0000000..e934adf --- /dev/null +++ b/data-raw/.gitignore @@ -0,0 +1 @@ +cache/ diff --git a/data/catr_srs_values.rda b/data/catr_srs_values.rda index 7dcf487..210c12a 100644 Binary files a/data/catr_srs_values.rda and b/data/catr_srs_values.rda differ diff --git a/inst/WORDLIST b/inst/WORDLIST index 1af0d10..4fc45d0 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -1,3 +1,4 @@ +Bernabéu CIUDAD CMD CPMRC @@ -35,9 +36,11 @@ Polígono RCCOOR RGBA Renviron +Royé SALAMANCA SRS Sede +SpatRaster TIRADERO VH WFS @@ -45,6 +48,7 @@ WGS WMS asmx cadastral +cadastre catastro catr cmun @@ -56,12 +60,18 @@ del dir doi dt +ec electrónica +eu +europa geo geocode geocoded +geocoding +georeferenced geotagged github +growthurban http https huso @@ -71,6 +81,7 @@ ldt loine lon lourb +organisations ovc ovcservweb pc @@ -80,6 +91,7 @@ rOS refcat ropenspain srs +terra tibble wfs xcen diff --git a/vignettes/CatastRo.Rmd b/vignettes/CatastRo.Rmd new file mode 100644 index 0000000..a54b7e7 --- /dev/null +++ b/vignettes/CatastRo.Rmd @@ -0,0 +1,232 @@ +--- +title: "Get started" +output: rmarkdown::html_vignette +biblography: REFERENCES.bib +nocite: '@*' +vignette: > + %\VignetteIndexEntry{Get started} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +bibliography: references.bib +--- + + + +**CatastRo** provide access to different API services of the [Spanish +Cadastre](https://www.sedecatastro.gob.es/). With **CatastRo** it is possible to +download official information of addresses, properties, parcels and buildings. + +## OVCCoordenadas Service + +The +[OVCCoordenadas](https://ovc.catastro.meh.es/ovcservweb/OVCSWLocalizacionRC/OVCCoordenadas.asmx) +service allows to retrieve the coordinates of a known cadastral reference +(geocoding). It is also possible to retrieve the cadastral references around a +specific pair of coordinates (reverse geocoding). **CatastRo** returns the +results on a tibble format (see `vignette("tibble", package = "tibble")`). This +functionality is described in detail in the corresponding vignette (see +`vignette("ovcservice")`). + +## INSPIRE Services + +> The INSPIRE Directive aims to create a European Union spatial data +> infrastructure for the purposes of EU environmental policies and policies or +> activities which may have an impact on the environment. This European Spatial +> Data Infrastructure will enable the sharing of environmental spatial +> information among public sector organisations, facilitate public access to +> spatial information across Europe and assist in policy-making across +> boundaries. +> +> *From * + +The implementation of the INSPIRE directive on the Spanish Cadastre (see +[Catastro INSPIRE](https://www.catastro.minhap.es/webinspire/index.html)) allows +to retrieve spatial objects from the database of the cadastre: + +- **Vector objects:** Parcels, addresses, buildings, cadastral zones and more. + These objects are provided by **CatastRo** as *sf* objects (see `?sf)`. + +- **Imagery:** Image layers representing the same information than the vector + objects. These objects are provided by **CatastRo** as *SpatRaster* objects + (see `?terra)`. + +Note that **the coverage of this service is 95% of the Spanish territory**, +excluding Basque Country and Navarre that have their own independent cadastral +offices. + +There are three types of functions, each one querying a different service: + +1. **ATOM service**: The ATOM service allows to batch-download vector objects + of different cadastral elements for a specific municipality. + +2. **WFS service**: The WFS service allows to download vector objects of + specific cadastral elements.Note that there are some *restrictions on the + extension and number of elements* to query. For batch-downloading the ATOM + service is preferred. + +3. **WMS service**: This service allows to download georeferenced images of + different cadastral elements. + +## Examples + +### Working with layers + +On this example we would demonstrate some of the main capabilities of the +package by recreating a cadastral map of the surroundings of the [Santiago +Bernabéu Stadium](https://en.wikipedia.org/wiki/Santiago_Bernab%C3%A9u_Stadium). +We would make use of the **WMS and WFS services** to get different layers in +order to show some of the capabilities of the package: + + +```r + +# Extract building by bounding box +# Check https://boundingbox.klokantech.com/ + +library(CatastRo) + +stadium <- catr_wfs_bu_bbox( + c(-3.6891446916, 40.4523311971, -3.687462138, 40.4538643165), + srs = 4326 +) +#> Error in catr_hlp_dwnload(api_entry, filename, cache_dir = tempdir(), : +#> Execution halted + +# Now extract cadastral parcels. We can use spatial objects on the query + +stadium_parcel <- catr_wfs_cp_bbox(stadium) +#> Error in wfs_bbox(bbox, srs): object 'stadium' not found + +# Extract imagery: Labels of the parcel + +labs <- catr_wms_layer(stadium_parcel, + what = "parcel" +) +#> Error in get_sf_from_bbox(bbox, srs): object 'stadium_parcel' not found + +# Plot +library(ggplot2) +library(mapSpain) # For terra tiles + +ggplot() + + layer_spatraster(labs) + + geom_sf(data = stadium_parcel, fill = NA, col = "red", size = 2) + + geom_sf(data = stadium, fill = "red", alpha = .5) +#> Error in layer_spatraster(labs): data object is not SpatRaster. See ?terra::rast() +``` + +### Thematic maps + +We can create also thematic maps using the information available on the spatial +objects. We would produce a visualization of the urban growth of Valencia using +**CatastRo**, replicating the map produced by Dominic Royé on his post +[Visualize urban +growthurban-growth/](https://dominicroye.github.io/en/2019/visualize-urban-growth/) +[@roye_2019], using the **ATOM service**. + +In first place, we batch-download the buildings of the municipality: + + +```r +valencia_bu <- catr_atom_bu("Valencia", + to = "Valencia" # We use "to" parameter to narrow the search +) +``` + +We used here the to parameter to narrow the search to the province of Valencia. +In case of doubt, the function `catr_atom_bu_db_all()` provides the names and +urls recognized by the Catastro API. + +Next step for creating the visualization is to limit the analysis to a circle of +radius 2.5 km around the city center: + + +```r +library(dplyr) +library(sf) + +# Coordinates of Ayuntamiento de Valencia + +buff <- st_point(c(-0.3770936916069698, 39.47032401425384)) %>% + st_sfc(crs = 4326) %>% + # Adjust CRS to 25830: (Buildings) + st_transform(st_crs(valencia_bu)) %>% + # Buffer + st_buffer(2500) + + +# Cut buildings + +dataviz <- st_intersection(valencia_bu, buff) + +ggplot(dataviz) + + geom_sf() +``` + +plot of chunk minimal + +The last step to recreate the data visualization is to manipulate the +construction year, available in the column `beginning`: + + +```r + +# Extract 4 initial positions +year <- substr(dataviz$beginning, 1, 4) + +# Replace all that doesn't look as a number with 0000 +year[!(year %in% 0:2500)] <- "0000" + +# To numeric +year <- as.integer(year) + +# New column +dataviz <- dataviz %>% + mutate(year = year) +``` + +Last step is to create groups based on the year and create the data +visualization. We use here the function `ggplot2::cut_number()` to create 15 +different classes: + + +```r + +dataviz <- dataviz %>% + mutate(year_cat = ggplot2::cut_number(year, + n = 15 + )) + + +ggplot(dataviz) + + geom_sf(aes(fill = year_cat), color = NA) + + scale_fill_manual(values = hcl.colors(15, "Spectral")) + + theme_void() + + labs(title = "VALÈNCIA", 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) + ) +``` + +plot of chunk dataviz + + + + +## References diff --git a/vignettes/CatastRo.Rmd.orig b/vignettes/CatastRo.Rmd.orig new file mode 100644 index 0000000..a523362 --- /dev/null +++ b/vignettes/CatastRo.Rmd.orig @@ -0,0 +1,239 @@ +--- +title: "Get started" +output: rmarkdown::html_vignette +biblography: REFERENCES.bib +nocite: '@*' +vignette: > + %\VignetteIndexEntry{Get started} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +bibliography: references.bib +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + fig.path = "./", + comment = "#>", + tidy = "styler", + warning = FALSE, + message = FALSE, + dev = "ragg_png", + dpi = 90, + out.width = "100%" +) + +# Cheat a bit to speed up process +oldpath <- CatastRo:::catr_hlp_detect_cache_dir() + +CatastRo::catr_set_cache_dir("../data-raw/cache") + +``` + +**CatastRo** provide access to different API services of the [Spanish +Cadastre](https://www.sedecatastro.gob.es/). With **CatastRo** it is possible to +download official information of addresses, properties, parcels and buildings. + +## OVCCoordenadas Service + +The +[OVCCoordenadas](https://ovc.catastro.meh.es/ovcservweb/OVCSWLocalizacionRC/OVCCoordenadas.asmx) +service allows to retrieve the coordinates of a known cadastral reference +(geocoding). It is also possible to retrieve the cadastral references around a +specific pair of coordinates (reverse geocoding). **CatastRo** returns the +results on a tibble format (see `vignette("tibble", package = "tibble")`). This +functionality is described in detail in the corresponding vignette (see +`vignette("ovcservice")`). + +## INSPIRE Services + +> The INSPIRE Directive aims to create a European Union spatial data +> infrastructure for the purposes of EU environmental policies and policies or +> activities which may have an impact on the environment. This European Spatial +> Data Infrastructure will enable the sharing of environmental spatial +> information among public sector organisations, facilitate public access to +> spatial information across Europe and assist in policy-making across +> boundaries. +> +> *From * + +The implementation of the INSPIRE directive on the Spanish Cadastre (see +[Catastro INSPIRE](https://www.catastro.minhap.es/webinspire/index.html)) allows +to retrieve spatial objects from the database of the cadastre: + +- **Vector objects:** Parcels, addresses, buildings, cadastral zones and more. + These objects are provided by **CatastRo** as *sf* objects (see `?sf)`. + +- **Imagery:** Image layers representing the same information than the vector + objects. These objects are provided by **CatastRo** as *SpatRaster* objects + (see `?terra)`. + +Note that **the coverage of this service is 95% of the Spanish territory**, +excluding Basque Country and Navarre that have their own independent cadastral +offices. + +There are three types of functions, each one querying a different service: + +1. **ATOM service**: The ATOM service allows to batch-download vector objects + of different cadastral elements for a specific municipality. + +2. **WFS service**: The WFS service allows to download vector objects of + specific cadastral elements.Note that there are some *restrictions on the + extension and number of elements* to query. For batch-downloading the ATOM + service is preferred. + +3. **WMS service**: This service allows to download georeferenced images of + different cadastral elements. + +## Examples + +### Working with layers + +On this example we would demonstrate some of the main capabilities of the +package by recreating a cadastral map of the surroundings of the [Santiago +Bernabéu Stadium](https://en.wikipedia.org/wiki/Santiago_Bernab%C3%A9u_Stadium). +We would make use of the **WMS and WFS services** to get different layers in +order to show some of the capabilities of the package: + +```{r santbernabeu} + +# Extract building by bounding box +# Check https://boundingbox.klokantech.com/ + +library(CatastRo) + +stadium <- catr_wfs_bu_bbox( + c(-3.6891446916, 40.4523311971, -3.687462138, 40.4538643165), + srs = 4326 +) + +# Now extract cadastral parcels. We can use spatial objects on the query + +stadium_parcel <- catr_wfs_cp_bbox(stadium) + +# Extract imagery: Labels of the parcel + +labs <- catr_wms_layer(stadium_parcel, + what = "parcel" +) + +# Plot +library(ggplot2) +library(mapSpain) # For terra tiles + +ggplot() + + layer_spatraster(labs) + + geom_sf(data = stadium_parcel, fill = NA, col = "red", size = 2) + + geom_sf(data = stadium, fill = "red", alpha = .5) +``` + +### Thematic maps + +We can create also thematic maps using the information available on the spatial +objects. We would produce a visualization of the urban growth of Valencia using +**CatastRo**, replicating the map produced by Dominic Royé on his post +[Visualize urban +growthurban-growth/](https://dominicroye.github.io/en/2019/visualize-urban-growth/) +[@roye_2019], using the **ATOM service**. + +In first place, we batch-download the buildings of the municipality: + +```{r} +valencia_bu <- catr_atom_bu("Valencia", + to = "Valencia" # We use "to" parameter to narrow the search +) +``` + +We used here the to parameter to narrow the search to the province of Valencia. +In case of doubt, the function `catr_atom_bu_db_all()` provides the names and +urls recognized by the Catastro API. + +Next step for creating the visualization is to limit the analysis to a circle of +radius 2.5 km around the city center: + +```{r minimal} +library(dplyr) +library(sf) + +# Coordinates of Ayuntamiento de Valencia + +buff <- st_point(c(-0.3770936916069698, 39.47032401425384)) %>% + st_sfc(crs = 4326) %>% + # Adjust CRS to 25830: (Buildings) + st_transform(st_crs(valencia_bu)) %>% + # Buffer + st_buffer(2500) + + +# Cut buildings + +dataviz <- st_intersection(valencia_bu, buff) + +ggplot(dataviz) + + geom_sf() +``` + +The last step to recreate the data visualization is to manipulate the +construction year, available in the column `beginning`: + +```{r} + +# Extract 4 initial positions +year <- substr(dataviz$beginning, 1, 4) + +# Replace all that doesn't look as a number with 0000 +year[!(year %in% 0:2500)] <- "0000" + +# To numeric +year <- as.integer(year) + +# New column +dataviz <- dataviz %>% + mutate(year = year) +``` + +Last step is to create groups based on the year and create the data +visualization. We use here the function `ggplot2::cut_number()` to create 15 +different classes: + +```{r dataviz} + +dataviz <- dataviz %>% + mutate(year_cat = ggplot2::cut_number(year, + n = 15 + )) + + +ggplot(dataviz) + + geom_sf(aes(fill = year_cat), color = NA) + + scale_fill_manual(values = hcl.colors(15, "Spectral")) + + theme_void() + + labs(title = "VALÈNCIA", 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) + ) +``` + + +```{r, include = FALSE} + +# Restore +CatastRo::catr_set_cache_dir(oldpath) + +``` + +## References diff --git a/vignettes/REFERENCES.bib b/vignettes/REFERENCES.bib new file mode 100644 index 0000000..86b8e4f --- /dev/null +++ b/vignettes/REFERENCES.bib @@ -0,0 +1,8 @@ +@misc{roye_2019, + title = {Visualize urban growth}, + author = {Royé, Dominic}, + year = 2019, + month = {nov}, + journal = {Dr. Dominic Royé}, + url = {https://dominicroye.github.io/en/2019/visualize-urban-growth/} +} diff --git a/vignettes/dataviz-1.png b/vignettes/dataviz-1.png new file mode 100644 index 0000000..2830472 Binary files /dev/null and b/vignettes/dataviz-1.png differ diff --git a/vignettes/minimal-1.png b/vignettes/minimal-1.png new file mode 100644 index 0000000..907bd38 Binary files /dev/null and b/vignettes/minimal-1.png differ