closestCountry: Return country from point

View source: R/closestCountry.R

closestCountryR Documentation

Return country from point

Description

Determines which country a given point falls in.

Usage

closestCountry(pt, crs = 4326)

Arguments

pt

longitude and latitude, as a numeric vector, 2-column table, or spatial points object.

crs

the CRS of the coordinate. If pt is a spatial object, this argument is ignored. The default 4326 indicates longlat unprojected.

Details

Based on a predetermined set of global points, this function finds the country of occurrence. This can be useful for checking the validity of a point by comparing the returned country to the country listed with the occurrence record. If a point falls close to the boundary between two countries, the names of the nearby countries are returned. This function will not be of much value if the point falls in the ocean, as it will return the country that is closest, regardless of how far away it is.

Value

If one point is provided, a character vector is returned. If multiple points are provided, a list of character vectors is returned.

Author(s)

Pascal Title

Examples

#point near a country border
closestCountry(c(-115.436, 32.657))

# testing different input options
samp <- sample(1:nrow(crotalus), 10)
xy <- crotalus[samp, c('decimallongitude', 'decimallatitude')]
sfpts <- sf::st_as_sf(xy, coords = c('decimallongitude', 'decimallatitude'), crs = 4326)
sfptsEA <- sf::st_transform(sfpts, crs = '+proj=eqearth')
spPts <- as(sfpts, 'Spatial')
closestCountry(xy)
closestCountry(sfpts)
closestCountry(sfptsEA)
closestCountry(spPts)


rangeBuilder documentation built on March 7, 2023, 5:30 p.m.