Create a logical mask identifying valid longitude/latitude pairs.

createLocationMask(
  longitude = NULL,
  latitude = NULL,
  lonRange = c(-180, 180),
  latRange = c(-90, 90),
  removeZeroZero = TRUE
)

Arguments

longitude

Vector of longitudes in decimal degrees east.

latitude

Vector of latitudes in decimal degrees north.

lonRange

Range of valid longitudes.

latRange

Range of valid latitudes.

removeZeroZero

Logical specifying whether the coordinate pair (0, 0) should be treated as invalid.

Value

Logical vector identifying valid locations.

Details

The returned logical vector contains TRUE for valid locations and FALSE for invalid locations. This is useful for filtering data frames to retain only records with valid geographic coordinates.

Longitude and latitude values are considered valid when they:

  • fall within lonRange and latRange

  • are not missing

  • are not located at (0, 0) when removeZeroZero = TRUE

The lonRange and latRange arguments can be used to restrict valid locations to a rectangular geographic region.

Examples

createLocationMask(
  longitude = c(-120, NA, -120, -220, -120, 0),
  latitude = c(45, 45, NA, 45, 100, 0)
)
#> [1]  TRUE FALSE FALSE FALSE FALSE FALSE

createLocationMask(
  longitude = -120:-90,
  latitude = 20:50,
  lonRange = c(-110, -100),
  latRange = c(30, 40)
)
#>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
#> [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE
#> [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE