@@ -104,20 +104,6 @@ format_address <- function(df, fields) {
104104# QA Checks --------------------------------------------------------------------
105105# functions called by reverse_geo() and/or geo()
106106
107- check_api_options <- function (api_options , func_name ) {
108- for (param in names(api_options )) {
109- if (! param %in% c(" cascade_flag" , " init" , names(pkg.globals $ default_api_options ))) {
110- stop(
111- paste0(
112- " Invalid parameter " , ' "' , param , ' "' ,
113- " used in the api_options argument. See ?" , func_name
114- ),
115- call. = FALSE
116- )
117- }
118- }
119- }
120-
121107# check the data type of an address argument - called by geo() function
122108# should not be a matrix, class, or dataframe for instance
123109# allow factor since it could be coerced to a datatype by address handler function
@@ -229,22 +215,6 @@ check_limit_for_batch <- function(limit, return_input, reverse) {
229215 }
230216}
231217
232-
233- # check for HERE method batch queries --- for use in geo() and reverse_geo()
234- check_here_return_input <- function (here_request_id , return_input , reverse ) {
235- input_terms <- get_coord_address_terms(reverse )
236-
237- # If a previous job is requested return_addresses should be FALSE
238- # This is because the job won't send the addresses, but would recover the
239- # results of a previous request
240- if (is.character(here_request_id ) && return_input == TRUE ) {
241- stop(" HERE: When requesting a previous job via here_request_id, set " , input_terms $ return_arg ,
242- " to FALSE. See ?" , input_terms $ base_func_name , " for details." ,
243- call. = FALSE
244- )
245- }
246- }
247-
248218# Misc -----------------------------------------------------------------------------------------
249219
250220# # function for extracting everything except the single line
@@ -349,15 +319,26 @@ api_url_modification <- function(method, api_url, generic_query, custom_query, r
349319 return (api_url )
350320}
351321
352- # apply api options defaults for options not specified by the user.
353- # called by geo() and reverse_geo()
354- apply_api_options_defaults <- function (api_options ) {
355- for (name in names(pkg.globals $ default_api_options )) {
356- if (is.null(api_options [[name ]])) api_options [[name ]] <- pkg.globals $ default_api_options [[name ]]
322+
323+ # for specific geocoders in batch setting...
324+ # give a warning that the query is going to run with flatten=TRUE
325+ # even though the user specified flatten=FALSE
326+ flatten_override_warning <- function (flatten , method , reverse , batch ) {
327+ if (flatten == FALSE && (method %in% pkg.globals $ batch_flatten_required_methods )) {
328+ input_terms <- get_coord_address_terms(reverse )
329+ message(paste0(
330+ " Note: flatten=FALSE is ignored. Outputs must be flattened for the " ,
331+ get_setting_value(tidygeocoder :: api_info_reference , method , " method_display_name" ), " " ,
332+ if (batch == TRUE ) " batch" else paste0(" single " , input_terms $ input_singular ),
333+ " geocoder"
334+ ))
357335 }
358- return (api_options )
359336}
360337
338+
339+
340+ # Api options functions ----------------------------------------------------------------------
341+
361342# Set the api_options[["init"]] parameter
362343# init is for internal package use only, used to designate if the geo() or reverse_geo() function
363344# is being called for the first time (init = TRUE) or if it has called itself
@@ -369,17 +350,94 @@ initialize_init <- function(api_options) {
369350 return (api_options )
370351}
371352
372- # for specific geocoders in batch setting...
373- # give a warning that the query is going to run with flatten=TRUE
374- # even though the user specified flatten=FALSE
375- flatten_override_warning <- function (flatten , method , reverse , batch ) {
376- if (flatten == FALSE && (method %in% pkg.globals $ batch_flatten_required_methods )) {
377- input_terms <- get_coord_address_terms(reverse )
378- message(paste0(
379- " Note: flatten=FALSE is ignored. Outputs must be flattened for the " ,
380- get_setting_value(tidygeocoder :: api_info_reference , method , " method_display_name" ), " " ,
381- if (batch == TRUE ) " batch" else paste0(" single " , input_terms $ input_singular ),
382- " geocoder"
383- ))
353+ # check for HERE method batch queries --- for use in geo() and reverse_geo()
354+ check_here_return_input <- function (here_request_id , return_input , reverse ) {
355+ input_terms <- get_coord_address_terms(reverse )
356+
357+ # If a previous job is requested return_addresses should be FALSE
358+ # This is because the job won't send the addresses, but would recover the
359+ # results of a previous request
360+ if (is.character(here_request_id ) && return_input == TRUE ) {
361+ stop(" HERE: When requesting a previous job via here_request_id, set " , input_terms $ return_arg ,
362+ " to FALSE. See ?" , input_terms $ base_func_name , " for details." ,
363+ call. = FALSE
364+ )
365+ }
366+ }
367+
368+ # apply api options defaults for options not specified by the user
369+ # that are relevant for the specified method
370+ # called by geo() and reverse_geo()
371+ apply_api_options_defaults <- function (method , api_options ) {
372+ for (api_opt in names(pkg.globals $ default_api_options )) {
373+ api_opt_method <- strsplit(api_opt , " _" )[[1 ]][[1 ]] # extract method name from api option
374+
375+ if ((method == api_opt_method ) && is.null(api_options [[api_opt ]])) {
376+ api_options [[api_opt ]] <- pkg.globals $ default_api_options [[api_opt ]]
377+ }
378+ }
379+ return (api_options )
380+ }
381+
382+ # throw error if method and a specified api_option is mismatched
383+ # ie. method='census' and api_options(list(geocodio_hipaa=TRUE))
384+ # return_inputs : return_addresses for geo() or return_inputs for reverse_geo()
385+ check_api_options <- function (method , api_options , reverse , return_inputs ) {
386+
387+ stopifnot(
388+ is.null(api_options [[" mapbox_permanent" ]]) || is.logical(api_options [[" mapbox_permanent" ]]),
389+ is.null(api_options [[" here_request_id" ]]) || is.character(api_options [[" here_request_id" ]]),
390+ is.null(api_options [[" mapquest_open" ]]) || is.logical(api_options [[" mapquest_open" ]]),
391+ is.null(api_options [[" geocodio_hipaa" ]]) || is.logical(api_options [[" geocodio_hipaa" ]])
392+ )
393+
394+ if (method == " here" ) check_here_return_input(api_options [[" here_request_id" ]], return_inputs , reverse = reverse )
395+
396+
397+ # cycle through the api options specified (except for init)
398+ # if (api_options$init == TRUE) {
399+ api_method_mismatch_args <- c() # store mismatch api_options here
400+ api_bad_args <- c() # store invalid api_options here
401+ error_message <- c() # store error message here (if any)
402+
403+ for (api_opt in names(api_options )[! names(api_options ) %in% pkg.globals $ special_api_options ]) {
404+ # extract method name from api_option
405+ api_opt_method <- strsplit(api_opt , " _" )[[1 ]][[1 ]]
406+
407+ # check if api parameter is valid
408+ if (! api_opt %in% names(pkg.globals $ default_api_options )) {
409+ api_bad_args <- c(api_bad_args , api_opt )
410+ }
411+ # if api parameter is valid but there is a mismatch with selected method
412+ # then add offending arg to vector
413+ else if (api_opt_method != method ) {
414+ api_method_mismatch_args <- c(api_method_mismatch_args , api_opt )
415+ }
416+ } # end loop
417+
418+ # error message for bad api arguments
419+ if (length(api_bad_args ) != 0 ) {
420+ error_message <- c(error_message ,
421+ paste0(
422+ " Invalid api_options parameter(s) used:\n\n " ,
423+ paste0(api_bad_args , sep = " " ), " \n\n "
424+ ))
425+ }
426+
427+ # error message for api arguments that mismatch with the method argument
428+ if (length(api_method_mismatch_args ) != 0 ) {
429+ error_message <- c(error_message ,
430+ ' method = "' , method , ' " is not compatible with the specified api_options parameter(s):\n\n ' ,
431+ paste0(api_method_mismatch_args , sep = " " ), " \n\n "
432+ )
433+ }
434+
435+ # show error (if applicable)
436+ if (length(error_message ) != 0 ) {
437+ stop(error_message ,
438+ ' See ?' , if (reverse == TRUE ) " reverse_geo" else " geo" ,
439+ call. = FALSE
440+ )
384441 }
442+ # }
385443}
0 commit comments