I am seeing an issue with some polygon features loading incorrectly as terra SpatVectors. I cannot replicate this issue yet, but here is an example polygon as an rds on Google Drive.

If I load this .rds through readRDS, I have a SpatialPolygonsDataFrame which shows the complete, correct polygon.

However, if I then convert this SpatialPolygonsDataFrame to a SpatVect through: terra::vect(spdf), the polygon has now changed. It is only a single part of the polygon, which is a standalone square part of the polygon. The extent has also changed. I'm not seeing any arguments that could alter this in the documentation for terra::vect.

If I load the original data directly as a SpatVect I have the same issue with this polygon and others. How can I load these polygons as SpatVectors (or convert from SpatialPolygonsDataFrame if necessary), but have the polygon shape remain correct?

  • Your rds appears to be corrupt but, nonetheless I would recommend converting to an sf object using sf::st_as_sf(spdf). This will hopefully retain multipart geometry. I honestly do not see a compelling reason to coerce to a SpatVect object as sf is much more powerful. There are a few instances where the result is a SpatVect (eg., vectorization using terra::as.polygons) object but, the power in vector analysis lays in the sf package. If you are want to use terra::extract or exactextractr::exact_extract sf vector objects are directly supported. You can always coerce sf to SpatVect. Commented Feb 5 at 22:58

1 Answer 1


There is an issue with your polygons. In short, they are invalid.

a <- readRDS("/home/sapi/Downloads/test_polygon.rds")


So far so good. But in {terra}, as you said, only first polygon is plotted:

a |>
  terra::vect() |>

Let's check it's (in)validity:

a |>
  terra::vect() |>
#> [1] FALSE

and repair it:

a |>
  terra::vect() |>
  terra::makeValid() |>

Created on 2024-02-06 with reprex v2.1.0

The similar approach can be used in {sf}: sf::st_as_sf() |> sf::st_make_valid().

  • Thank you, I thought there was a function like that I was trying to remember. Short of checking for issues like this / knowing when it may be an issue, is there any way you know of the expect that it will be necessary to use makeValid()?
    – Scott Z
    Commented Feb 6 at 16:42
  • Not by definition. Sometimes a warning message can be thrown by function, sometimes the results are far away from what is expected. But to generalize - it can be assumed that files downloaded from the Internet, prepared by someone in an unknown way, may be defective. Moreover, rds is a non-spatial data format, my experience with sf features tells me that storing spatial data in such files is asking for trouble. Commented Feb 6 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.