Skip to content

Commit

Permalink
small fix to reverse geocode
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Dec 16, 2024
1 parent f79d858 commit d0d62b7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
13 changes: 6 additions & 7 deletions R/reverse_geocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ reverse_geocode <- function(input_table,

# create a small range around coordinates

data.table::setDT(input_table)
margin <- 0.001 # 0.0001
input_table[, lon_min := lon - margin]
input_table[, lon_max := lon + margin]
input_table[, lat_min := lat - margin]
input_table[, lat_max := lat + margin]

data.table::setDT(input_table)
input_table[, c("lon_min", "lon_max", "lat_min", "lat_max") :=
.(lon - margin, lon + margin, lat - margin, lat + margin)]


# Narrow search scope in cnefe to bounding box
Expand Down Expand Up @@ -117,13 +116,13 @@ reverse_geocode <- function(input_table,
cnefe_nearby <- duckdb::dbSendQuery(con, query_filter_cases_nearby)
cnefe_nearby <- duckdb::dbFetch(cnefe_nearby)

# find closest point
# find closest points
data.table::setDT(cnefe_nearby)
cnefe_nearby[, lon_diff := abs(lon_inp - lon)]
cnefe_nearby[, lat_diff := abs(lat_inp - lat)]

cnefe_nearest <- cnefe_nearby[, .SD[which.min(lon_diff)]]
cnefe_nearest <- cnefe_nearest[, .SD[which.min(lon_diff)]]
cnefe_nearest <- cnefe_nearest[, .SD[which.min(lat_diff)]]

# organize output
cnefe_nearest[, c('lon_diff', 'lat_diff') := NULL]
Expand Down
14 changes: 10 additions & 4 deletions tests/tests_rafa/reverse_geocode_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ library(arrow)
library(duckdb)
library(DBI)
library(dplyr)
library(data.table)


# input -------------------------------------------------------

lonlat_df <- data.table(
df_coords <- data.frame(
id = 1:6,
lon = c(-67.83112, -67.83559, -67.81918, -43.47110, -51.08934, -67.8191),
lat = c(-9.962392, -9.963436, -9.972736, -22.695578, -30.05981, -9.97273)
)


tictoc::tic()
a <- reverse_geocode(lonlat_df)
tictoc::toc()
# 37.78 sec elapsed



a <- reverse_geocode(lonlat_df)
tictoc::tic()
b <- reverse_geocode2(lonlat_df)
tictoc::toc()
# 37.78 sec elapsed


ttt <- data.frame(id=1, lat=-15.814192047159876, lon=-47.90534614672923)
Expand Down
35 changes: 35 additions & 0 deletions tests/tests_rafa/states_bbox.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
library(sf)
library(dplyr)

# Load the state polygons
df <- geobr::read_state()

# Calculate bounding boxes of states
bounding_boxes <- df |>
st_as_sf() |> # Ensure df is an sf object
rowwise() |> # Process each polygon individually
mutate(
xmin = st_bbox(geom)["xmin"], # Extract xmin from the bounding box
ymin = st_bbox(geom)["ymin"], # Extract ymin from the bounding box
xmax = st_bbox(geom)["xmax"], # Extract xmax from the bounding box
ymax = st_bbox(geom)["ymax"] # Extract ymax from the bounding box
) |>
ungroup() |> # Unrowwise after rowwise operations
select(abbrev_state, xmin, ymin, xmax, ymax) |> # Select desired columns
st_drop_geometry()

# View the resulting bounding box data.frame
bounding_boxes

data.table::fwrite(bounding_boxes, './inst/extdata/states_bbox.csv')


head(input_table)

candidate_states <-
subset(x = bounding_boxes,
(xmin < bbox_lon_min | xmax > bbox_lon_max) &
(ymin < bbox_lat_min | ymax > bbox_lat_max)
)


0 comments on commit d0d62b7

Please sign in to comment.