diff --git a/DESCRIPTION b/DESCRIPTION index 625092e..8a95849 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,6 +19,7 @@ Imports: tibble, purrr, rlang, + stringr, utils Suggests: testthat, diff --git a/NAMESPACE b/NAMESPACE index f0d1860..733d0ff 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,3 +6,4 @@ export(geocode_tbl) export(reverse_geocode) export(reverse_geocode_tbl) importFrom(magrittr,"%>%") +importFrom(rlang,":=") diff --git a/R/geocode.R b/R/geocode.R index 3abfa92..cdeb626 100644 --- a/R/geocode.R +++ b/R/geocode.R @@ -50,17 +50,37 @@ get_features <- function(x) { #' Geocode #' -#' @param query a string of the adress you want to geocode +#' @param query a string of the address you want to geocode +#' @param limit an integer indicating the maximum number of results wanted +#' @param autocomplete a boolean +#' @param type a string with the type of result wanted. +#' Default returns all results. +#' Can have one of the following values: "all", "housenumber", "street", "locality" or "municipality" #' #' @return a tibble #' @export #' #' @examples #' geocode(query = "39 quai André Citroën, Paris") -#' -geocode <- function(query) { - base_url <- "http://api-adresse.data.gouv.fr/search/?q=" - get_query <- httr::GET(utils::URLencode(paste0(base_url, query))) +#' +geocode <- function(query, + limit = NULL, + autocomplete = NULL, + type = c("all", "housenumber", "street", "locality", "municipality")) { + + if (missing(type) || type == "all") + type <- NULL + + base_url <- "http://api-adresse.data.gouv.fr/search/" + get_query <- httr::GET( + url = base_url, + query = list( + q = query, + limit = limit, + autocomplete = as.integer(autocomplete), + type = type + ) + ) message( httr::status_code(get_query) ) diff --git a/R/geocode_tbl.R b/R/geocode_tbl.R index 0af67cb..0669f0b 100644 --- a/R/geocode_tbl.R +++ b/R/geocode_tbl.R @@ -10,6 +10,8 @@ #' @return an augmented data frame of class tbl with latitude, longitude, etc #' @export #' +#' @importFrom rlang := +#' #' @examples #' #' table_test <- tibble::tibble( @@ -35,7 +37,7 @@ geocode_tbl <- function(tbl, adresse, code_insee = NULL, code_postal = NULL) { dplyr::select(.data = tbl, !!! vars) %>% dplyr::mutate({{adresse}} := stringr::str_replace({{adresse}}, "'", " ")) %>% - readr::write_csv(path = tmp) + readr::write_csv(file = tmp, na = "") message( "If file is larger than 8 MB, it must be splitted\n", @@ -134,7 +136,7 @@ reverse_geocode_tbl <- function(tbl, longitude, latitude) { "latitude" = rlang::enquo(latitude) ) dplyr::select(.data = tbl, !!! vars) %>% - readr::write_csv(path = tmp) + readr::write_csv(file = tmp) tbl_temp <- dplyr::select( .data = tbl, diff --git a/man/geocode.Rd b/man/geocode.Rd index 934ca8f..63c3c57 100644 --- a/man/geocode.Rd +++ b/man/geocode.Rd @@ -4,10 +4,23 @@ \alias{geocode} \title{Geocode} \usage{ -geocode(query) +geocode( + query, + limit = NULL, + autocomplete = NULL, + type = c("all", "housenumber", "street", "locality", "municipality") +) } \arguments{ -\item{query}{a string of the adress you want to geocode} +\item{query}{a string of the address you want to geocode} + +\item{limit}{an integer indicating the maximum number of results wanted} + +\item{autocomplete}{a boolean} + +\item{type}{a string with the type of result wanted. +Default returns all results. +Can have one of the following values: "all", "housenumber", "street", "locality" or "municipality"} } \value{ a tibble diff --git a/tests/testthat/test_geocode.R b/tests/testthat/test_geocode.R index d29ffe4..1a71883 100644 --- a/tests/testthat/test_geocode.R +++ b/tests/testthat/test_geocode.R @@ -9,6 +9,16 @@ test_that( } ) +test_that( + desc = "Geocode works with arguments", + code = { + perros_out <- geocode("perros", type = "municipality", limit = 3) + expect_s3_class(perros_out, "tbl_df") + expect_equal(nrow(perros_out), 3) + expect_true(all(perros_out$type %in% "municipality")) + } +) + test_that( desc = "Reverse geocode works", code = {