0

I have a bunch of polygons representing land cover classes, however I don't need this much detail. I would like to merge small polygons (ie < 150m2) with it's largest neighbour. This would be similar to "Eliminate" in ArcGIS Pro or "eliminate selected polygons" in QGIS. This process will be repeated so I'd like to script it in R and save some time.

The only way I could think to do it was to add a column indicating which polygons were under 150m2, then somehow merging them to nearest neighbour.

#Packages
library(terra)
library(sf)
library(dplyr)

#Adding polygons from Terra and converting to sf
v <- vect(system.file("ex/lux.shp", package="terra"))
v <- st_as_sf(v[c(1:12)])


#Adding a column indicating which polygons are under 150m2
mutate(v, area_thresh = case_when(AREA < 150 ~ 1,
                               AREA > 150 ~ 0))

1 Answer 1

1

You can try terra::combineGeoms.

Example data

library(terra)
v <- vect(system.file("ex/lux.shp", package="terra"))

Solution

th <- v$AREA < 150
x <- combineGeoms(v[!th], v[th])

Illustration

v$th <- th
plot(v, "th")
lines(x, lwd=3, col="blue")

enter image description here

2
  • I'm getting errors using combine Geoms with some multipolygons: Error: [combineGeoms] x and y must be polygons and Error: [] nrow dataframe does not match nrow geometry any idea what is causing those?
    – B-C B.
    Dec 1 at 22:09
  • Then please raise an issue here github.com/rspatial/terra/issues and include an example that generates this error. Dec 2 at 15:36

Your Answer

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

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