Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add street names to addresses #39

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CatastRo.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source

MarkdownWrap: Column
MarkdownWrapAtColumn: 80
LineEndingConversion: Posix
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cph"),
comment = c(ORCID = "0000-0002-8189-9251")),
Expand Down
14 changes: 7 additions & 7 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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()`.
Expand All @@ -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
Expand Down
33 changes: 32 additions & 1 deletion R/atom_ad.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#'
#'
#' 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
#' Documentation](https://www.catastro.minhap.es/webinspire/documentos/inspire-ATOM.pdf)

Check warning on line 11 in R/atom_ad.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/atom_ad.R,line=11,col=81,[line_length_linter] Lines should not be more than 80 characters.
#'
#' [INSPIRE Services for Cadastral
#' Cartography](https://www.catastro.minhap.es/webinspire/index.html)
Expand Down Expand Up @@ -124,5 +126,34 @@

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)
}
63 changes: 26 additions & 37 deletions R/utils_read.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 0 additions & 3 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Cadastral
CadastralParcel
CadastralZoning
Cadastre
CatastRo’
Catastro
Consulta
ConsultaMunicipioCodigos
Expand All @@ -23,11 +22,9 @@ EL
EPSG
ESPAÑA
ETRS
Electrónica
Geocode
Geocoding
Geográficas
Hernangomez
INE
MUDELA
ORCID
Expand Down
4 changes: 3 additions & 1 deletion man/catr_atom_get_address.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("Test cache online", {
skip_on_os("windows")
# Get current cache dir
current <- catr_hlp_detect_cache_dir()

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading