@@ -22,73 +22,23 @@ check_terminated <- function(code) {
2222check_terminated_possibly <- purrr :: possibly(check_terminated )
2323
2424
25- bulk_reverse_geocode <- function (dat , prev_data = NULL , curr_radius = 125L ) {
26- if (curr_radius > 2000L ) {
27- cli :: cli_alert_info(paste0(
28- " Geocoding searches have not found some replacement postcodes despite " ,
29- " searching up to 2km in radius from the original postcode location."
30- ))
31- prev_data # return early
32- }
33-
34- geodata_return <- dat | >
35- dplyr :: select(c(" longitude" , " latitude" )) | >
36- dplyr :: mutate(limit = 1L ) | >
37- dplyr :: mutate(radius = curr_radius ) | >
38- dplyr :: mutate(batch = ceiling(dplyr :: row_number() / 100L )) | >
39- # batch into groups of max 100 rows
40- tidyr :: nest(.by = " batch" ) | >
41- dplyr :: pull(" data" ) | >
42- purrr :: map(get_geodata_return ) | >
43- purrr :: list_flatten() # re-combine batches into a single list
44-
45- geodata_queries <- geodata_return | >
46- purrr :: map(" query" )
47-
48- geodata_query_results <- geodata_return | >
49- purrr :: map(" result" ) | >
50- purrr :: list_flatten()
51-
52- nifty_bind_cols <- function (x , y ) {
53- x2 <- x | >
54- tibble :: as_tibble_row() | >
55- dplyr :: select(c(" longitude" , " latitude" )) | >
56- dplyr :: rename_with(\(x ) paste0(" orig_" , x ))
57- if (! is.null(y )) {
58- y2 <- y | >
59- purrr :: compact() | >
60- tibble :: as_tibble() | >
61- unnest_codes()
62- } else {
63- y2 <- NULL
64- }
65- dplyr :: bind_cols(x2 , y2 )
66- }
67-
68- data_out <- geodata_queries | >
69- purrr :: map2(geodata_query_results , nifty_bind_cols ) | >
70- purrr :: list_rbind()
71-
72- if (! " postcode" %in% colnames(data_out )) {
73- data_out <- data_out | >
74- dplyr :: mutate(postcode = NA_character_ )
75- }
76-
77- dat_done <- data_out | >
78- dplyr :: filter(! dplyr :: if_any(" postcode" , is.na )) | >
79- dplyr :: bind_rows(prev_data )
25+ get_geodata_return <- function (x ) {
26+ base_request() | >
27+ httr2 :: req_body_json(list (geolocations = x )) | >
28+ pluck_result()
29+ }
8030
81- dat_missing <- data_out | >
82- dplyr :: filter(dplyr :: if_any(" postcode" , is.na )) | >
83- dplyr :: select(tidyselect :: starts_with(" orig_" )) | >
84- # rename to "longitude" and "latitude" for resubmission
85- dplyr :: rename_with(\(x ) sub(" ^orig_" , " " , x ))
8631
87- if (nrow(dat_missing ) > 0L ) {
88- bulk_reverse_geocode(dat_missing , dat_done , curr_radius * 2 )
32+ bulk_lookup <- function (x , filter_fields = filter_fields()) {
33+ if (is.null(filter_fields )) {
34+ req <- base_request()
8935 } else {
90- dat_done
36+ req <- base_request() | >
37+ httr2 :: req_url_query(filter = filter_fields , .multi = " comma" )
9138 }
39+ req | >
40+ httr2 :: req_body_json(list (postcodes = x ), auto_unbox = FALSE ) | >
41+ get_json_data()
9242}
9343
9444
@@ -107,21 +57,7 @@ autocomplete <- function(code) {
10757 unlist() | >
10858 sample(1L )
10959}
110- autocomplete_possibly <- purrr :: possibly(autocomplete )
111-
112-
113- bulk_lookup <- function (x ) {
114- base_request() | >
115- httr2 :: req_body_json(list (postcodes = x ), auto_unbox = FALSE ) | >
116- get_json_data()
117- }
118-
119-
120- get_geodata_return <- function (x ) {
121- base_request() | >
122- httr2 :: req_body_json(list (geolocations = x )) | >
123- pluck_result()
124- }
60+ autocomplete_possibly <- purrr :: possibly(autocomplete , NA_character_ )
12561
12662
12763pluck_result <- \(req ) purrr :: pluck(get_json_data(req ), " result" )
0 commit comments