Merge / Dissolve small polygons by size using R
Asked Answered
H

0

6

I converted a raster with 4 classes to a SpatialPolygonsDataFrame, where the features representing a class are not always connected. I used disaggregate (package sp) to get single features.

I now want to get rid of all those features, smaller than 3000 m². I already generated the area with poly.areas(package GISTools) and added the info as attribute to the Shapefile.

The small features should be merged with their surrounding feature.

Has anyone an idea how to solve this? unionSpatialPolygons does only work with IDs, not conditions of attributes.

plot: "small features" in red, numbers indicate the attribute (1-4)

Hine answered 25/11, 2015 at 13:54 Comment(7)
What's "their surrounding feature"? Are the "small features" always completely enclosed by another feature? Otherwise which neighbouring feature do you merge it with? Do you need to preserve attributes of the small features? Could you colour the "small features" on your plot so we can see what you are up to?Adeleadelheid
Yes, the "small features" are always enclosed, and no the attributes of those small ones do not need to be preserved. In general I want to have fewer features and eliminate "islands" / "fragments".Hine
So if you delete the small features and removed holes from any features with holes that would do it? Oh no, you have small features on the edge... They arent surrounded... and there's a "2" on the left sandwiched between a 1 and a 3 - who gets that territory?Adeleadelheid
Oh well, I see your point! Seems for the edges etc. it'll be way to complicated...Hine
Maybe not... Compute the polygon adjacency (spdep::poly2nb) then for each small polygon choose one of its adjacent polys to merge with via maptools::unionSpatialPolygons - can you share your shapefile?Adeleadelheid
Ok, I'll try that, thank you! My whole work flow is supposed to be automatic, so I'll have to check if I can implement the functions wisely.Hine
The prob is how to automate the choice then - if the small polygon has one neighbour then its trivial, otherwise choose the neighbour with the largest area?Adeleadelheid

© 2022 - 2024 — McMap. All rights reserved.