Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nearest() returns NA for all to_id when centroids = FALSE #1471

Open
mablmabl opened this issue Apr 9, 2024 · 2 comments
Open

nearest() returns NA for all to_id when centroids = FALSE #1471

mablmabl opened this issue Apr 9, 2024 · 2 comments

Comments

@mablmabl
Copy link

mablmabl commented Apr 9, 2024

I have a number of geographical points in the sea and a map with Swedish sea areas. I want to know in which sea area each point lies and for the points outside sea areas the distance to nearest sea area including ID for the nearest sea area.

library(terra) #version 1.7-74

map<-vect("/vsizip//vsicurl/https://www.smhi.se/polopoly_fs/1.140307!/Havsomr_SVAR_2016_3b.zip")
id<-c(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)
lon<-c(15.29416656,15.10333347,15.27083302,15.27250004,15.1291666,15.31333351,15.24166679,15.30716705,15.32333374,15.38833332,15.41416645,15.40666676,15.27166653,15.26083374,15.1916666)
lat<-c(56.1566658,56.15000153,56.15250015,56.13999939,56.16383362,56.15333176,56.1558342,56.17416763,56.12366486,56.14250183,56.15916824,56.17666626,56.17666626,56.17300034,56.1570015)
pts<-vect(data.frame(id,lon,lat),geom=c("lon", "lat"), crs="EPSG:4326")
map<-project(map,"EPSG:3006")
pts<-project(pts,"EPSG:3006")
npol = nearest(pts, map[,"HID"], centroids=FALSE)

As you can see when you run this simple code that to_id is always NA and of type logical. I want to have the column HID from map in column to_id (or other ID from map). Can I get that in some way?

> npol
class : SpatVector
geometry : points
dimensions : 15, 5 (geometries, attributes)
extent : 506419.7, 525716.6, 6219890, 6225817 (xmin, xmax, ymin, ymax)
coord. ref. : SWEREF99 TM (EPSG:3006)
names : from_id from_x from_y to_id distance
type : <int> <num> <num> <logical> <num>
values : 1 5.183e+05 6.224e+06 <NA> 0
2 5.064e+05 6.223e+06 <NA> 0
3 5.168e+05 6.223e+06 <NA> 0

Many thanks :-)

@mablmabl
Copy link
Author

If I set centroids = TRUE in function call everything works as it should and to_id is filled with correct index integer values. It seems that it is centroids = FALSE that not works. Possibly a bug?

@mablmabl mablmabl changed the title nearest() returns NA for all to_id nearest() returns NA for all to_id when centroids = FALSE Apr 11, 2024
@mgdesaix
Copy link

I just ran into this problem too - I have code that used to work with centroids = FALSE from ~6 months or so ago. Not sure what my terra version was then, but now I have 1.7-71 and the same code is providing me with all NAs as well for the "to_id"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants