Skip to contents

Takes a data frame with point locations in decimal degrees, and an sf POLYGON object with county boundaries and uses st_join to assign country, countryCode, stateProvince and county Darwin Core terms to each point location.

Usage

dwc_country_to_county(
  df,
  decimalLongitude,
  decimalLatitude,
  county_sf,
  country_column_name = "country",
  countryCode_column_name = "countryCode",
  stateProvince_column_name = "stateProvince",
  county_column_name = "county"
)

Arguments

df

A data frame containing the decimal longitude and latitude coordinates

decimalLongitude

The longitude of the focal point in decimal degrees. Output of dwc_coordinates. See: http://rs.tdwg.org/dwc/terms/decimalLongitude.

decimalLatitude

The latitude of the focal point in decimal degrees. Output of dwc_coordinates. See: http://rs.tdwg.org/dwc/terms/decimalLatitude.

county_sf

An sf POLYGON object that includes the county boundaries along with the higher geography features.

country_column_name

The column name in county_sf that gives the name of the country in which the county is found. The default value is "country".

countryCode_column_name

The column name in county_sf that gives the countryCode of the country in which the county is found. The default value is "countryCode".

stateProvince_column_name

The column name in county_sf that gives the State or Province in which the county is found. The default value is "stateProvince".

county_column_name

The column name in county_sf that gives the county name. The default value is "county".

Value

The original df object with the additional columns country, countryCode, stateProvince and county.

Details

Note that the function expects county to be nested within stateProvince which is in turn nested within country. The sf POLYGON object with the county boundaries should contain features describing these higher level geographies.

Examples

# Make up some point data
point_locations <- data.frame(
  name = c("A", "B", "C"),
  lon = c(117.093225, 127.502052, 115.674972),
  lat = c(-33.110168, -25.128663, -33.982473)
)

# Make up some county polygon data nested within higher geographies
county_sf <-
  sf::st_buffer(sf::st_as_sf(point_locations,
    coords = c("lon", "lat"),
    remove = FALSE,
    crs = sf::st_crs("EPSG:4326")
  ), dist = 1000) |>
  dplyr::mutate(
    country_raw = c("Country 1", "Country 2", "Country 3"),
    countryCode_raw = c("C1", "C2", "C3"),
    stateProvince_raw = c("State 1", "State 2", "State 3"),
    county_raw = c("County 1", "County 2", "County 3")
  )

# Run the function
dwc_country_to_county(
  df = point_locations,
  decimalLongitude = "lon",
  decimalLatitude = "lat",
  county_sf = county_sf,
  country_column_name = "country_raw",
  countryCode_column_name = "countryCode_raw",
  stateProvince_column_name = "stateProvince_raw",
  county_column_name = "county_raw"
)
#>   name      lon       lat   country countryCode stateProvince   county
#> 1    A 117.0932 -33.11017 Country 1          C1       State 1 County 1
#> 2    B 127.5021 -25.12866 Country 2          C2       State 2 County 2
#> 3    C 115.6750 -33.98247 Country 3          C3       State 3 County 3