Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract coordinates from an R/terra SpatVector object?

I have some SpatVector objects from which I would like to get the point coordinates back out. From the documentation and examples, it looks like as.data.frame should do the job…

sq = terra::vect(cbind(c(0, 1, 1, 0, 0), c(0, 0, 1, 1, 0)), type = "points", crs = "local")
terra::as.data.frame(sq)

… but I am only getting empty values.

data frame with 0 columns and 0 rows

What is the correct way to get the matrix/data.frame of coordinates in this situation?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

terra::geom() returns a matrix with i.a. coordinate columns which you can convert to a data frame as follows (using your example data sq):

sq |> geom() |> as.data.frame()
  geom part x y hole
1    1    1 0 0    0
2    2    1 1 0    0
3    3    1 1 1    0
4    4    1 0 1    0
5    5    1 0 0    0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading