Skip to content

Commit

Permalink
Adding mutate_USIG_geocode
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandbricks committed Jul 4, 2019
1 parent 67175e1 commit f42cfb3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 40 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Title: A Set of tools for the analysis of the RUMBA (Región Urbana Metropolitan
Version: 0.1.0
Author: H. Antonio Vazquez Brust
Maintainer: H. Antonio Vazquez Brust <avazquez@gmail.com>
Description: This package provides functions for easy access to geocoding (reverse and direct) and address normalisation for places within the RUMBA.
Description: This package provides functions for easy access to geocoding and address normalisation for places within the RUMBA.
Coming soon: shapefiles and Census data
Imports: httr
License: GPL
Encoding: UTF-8
LazyData: true
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Generated by roxygen2: do not edit by hand

import(futile.logger)
import(httr)
84 changes: 50 additions & 34 deletions R/USIG_geocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#' Geocoder basado en el servicio de USIG GCABA (http://usig.buenosaires.gob.ar/)
#' uso: USIG_geocode(direccion)
#'
#' @import futile.logger
#' @import httr
#' @param direccion Una dirección dentro de los límites de la Región Urbana Metropolitana de Buenos Aires
#' @examples
Expand All @@ -12,58 +11,75 @@
#'
#' resultado
#'
#' address address_normalised lat lng
#' 9 de julio y belgrano 9 DE JULIO AV. y BELGRANO, CABA -34.613090 -58.381226
#' address_normalised lon lat
#'9 DE JULIO AV. y BELGRANO, CABA -58.381226 -34.613090
#'
#'
#' Si se trata de una dirección fuera de la Ciudad Autónoma de Buenos Aires, explicitar el municipio o partido
#' USIG_geocode("9 de julio y belgrano, temperley")
#'
#' resultado
#'
#' address address_normalised lat lng
#' 9 de julio y belgrano 9 DE JULIO AV. y BELGRANO, CABA -34.613090 -58.381226
#' address_normalised lon lat
#'Avenida 9 de Julio y Paso bajo nivel Manuel Belgrano, Lomas de Zamora -58.39645 -34.77974
#'
#' Se pueden georeferenciar varias direcciones a la vez:
#'
#' USIG_geocode(c("9 de Julio y Belgrano, Temperley", "Callao y Corrientes", "Anchorena 1210, La Lucila"))
#'
#' resultado
#' address_normalised lon lat
#' Avenida 9 de Julio y Paso bajo nivel Manuel Belgrano, Lomas de Zamora -58.3964491 -34.7797373
#' CALLAO AV. y CORRIENTES AV., CABA -58.392293 -34.604434
# ' Tomás Anchorena 1210, Vicente López -58.4935336530612 -34.5009281857143

USIG_geocode <- function(address) {

base_url <- "http://servicios.usig.buenosaires.gob.ar/normalizar/"
make_call <- function(address) {

make_address_query <- function(address) {
query <- paste0(base_url,
"?direccion=",
address,
"&geocodificar=TRUE")
}
#stopifnot(is.character(address))

query <- URLencode(make_address_query(address))
base_url <- "http://servicios.usig.buenosaires.gob.ar/normalizar/"

flog.info(paste("Trying", query))
make_address_query <- function(address) {
query <- paste0(base_url,
"?direccion=",
address,
"&geocodificar=TRUE")
}

query <- URLencode(make_address_query(address))

paste("Trying", query)

results <- tryCatch(httr::GET(query),
error = function(error_message) {return(NULL)})
results <- tryCatch(httr::GET(query),
error = function(error_message) {return(NULL)})



if (!httr:::is.response(results)) {
data.frame(address = address, address_normalised = NA,
lat = NA, lng = NA, stringsAsFactors = FALSE)
} else {
results <- httr::content(results)
if (length(results$direccionesNormalizadas)) {
x <- results$direccionesNormalizadas[[1]]$coordenadas$x
y <- results$direccionesNormalizadas[[1]]$coordenadas$y
address_normalised <- results$direccionesNormalizadas[[1]]$direccion
if (!httr:::is.response(results)) {
data.frame(address = address, address_normalised = NA,
lon = NA, lat = NA, stringsAsFactors = FALSE)
} else {
x <- NA
y <- NA
address_normalised <- NA
}
results <- httr::content(results)
if (length(results$direccionesNormalizadas)) {
x <- results$direccionesNormalizadas[[1]]$coordenadas$x
y <- results$direccionesNormalizadas[[1]]$coordenadas$y
address_normalised <- results$direccionesNormalizadas[[1]]$direccion
} else {
x <- NA
y <- NA
address_normalised <- NA
}


data.frame(address = address,
address_normalised = ifelse(is.null(address_normalised), NA, address_normalised),
lat = ifelse(is.null(y), NA, y),
lng = ifelse(is.null(x), NA, x),
stringsAsFactors = FALSE)
data.frame(address_normalised = ifelse(is.null(address_normalised), NA, address_normalised),
lon = ifelse(is.null(x), NA, x),
lat = ifelse(is.null(y), NA, y),
stringsAsFactors = FALSE)
}
}

Reduce(rbind, lapply(address, make_call))

}
18 changes: 14 additions & 4 deletions man/USIG_geocode.Rd

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

0 comments on commit f42cfb3

Please sign in to comment.