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 arguments to geocode() #32

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Imports:
tibble,
purrr,
rlang,
stringr,
utils
Suggests:
testthat,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export(geocode_tbl)
export(reverse_geocode)
export(reverse_geocode_tbl)
importFrom(magrittr,"%>%")
importFrom(rlang,":=")
30 changes: 25 additions & 5 deletions R/geocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
6 changes: 4 additions & 2 deletions R/geocode_tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' @return an augmented data frame of class tbl with latitude, longitude, etc
#' @export
#'
#' @importFrom rlang :=
#'
#' @examples
#'
#' table_test <- tibble::tibble(
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 15 additions & 2 deletions man/geocode.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test_geocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down