jdberson/dwcPrepare

It is likely hard for people to use `dwc_locality` without a fallback default

Opened this issue · 0 comments

We currently require users to supply localities_sf and localities_names to dwc_locality for it work. It would be nice if there was an automatic method like the below function:

library(tidygeocoder)
library(geosphere)
library(purrr)

# to use, change the lats and longs values below to those you want to 
# reverse geocode. Then, click Run and set the output in the Console
# (is also saved to the our variable)

lats <- c(38.89,-32.029655, -33.110168, -25.128663, -33.982473)
longs <- c(-77.03, 115.949465, 117.093225, 127.502052, 115.674972)



tib <- reverse_geo(
  lat = lats,
  long = longs,
  method = 'arcgis',
  full_results = TRUE
)

# get city coordinates
cities <- tib$City
states <- tib$Region
countries <- tib$CntryName
cities_gps <- geo(city=cities)

# get distance and bearing to the nearest city
get_dist_and_bear <- function(long1, lat1, long2, lat2) {
  distance <- distHaversine(c(long1, lat1), c(long2, lat2))
  bearing <- bearing(c(long1, lat1), c(long2, lat2))
  dir <- setNames(
    seq(0, 360, by=22.5),
    c("N","NNE","NE", "ENE", "E", "ESE", "SE", "SSE", "S",
      "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N")
  )
  
  b <- bearing
  if (b < 0) b <- b + 360
  compass_dir <- names(which.min(abs(dir - b)))
  
  return (cbind(distance, compass_dir, bearing))
}

dists_and_bears <- 1:nrow(cities_gps) %>%
  map(function(x) get_dist_and_bear(cities_gps$long[x], cities_gps$lat[x], longs[x], lats[x]))


print_findings <- function(lat, long, d_and_b, city, state, country) {
  print(
    paste0(
      'Latitude: ', lat,
      ', Longitude: ', long,
      ' is ',
      round(as.numeric(d_and_b[1])/1000, 3), ' Kms ',
      d_and_b[2], ' of ',
      city, ', ',
      state, ', ',
      country
    )
  )
}

out <- 1:nrow(cities_gps) %>%
  map(function(x) print_findings(lats[x], longs[x], dists_and_bears[[x]], cities[x], states[x], countries[x]))

I believe we updated dwc_locality to use an sf as it is tradition in Australia to use gazetted localities, however, it seems that gazetted localities are an Australian term. In that case, I think we could make localities_sf and localities_names optional arguments and then use tidygeosphere as a fallback @jdberson.