ropensci / CoordinateCleaner

Automated flagging of common spatial and temporal errors in biological and palaeontological collection data, for the use in conservation, ecology and palaeontology.

Home Page:https://docs.ropensci.org/CoordinateCleaner/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Persistent Error country code

Edna-AM opened this issue · comments

Hi
I am trying to use the package, but I have some problems.
Apparently, the conversion from iso2c to iso3c is correct but the error persists. I use the code suggested in the Tutorial - Cleaning GBIF data. I used the code suggested in another issue too and the error continues.

`P_guajava <- read.csv("~/OneDrive/P. guajava Fin2.csv")

#Test 1
P_guajava$countryCode <- countrycode(P_guajava$countryCode, origin = 'iso2c', destination = 'iso3c')
View(P_guajava)
P_guajava<-data.frame(P_guajava)
View(P_guajava)

Prueba <- clean_coordinates(x = P_guajava, lon = "decimalLongitude",
lat = "decimalLatitude", countries = "countryCode",
species = "species",
tests = c("centroids", "equal", "institutions",
"duplicates", "outliers", "seas","zeros",
"countries"))

#Test 2
library("ISOcodes")
P_guajava$countryCode <- ISO_3166_1$Alpha_3[match(P_guajava$countryCode, ISOcodes::ISO_3166_1$Alpha_2)]
View(P_guajava)
P_guajava_clean <- CoordinateCleaner::clean_coordinates(P_guajava, lon = "decimalLongitude",
lat = "decimalLatitude",
species = "species",
countries = "countryCode",
value = "clean",
tests = c("centroids", "equal", "institutions",
"duplicates", "outliers", "seas","zeros",
"countries"))`

Error
Testing country identity x[i, ] is invalid Error in [.data.frame(sp::over(x = dat, y = ref), , "iso_a3_eh") : undefined columns selected In addition: Warning messages: 1: In RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid") : Ring Self-intersection at or near point 78.719726559999998 31.887646480000001 2: In rgeos::gIntersection(x[i, ], y, byid = TRUE, drop_lower_td = TRUE) : Invalid objects found; consider using set_RGEOS_CheckValidity(2L)

I appreciate it if you can help me.
Sincerely
Edna

OK, could you provide exampled ata to reproduce the error please? Thanks!

Ok this is related to the cc_coun test. The function cannot find the default column in the country borders gazetteer to math the iso-3 names. This can have two reasons 1) you use a custom reference dataset which ahs different column names, or 2) you use a different version of rnaturalearth, since the names changed during version. The solutions since the latest version of CoordinateCleaner is that you can specify the cloumn name in the reference dataset via the col_ref argument in cc_coun or the country_refcol argument in clean_coordinates. To illustrate the issue:

# Two different reference datasets with different structure
ref1 <- rnaturalearth::ne_countries(scale = "medium")
ref2 <- rnaturalearth::ne_countries(scale = "small")

# example data
dat <- data.frame(decimallatitude = c(51.5, -10), decimallongitude = c(8, 40), countrycode = c("DEU", "DEU"))


"iso_a3_eh" %in% names(ref1) # does contain the default name (since it is the default dataset)
cc_coun(x = dat) # therefore this works

"iso_a3_eh" %in% names(ref2) # does not contain the default name 
cc_coun(x = dat, ref = ref2) # therefore this does not work
cc_coun(x = dat, ref = ref2, ref_col = "iso_a3") # but this does