[R-sig-Geo] Cleaning small spatial polygons

Jianyun Wu jianyun.fred.wu at gmail.com
Thu Nov 5 07:01:38 CET 2015


I tried to do that with a lattice map, and figured out a way to remove
those small polygons within a large area. But don't know whether it suits
your needs for raster.

I read the digital map with rgdal package.

Using Australia local government area as an example.

#remove the small polygons within the large LGA area

aus_lga = readOGR(dsn = ".", "LGA11aAust") #LGA
nsw_lga = aus_lga[aus_lga$STATE_CODE == 1, ]
N = length(nsw_lga)
for (i in 1:N) {
  pol = nsw_lga at polygons[[i]]@Polygons
  index = 1:length(pol)
  area = sapply(pol, function(x) x at area)
  n_remove = index[area!=max(area)]
  nsw_lga at polygons[[i]]@Polygons[n_remove] <- NULL
}

It basically extracts the area of the polygons within the S4 objects, and
set the spatial object of small polygons within a large area to NULL.

Regards

Fred

On Thu, Nov 5, 2015 at 5:34 AM, Eduardo Diez <eduardodiez at gmx.com> wrote:

> Is there any way of doing this or should i forget it and go on using GRASS
> through rgrass7?
>
> Thanks
>
> 2015-10-19 15:03 GMT-03:00 Eduardo Diez <eduardodiez at gmx.com>:
>
> > Ok. So here's a link to a zip file that contains two shapefiles:
> >  - pol_to_be_cleaned: the layer from which i'd like to remove small
> > polygons
> >  - pol_cleaned: the layer cleaned with the function v.clean rmarea
> >
> > http://1drv.ms/1GmRWS7
> >
> > The threshold i used for cleaning was 3000 (meaning 3000 squared meters).
> >
> > Although i do project it before sending it to GRASS, according to the
> > official help page it should be able to handle it:
> > "Threshold must always be in square meters, also for latitude-longitude
> > locations or locations with units other than meters"
> >
> > Thanks
> >
> > 2015-10-19 8:28 GMT-03:00 Roger Bivand <Roger.Bivand at nhh.no>:
> >
> >> On Fri, 16 Oct 2015, Eduardo Diez wrote:
> >>
> >> Dear list,
> >>> I'm willing to know if any knows a way of performing tha same thing i'm
> >>> doing through rgrass7 with GRASS when I execute the function v.clean
> with
> >>> "rmarea" as the tool argument. That is:
> >>>
> >>> "The rmarea tool removes all areas <= thresh. The longest boundary with
> >>> an
> >>> adjacent area is removed or all boundaries if there is no adjacent
> area.
> >>> Area categories are not combined when a small area is merged with a
> >>> larger
> >>> area."
> >>>
> >>> Basically i have raster of zones within a field. I convert it to
> >>> SpatialPolygonsDataFrame and in order to leave only the more
> >>> important/meaningful ones i remove the small/sliver with this tool. In
> >>> general it works fine but having to call an external software with a
> >>> specific version makes the script less portable and you have to be
> >>> careful
> >>> with updates and such. Also you have to write rasters and shapefiles
> back
> >>> and forth as GRASS can't work with in-memory objects.
> >>>
> >>
> >> Could you please provide an example of a built-in or contributed data
> set
> >> (URL, not attachment) with the slivers you mention, so that we know
> that we
> >> are addressing your problem? I don't think that:
> >>
> >> https://cran.r-project.org/web/packages/cleangeo/index.html
> >>
> >> does this, as it seems to try to repair broken geometries.
> >>
> >> Also note that you need to specify that the area threshold is in a
> square
> >> planar metric - dropping slivers in unprojected geometries may be more
> >> complicated.
> >>
> >> Roger
> >>
> >>
> >>> Does someone know a way of doing this in plain R?
> >>>
> >>> Thanks
> >>>
> >>>         [[alternative HTML version deleted]]
> >>>
> >>> _______________________________________________
> >>> R-sig-Geo mailing list
> >>> R-sig-Geo at r-project.org
> >>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >>>
> >>>
> >> --
> >> Roger Bivand
> >> Department of Economics, Norwegian School of Economics,
> >> Helleveien 30, N-5045 Bergen, Norway.
> >> voice: +47 55 95 93 55; fax +47 55 95 91 00
> >> e-mail: Roger.Bivand at nhh.no
> >>
> >>
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list