-
Notifications
You must be signed in to change notification settings - Fork 8
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
Geocodificado recursivo #5
base: master
Are you sure you want to change the base?
Changes from all commits
05efa31
d7f85b9
f0a865c
845f2dd
dc3c1b6
7a542ce
1177131
f70df78
5663efa
2456dc9
88685a5
07e81e6
d20db8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
Package: caRtociudad | ||
Type: Package | ||
Title: Interface to Cartociudad API | ||
Version: 0.5.2 | ||
Version: 0.5.5 | ||
Date: 2017-07-26 | ||
Encoding: UTF-8 | ||
Authors@R: c(person("Carlos J.", "Gil Bellosta", email="[email protected]", role=c('cre', 'aut')), | ||
person("Luz", "Frías", email = "[email protected]", role = "aut")) | ||
Author: Carlos J. Gil Bellosta, Luz Frías | ||
Maintainer: Carlos J. Gil Bellosta <[email protected]> | ||
Description: Access to Cartociudad cartography API, which provides mapping and other related services for Spain. | ||
Imports: httr, jsonlite, xml2, plyr, geosphere | ||
Imports: httr, jsonlite, xml2, plyr, geosphere, utils | ||
Depends: R (>= 3.0.0) | ||
Suggests: ggmap, testthat | ||
URL: https://github.com/cjgb/caRtociudad | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,15 @@ | |
#' @details This function performs reverse geocoding of a location. It returns | ||
#' the details of the closest address in Spain. | ||
#' | ||
#' @usage cartociudad_reverse_geocode(latitude, longitude) | ||
#' @usage cartociudad_reverse_geocode(latitude, longitude, ntries = 10) | ||
#' | ||
#' @param latitude Point latitude in geographical coordinates (e.g., 40.473219) | ||
#' @param longitude Point longitude in geographical coordinates (e.g., | ||
#' -3.7227241) | ||
#' @param ntries Numeric. In case of connection failure, number of \code{GET} | ||
#' requests to be made before stopping the function call. | ||
#' | ||
#' @return A list with the following items: | ||
#' @return A data frame consisting of a single row per query, with columns: | ||
#' \item{tipo}{type of location.} | ||
#' \item{tipo.via}{road type.} | ||
#' \item{nombre.via}{road name.} | ||
|
@@ -32,34 +34,53 @@ | |
#' \url{http://www.cartociudad.es/recursos/Documentacion_tecnica/CARTOCIUDAD_ServiciosWeb.pdf} | ||
#' | ||
#' @examples | ||
#' # Query one point | ||
#' cartociudad_reverse_geocode(40.473219, -3.7227241) | ||
#' | ||
#' # Query multiple points | ||
#' cartociudad_reverse_geocode(c(40.473219, 39.46979), c(-3.7227241, -0.376963)) | ||
#' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Como recomendación, estaría bien añadir un test en There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yo me ocupo de las pruebas. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfecto! |
||
#' @export | ||
#' | ||
cartociudad_reverse_geocode <- function(latitude, longitude) { | ||
|
||
query.parms <- list( | ||
lat = latitude, | ||
lon = longitude | ||
) | ||
|
||
url <- "http://www.cartociudad.es/services/api/geocoder/reverseGeocode" | ||
ua <- get_cartociudad_user_agent() | ||
|
||
|
||
res <- httr::GET(url, query = query.parms, ua) | ||
httr::stop_for_status(res) | ||
info <- httr::content(res) | ||
# Parse the response | ||
res <- list( | ||
tipo = info$type, | ||
tipo.via = info$tip_via, | ||
nombre.via = info$address, | ||
num.via = info$portalNumber, | ||
num.via.id = info$id, | ||
municipio = info$muni, | ||
provincia = info$province, | ||
cod.postal = info$postalCode | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sobre la eliminación de esto, creo que conviene mantener en el paquete el mapeo de nombres, para:
Dime cómo lo ves. Si estás de acuerdo, sería bueno mantener el trozo de documentación eliminado sobre los nombres There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Me parece perfecto, aunque creo que sería mejor devolver un data.frame en lugar de una lista. ¿Conservamos esa parte? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, totalmente de acuerdo, tiene más sentido ahora que está vectorizado |
||
return(res) | ||
cartociudad_reverse_geocode <- function(latitude, longitude, ntries = 1) { | ||
|
||
stopifnot(length(latitude) == length(longitude) | length(latitude) == 0) | ||
|
||
res_list <- list() | ||
url <- "http://www.cartociudad.es/services/api/geocoder/reverseGeocode" | ||
ua <- get_cartociudad_user_agent() | ||
no_select <- c("geom", "poblacion", "stateMsg", "state", "priority", "countryCode") | ||
total <- length(latitude) | ||
pb <- utils::txtProgressBar(min = 0, max = total, style = 3) | ||
|
||
for (i in seq_len(total)) { | ||
query.parms <- list(lat = latitude[i], lon = longitude[i]) | ||
res <- get_ntries(url, query.parms, ua, ntries) | ||
if (httr::http_error(res)) { | ||
warning("Error in query ", i, ": ", httr::http_status(res)$message) | ||
res_list[[i]] <- data.frame(lat = latitude[i], lng = longitude[i], | ||
stringsAsFactors = FALSE) | ||
} else if (length(httr::content(res)) == 0) { | ||
warning("Query ", i, " produced 0 results.") | ||
res_list[[i]] <- data.frame(lat = latitude[i], lng = longitude[i], | ||
stringsAsFactors = FALSE) | ||
} else { | ||
info <- httr::content(res) | ||
info <- info[-which(names(info) %in% no_select)] | ||
res_list[[i]] <- as.data.frame(t(unlist(info)), stringsAsFactors = FALSE) | ||
} | ||
utils::setTxtProgressBar(pb, i) | ||
} | ||
|
||
cat("\n") | ||
results <- plyr::rbind.fill(res_list) | ||
names_old <- c("type", "tip_via", "address", "portalNumber", "id", | ||
"muni", "province", "postalCode", "lat", "lng") | ||
names_new <- c("tipo", "tipo.via", "nombre.via", "num.via", "num.via.id", | ||
"municipio", "provincia", "cod.postal", "lat", "lng") | ||
for (i in seq_len(ncol(results))) { | ||
colnames(results)[colnames(results) == names_old[i]] <- names_new[i] | ||
} | ||
|
||
return(results) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misma recomendación que con
cartociudad_reverse_geocode
: sugiero incluir un test para múltiples direcciones