
Identify and remove invalid coordinates
Source:R/remove_invalid_coordinates.R
remove_invalid_coordinates.RdThis function identifies and removes invalid geographic coordinates, including non-numeric values, NA or empty values, and coordinates outside the valid range for Earth (latitude > 90 or < -90, and longitude > 180 or < -180).
Usage
remove_invalid_coordinates(
occ,
long = "decimalLongitude",
lat = "decimalLatitude",
return_invalid = TRUE,
save_invalid = FALSE,
output_dir = NULL,
overwrite = FALSE,
output_format = ".gz",
verbose = FALSE
)Arguments
- occ
(data.frame or data.table) a dataset with occurrence records.
- long
(character) column name in
occwith the longitude.- lat
(character) column name in
occwith the latitude.- return_invalid
(logical) whether to return a list containing records that passed and failed this test. Default is TRUE.
- save_invalid
(logical) whether to save the records that failed this test (i.e., flagged as
FALSE). IfTRUE, anoutput_dirmust be provided. Default isFALSE.- output_dir
(character) path to an existing directory where records flagged as
FALSEwill be saved. Only used whensave_invalid = TRUE.- overwrite
(logical) whether to overwrite existing files in
output_dir. Only used whensave_invalid = TRUE. Default isFALSE.- output_format
(character) output format for saving flagged records. Options are
".csv"or".gz". Only used whensave_invalid = TRUE. Default is".gz".- verbose
(logical) whether to print messages about function progress. Default is
TRUE.
Value
The input data.frame with an additional logical column indicating whether
each record passed (TRUE) or failed (FALSE) the coordinate quality check
(i.e., is not missing, non-numeric, or outside the possible range for Earth:
latitude between -90 and 90, longitude between -180 and 180). As with all
other flagging functions in RuHere, TRUE indicates that the record passed
this test and is eligible for retention; FALSE indicates it failed and is
flagged as potentially problematic, and can be removed using
remove_flagged().
If return_invalid = FALSE, returns the occurrence dataset containing only
records flagged as TRUE.
If return_invalid = TRUE (default), returns a list with two elements:
valid– the dataset with records flagged asTRUE.invalid– the dataset with records flagged asFALSE.
Examples
# Create fake data example
occ <- data.frame("species" = "spp",
"decimalLongitude" = c(10, -190, 20, 50, NA),
"decimalLatitude" = c(20, 20, 240, 50, NA))
# Split valid and invalid coordinates
occ_valid <- remove_invalid_coordinates(occ)