From fe76dced0decd17997235a625a55c80f2d3d4cb4 Mon Sep 17 00:00:00 2001 From: Diego H Date: Mon, 25 Sep 2023 13:07:15 +0000 Subject: [PATCH] Add street names to addresses --- CatastRo.Rproj | 2 +- DESCRIPTION | 2 +- NEWS.md | 14 ++--- R/atom_ad.R | 33 +++++++++- R/utils_read.R | 63 ++++++++----------- inst/WORDLIST | 3 - man/catr_atom_get_address.Rd | 4 +- .../{test-catr_atom_ad.R => test-atom_ad.R} | 2 + ...st-catr_atom_ad_db.R => test-atom_ad_db.R} | 0 .../{test-catr_atom_bu.R => test-atom_bu.R} | 0 ...st-catr_atom_bu_db.R => test-atom_bu_db.R} | 0 .../{test-catr_atom_cp.R => test-atom_cp.R} | 0 ...st-catr_atom_cp_db.R => test-atom_cp_db.R} | 0 ...earch_munic.R => test-atom_search_munic.R} | 0 ...test-catr_cache_dir.R => test-cache_dir.R} | 1 + ...m_coords.R => test-get_code_from_coords.R} | 0 ...test-catr_ovc_cpmrc.R => test-ovc_cpmrc.R} | 0 ...st-catr_ovc_rccoor.R => test-ovc_rccoor.R} | 0 ...istancia.R => test-ovc_rccoor_distancia.R} | 0 ...atr_ovccallejero.R => test-ovccallejero.R} | 0 .../{test-catr_wfs_at.R => test-wfs_ad.R} | 0 .../{test-catr_wfs_bu.R => test-wfs_bu.R} | 0 .../{test-catr_wfs_cp.R => test-wfs_cp.R} | 0 .../{test-catr_wms.R => test-wms_all.R} | 0 24 files changed, 73 insertions(+), 51 deletions(-) rename tests/testthat/{test-catr_atom_ad.R => test-atom_ad.R} (94%) rename tests/testthat/{test-catr_atom_ad_db.R => test-atom_ad_db.R} (100%) rename tests/testthat/{test-catr_atom_bu.R => test-atom_bu.R} (100%) rename tests/testthat/{test-catr_atom_bu_db.R => test-atom_bu_db.R} (100%) rename tests/testthat/{test-catr_atom_cp.R => test-atom_cp.R} (100%) rename tests/testthat/{test-catr_atom_cp_db.R => test-atom_cp_db.R} (100%) rename tests/testthat/{test-catr_atom_search_munic.R => test-atom_search_munic.R} (100%) rename tests/testthat/{test-catr_cache_dir.R => test-cache_dir.R} (98%) rename tests/testthat/{test-catr_get_code_from_coords.R => test-get_code_from_coords.R} (100%) rename tests/testthat/{test-catr_ovc_cpmrc.R => test-ovc_cpmrc.R} (100%) rename tests/testthat/{test-catr_ovc_rccoor.R => test-ovc_rccoor.R} (100%) rename tests/testthat/{test-catr_ovc_rccoor_distancia.R => test-ovc_rccoor_distancia.R} (100%) rename tests/testthat/{test-catr_ovccallejero.R => test-ovccallejero.R} (100%) rename tests/testthat/{test-catr_wfs_at.R => test-wfs_ad.R} (100%) rename tests/testthat/{test-catr_wfs_bu.R => test-wfs_bu.R} (100%) rename tests/testthat/{test-catr_wfs_cp.R => test-wfs_cp.R} (100%) rename tests/testthat/{test-catr_wms.R => test-wms_all.R} (100%) diff --git a/CatastRo.Rproj b/CatastRo.Rproj index 9137e9d..7e9b83f 100644 --- a/CatastRo.Rproj +++ b/CatastRo.Rproj @@ -14,6 +14,7 @@ LaTeX: pdfLaTeX AutoAppendNewline: Yes StripTrailingWhitespace: Yes +LineEndingConversion: Posix BuildType: Package PackageUseDevtools: Yes @@ -21,4 +22,3 @@ PackageInstallArgs: --no-multiarch --with-keep.source MarkdownWrap: Column MarkdownWrapAtColumn: 80 -LineEndingConversion: Posix diff --git a/DESCRIPTION b/DESCRIPTION index ef42598..fc95b6b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: CatastRo Title: Interface to the API 'Sede Electronica Del Catastro' -Version: 0.2.3 +Version: 0.2.3.9000 Authors@R: c( person("Ángel", "Delgado Panadero", , "delgadopanadero@gmail.com", role = c("aut", "cph"), comment = c(ORCID = "0000-0002-8189-9251")), diff --git a/NEWS.md b/NEWS.md index 0a24655..303de78 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# CatastRo (development version) + +- `catr_atom_get_address()` returns also the names of the streets (layer + `"ThoroughfareName"` of the `*.gml file`). The new fields are named with the + prefix `tfname_*`. +- Update documentation and tests. + # CatastRo 0.2.3 - Housekeeping and update of documentation. @@ -18,15 +25,12 @@ **Overall revamp of the package. Major changes on the API.** - Add **ATOM INSPIRE** capabilities: - - Addresses: `catr_atom_get_address()`, `catr_atom_get_address_db_all()`. - Cadastral Parcels: `catr_atom_get_parcels()`, `catr_atom_get_parcels_db_all()`. - Buildings: `catr_atom_get_buildings()`, `catr_atom_get_buildings_db_all()`. - - Add **WFS INSPIRE** capabilities: - - Addresses: `catr_wfs_get_address_bbox()`, `catr_wfs_get_address_codvia()`, `catr_wfs_get_address_postalcode()`, `catr_wfs_get_address_rc()`. @@ -35,17 +39,13 @@ `catr_wfs_get_parcels_zoning()`. - Buildings: `catr_wfs_get_buildings_bbox()`,`catr_wfs_get_buildings_rc()`. - - Add **WMS INSPIRE** capabilities: `catr_wms_get_layer()`. - - New interface for **OVC Services**. Deprecate previous functions in favor of the new API: - - New SRS database on `catr_srs_values`, replaces `coordinates`. - `catr_ovc_get_rccoor_distancia()` replaces `near_rc()`. - `catr_ovc_get_rccoor()` replaces `get_rc()`. - `catr_ovc_get_cpmrc()` replaces `get_coor()`. - - Add precomputed vignettes. # CatastRo 0.1.0 diff --git a/R/atom_ad.R b/R/atom_ad.R index 1b24291..24fafb9 100644 --- a/R/atom_ad.R +++ b/R/atom_ad.R @@ -2,7 +2,9 @@ #' #' #' Get the spatial data of all the addresses belonging to a single -#' municipality using the INSPIRE ATOM service. +#' municipality using the INSPIRE ATOM service. Additionally, the function also +#' returns the corresponding street information on the fields with the +#' prefix `tfname_*`. #' #' @references #' [API @@ -124,5 +126,34 @@ catr_atom_get_address <- function(munic, sfobj <- st_read_layers_encoding(files, verbose) + # See if we can add street names + whatlay <- sf::st_layers(files) + if ("ThoroughfareName" %in% whatlay$name) { + if (verbose) message("Adding ThoroughfareName to Address") + + str_names <- st_read_layers_encoding(files, + verbose = FALSE, + layer = "ThoroughfareName" + ) + + # Rename and prepare for left join + names(str_names) <- paste0("tfname_", names(str_names)) + + sfobj$gml_id + + sfobj$tfname_gml_id <- vapply( + sfobj$gml_id, + FUN = function(x) { + ids <- paste0(unlist(strsplit(x, ".", fixed = TRUE))[seq(1, 6)], + collapse = "." + ) + ids <- gsub("AD", "TN", ids) + ids + }, FUN.VALUE = character(1), USE.NAMES = FALSE + ) + + sfobj <- dplyr::left_join(sfobj, str_names, by = "tfname_gml_id") + } + return(sfobj) } diff --git a/R/utils_read.R b/R/utils_read.R index 9f452c7..5575b5e 100644 --- a/R/utils_read.R +++ b/R/utils_read.R @@ -1,51 +1,40 @@ -st_read_layers_encoding <- function(path, verbose) { - # Layer management and errors - layers <- tryCatch( - sf::st_layers(path), - warning = function(e) { - return(NULL) - }, - error = function(e) { - return(NULL) - } - ) - - # If NULL change to a new tempfile and retry - # This may be an error on encoding - if (is.null(layers)) { - newlines <- readLines(path, encoding = "ISO-8859-1") +st_read_layers_encoding <- function(path, verbose, layer = NULL) { + newlines <- readLines(path, encoding = "ISO-8859-1") - # Thanks @santiagomota #19 - newlines <- stringi::stri_trans_general(newlines, "latin-ascii") - path <- tempfile(fileext = ".gml") - writeLines(newlines, path) + # Thanks @santiagomota #19 + newlines <- stringi::stri_trans_general(newlines, "latin-ascii") + path <- tempfile(fileext = ".gml") + writeLines(newlines, path) + # If not provided then infer by name + if (is.null(layer)) { layers <- sf::st_layers(path) - } - df_layers <- tibble::tibble( - layer = layers$name, - geomtype = unlist(layers$geomtype) - ) + df_layers <- tibble::tibble( + layer = layers$name, + geomtype = unlist(layers$geomtype) + ) - if (any(nrow(df_layers) == 0, !"geomtype" %in% names(df_layers))) { - message("No spatial layers found.") - return(invisible(NULL)) - } + if (any(nrow(df_layers) == 0, !"geomtype" %in% names(df_layers))) { + message("No spatial layers found.") + return(invisible(NULL)) + } - df_layers <- df_layers[!is.na(df_layers$geomtype), ] + df_layers <- df_layers[!is.na(df_layers$geomtype), ] - # nocov start - if (nrow(df_layers) == 0) { - message("No spatial layers found.") - return(invisible(NULL)) - } - # nocov end + # nocov start + if (nrow(df_layers) == 0) { + message("No spatial layers found.") + return(invisible(NULL)) + } + # nocov end + layer <- as.character(df_layers$layer[1]) + } out <- try( sf::st_read(path, - layer = df_layers$layer[1], + layer = layer, quiet = !verbose ), silent = TRUE diff --git a/inst/WORDLIST b/inst/WORDLIST index 9ffda6a..46d5fcf 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -10,7 +10,6 @@ Cadastral CadastralParcel CadastralZoning Cadastre -CatastRo’ Catastro Consulta ConsultaMunicipioCodigos @@ -23,11 +22,9 @@ EL EPSG ESPAÑA ETRS -Electrónica Geocode Geocoding Geográficas -Hernangomez INE MUDELA ORCID diff --git a/man/catr_atom_get_address.Rd b/man/catr_atom_get_address.Rd index bd5408f..4a3c609 100644 --- a/man/catr_atom_get_address.Rd +++ b/man/catr_atom_get_address.Rd @@ -38,7 +38,9 @@ A \code{sf} object. } \description{ Get the spatial data of all the addresses belonging to a single -municipality using the INSPIRE ATOM service. +municipality using the INSPIRE ATOM service. Additionally, the function also +returns the corresponding street information on the fields with the +prefix \verb{tfname_*}. } \examples{ \donttest{ diff --git a/tests/testthat/test-catr_atom_ad.R b/tests/testthat/test-atom_ad.R similarity index 94% rename from tests/testthat/test-catr_atom_ad.R rename to tests/testthat/test-atom_ad.R index 911af02..6b3ea45 100644 --- a/tests/testthat/test-catr_atom_ad.R +++ b/tests/testthat/test-atom_ad.R @@ -28,6 +28,8 @@ test_that("ATOM Encoding issue", { s <- catr_atom_get_address("12028") expect_s3_class(s, "sf") + expect_true("tfname_text" %in% names(s)) + expect_silent(catr_atom_get_address("23078")) expect_silent(catr_atom_get_address("03050")) expect_silent(catr_atom_get_address("23051")) diff --git a/tests/testthat/test-catr_atom_ad_db.R b/tests/testthat/test-atom_ad_db.R similarity index 100% rename from tests/testthat/test-catr_atom_ad_db.R rename to tests/testthat/test-atom_ad_db.R diff --git a/tests/testthat/test-catr_atom_bu.R b/tests/testthat/test-atom_bu.R similarity index 100% rename from tests/testthat/test-catr_atom_bu.R rename to tests/testthat/test-atom_bu.R diff --git a/tests/testthat/test-catr_atom_bu_db.R b/tests/testthat/test-atom_bu_db.R similarity index 100% rename from tests/testthat/test-catr_atom_bu_db.R rename to tests/testthat/test-atom_bu_db.R diff --git a/tests/testthat/test-catr_atom_cp.R b/tests/testthat/test-atom_cp.R similarity index 100% rename from tests/testthat/test-catr_atom_cp.R rename to tests/testthat/test-atom_cp.R diff --git a/tests/testthat/test-catr_atom_cp_db.R b/tests/testthat/test-atom_cp_db.R similarity index 100% rename from tests/testthat/test-catr_atom_cp_db.R rename to tests/testthat/test-atom_cp_db.R diff --git a/tests/testthat/test-catr_atom_search_munic.R b/tests/testthat/test-atom_search_munic.R similarity index 100% rename from tests/testthat/test-catr_atom_search_munic.R rename to tests/testthat/test-atom_search_munic.R diff --git a/tests/testthat/test-catr_cache_dir.R b/tests/testthat/test-cache_dir.R similarity index 98% rename from tests/testthat/test-catr_cache_dir.R rename to tests/testthat/test-cache_dir.R index 8b40079..a61e36f 100644 --- a/tests/testthat/test-catr_cache_dir.R +++ b/tests/testthat/test-cache_dir.R @@ -1,4 +1,5 @@ test_that("Test cache online", { + skip_on_os("windows") # Get current cache dir current <- catr_hlp_detect_cache_dir() diff --git a/tests/testthat/test-catr_get_code_from_coords.R b/tests/testthat/test-get_code_from_coords.R similarity index 100% rename from tests/testthat/test-catr_get_code_from_coords.R rename to tests/testthat/test-get_code_from_coords.R diff --git a/tests/testthat/test-catr_ovc_cpmrc.R b/tests/testthat/test-ovc_cpmrc.R similarity index 100% rename from tests/testthat/test-catr_ovc_cpmrc.R rename to tests/testthat/test-ovc_cpmrc.R diff --git a/tests/testthat/test-catr_ovc_rccoor.R b/tests/testthat/test-ovc_rccoor.R similarity index 100% rename from tests/testthat/test-catr_ovc_rccoor.R rename to tests/testthat/test-ovc_rccoor.R diff --git a/tests/testthat/test-catr_ovc_rccoor_distancia.R b/tests/testthat/test-ovc_rccoor_distancia.R similarity index 100% rename from tests/testthat/test-catr_ovc_rccoor_distancia.R rename to tests/testthat/test-ovc_rccoor_distancia.R diff --git a/tests/testthat/test-catr_ovccallejero.R b/tests/testthat/test-ovccallejero.R similarity index 100% rename from tests/testthat/test-catr_ovccallejero.R rename to tests/testthat/test-ovccallejero.R diff --git a/tests/testthat/test-catr_wfs_at.R b/tests/testthat/test-wfs_ad.R similarity index 100% rename from tests/testthat/test-catr_wfs_at.R rename to tests/testthat/test-wfs_ad.R diff --git a/tests/testthat/test-catr_wfs_bu.R b/tests/testthat/test-wfs_bu.R similarity index 100% rename from tests/testthat/test-catr_wfs_bu.R rename to tests/testthat/test-wfs_bu.R diff --git a/tests/testthat/test-catr_wfs_cp.R b/tests/testthat/test-wfs_cp.R similarity index 100% rename from tests/testthat/test-catr_wfs_cp.R rename to tests/testthat/test-wfs_cp.R diff --git a/tests/testthat/test-catr_wms.R b/tests/testthat/test-wms_all.R similarity index 100% rename from tests/testthat/test-catr_wms.R rename to tests/testthat/test-wms_all.R