terra icon indicating copy to clipboard operation
terra copied to clipboard

vect() inconsistent behaviour when geom not supplied

Open AMBarbosa opened this issue 1 month ago • 0 comments

vect() used to work for two-column matrices but fail for two-column data frames if geom was not supplied. Now, it works without geom also for data frames if the coordinate column names have certain characteristics (which is now documented under the geom argument), but sometimes it also works if the coordinates are real rather than integer (regardless of column names):

df <- data.frame(rbind(c(-180, -20), c(-140, 55)))

vect(df)
# Error: [vect] geom=NULL and no unique lon/lat or x/y variable pairs detected

vect(df, geom = c("X1", "X2"))
# OK

vect(sapply(df, jitter))
# also OK?!


names(df) <- c("x", "y")

vect(df)
# OK


names(df) <- c("long", "lati")

vect(df)
# OK


names(df) <- c("location.long", "location.lat")

vect(df)
# Error: [vect] geom=NULL and no unique lon/lat or x/y variable pairs detected

vect(sapply(df, jitter))
# OK

Even if the input data frame has more than two columns and the coordinate columns are not named as required, vect() may work without geom if the columns have real rather than integer numbers:

df <- data.frame(ID = 1:nrow(df), x1 = c(-180, -20), x2 = c(-140, 55))

vect(df)
# Error: [vect] geom=NULL and no unique lon/lat or x/y variable pairs detected

vect(sapply(df, jitter))
# SpatVector produced

Is this intentional? In any case, it doesn't appear to be documented. Regards,

AMBarbosa avatar Dec 11 '25 17:12 AMBarbosa