-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subo nuevas funciones para barrios vulnerables
- Loading branch information
1 parent
4f0b621
commit a88af62
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#' USIG_geocode_barrios_vulnerables | ||
#' | ||
#' Geocoder para manzanas en barrios vulnerables basado en el servicio de USIG GCABA (https://epok.buenosaires.gob.ar/deficithabitacional/) | ||
#' @import RCurl | ||
#' @import rjson | ||
#' @import httr | ||
#' @importFrom utils URLencode | ||
#' @export USIG_geocode_barrios_vulnerables | ||
#' @param address Una dirección con la manzana de un barrio vulnerable dentro de los límites de la Ciudad Autónoma de Buenos Aires | ||
#' @examples | ||
#' USIG_geocode_barrios_vulnerables("Villa 31, manzana 8") | ||
#' | ||
#'# Resultado: | ||
#'# | ||
#'# barrio_vuln lon lat | ||
#'# Barrio 15 (Ciudad Oculta) -58.49024 -34.66918 | ||
#'# | ||
#'# | ||
#'# | ||
#'# | ||
#' | ||
#'# Se pueden georeferenciar varias direcciones a la vez: | ||
#' | ||
#' USIG_geocode_barrios_vulnerables(c("Villa 31, manzana 2", "Rodrigo Bueno, manzana 2", "Villa 1-11-14, manzana 5")) | ||
#' | ||
#'# Resultado: | ||
#'# | ||
#'# barrio_vulnerable lon lat | ||
#'# Barrio Padre Carlos Mugica (Villa 31 bis) -58.37897 -34.58448 | ||
#'# Asentamiento Rodrigo Bueno -58.35382 -34.61905 | ||
# '# Barrio Padre Ricciardelli (ex 1-11-14) -58.43477 -34.64977 | ||
|
||
|
||
USIG_geocode_barrios_vulnerables <- function(address) { | ||
|
||
make_call <- function(address) { | ||
|
||
#stopifnot(is.character(address)) | ||
|
||
base_url <- "https://epok.buenosaires.gob.ar/deficithabitacional/buscarManzana/" | ||
|
||
make_address_query <- function(address) { | ||
query <- paste0(base_url, | ||
"?ubicacion=", | ||
address) | ||
} | ||
|
||
query <- URLencode(make_address_query(address)) | ||
|
||
paste("Trying", query) | ||
|
||
results <- tryCatch(fromJSON(getURL(query)), | ||
error = function(error_message) {return(NULL)}) | ||
|
||
|
||
|
||
if (httr:::is.response(results)) { | ||
data.frame(address = address, barrio_vulnerable = NA, | ||
lon = NA, lat = NA, stringsAsFactors = FALSE) | ||
} else { | ||
results <- results | ||
if (length(results$instancias)) { | ||
x <- as.numeric(gsub(".*?([-]*[0-9]+[.][0-9]+).*", "\\1", results$instancias[[1]]$ubicacion$centroide)) | ||
y <- as.numeric(gsub(".* ([-]*[0-9]+[.][0-9]+).*", "\\1", results$instancias[[1]]$ubicacion$centroide)) | ||
barrio_vulnerable <- results$instancias[[1]]$contenido[[2]]$valor | ||
} else { | ||
x <- NA | ||
y <- NA | ||
barrio_vulnerable <- NA | ||
} | ||
|
||
|
||
data.frame(direccion = address, | ||
barrio_vulnerable = ifelse(is.null(barrio_vulnerable), NA, barrio_vulnerable), | ||
lon = ifelse(is.null(x), NA, as.numeric(x)), | ||
lat = ifelse(is.null(y), NA, as.numeric(y)), | ||
stringsAsFactors = FALSE) | ||
} | ||
} | ||
|
||
Reduce(rbind, lapply(address, make_call)) | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#' mutate_USIG_geocode_barrios_vulnerables | ||
#' | ||
#' Geocoder basado en el servicio de USIG GCABA (http://usig.buenosaires.gob.ar/) | ||
#' @export mutate_USIG_geocode_barrios_vulnerables | ||
#' @param data Un dataframe con una columna que contiene una dirección con la manzana de un barrio vulnerable dentro de los límites de la Ciudad Autónoma de Buenos Aires | ||
#' @param address Nombre de la columna que contiene las direcciones | ||
#' @examples | ||
#'# Con un dataframe imput: | ||
#'# lugar valor direccion | ||
#'# A 225000 Villa 31, manzana 2 | ||
#'# B 130500 Rodrigo Bueno, manzana 2 | ||
#'# C 34000 Villa 1-11-14, manzana 5 | ||
#' | ||
#' | ||
#'datos <- data.frame(lugar = c("A", "B", "C"), | ||
#' valor = c(225000, 130500, 34000), | ||
#' direccion = c("Villa 31, manzana 2", | ||
#' "Rodrigo Bueno, manzana 2", | ||
#' "Villa 1-11-14, manzana 5")) | ||
#' | ||
#' | ||
#' | ||
#' | ||
#'mutate_USIG_geocode_barrios_vulnerables(datos, "direccion") | ||
#' | ||
#'# Resultado: | ||
#' | ||
#'# lugar valor direccion address_normalised lon lat | ||
#'# A 225000 Villa 31, manzana 2 Barrio Padre Carlos Mugica (Villa 31 bis) -58.37897 -34.58448 | ||
#'# B 130500 Rodrigo Bueno, manzana 2 Asentamiento Rodrigo Bueno -58.35382 -34.61905 | ||
#'# C 34000 Villa 1-11-14, manzana 5 Barrio Padre Ricciardelli (ex 1-11-14) -58.43477 -34.64977 | ||
|
||
|
||
mutate_USIG_geocode_barrios_vulnerables <- function(data, address) { | ||
addresses <- data[[address]] | ||
results <- USIG_geocode_barrios_vulnerables(addresses) | ||
cbind(data, results) | ||
} |