[R-sig-Geo] Is there a function for counting polygons within a multi-polygon SpatialPolygonDataFrame class

Michael Sumner mdsumner at gmail.com
Fri Nov 5 01:33:43 CET 2010


This will give you the number of separate "rings", but that won't
allow you to differentiate between separate simple polygons, and rings
that represent "holes":

sapply(SrDf at polygons, function(x) length(slot(x, "Polygons")))

It needs more work to check each individual "Polygon" in "x" to see if
slot(x[[i]], "hole") is TRUE. I think this will do it:

sapply(SrDf at polygons, function(x) sum( !sapply(slot(x, "Polygons"),
slot, "hole")))

Note that use slot(x, "name") is more verbose that x at name, but either
is not recommended for normal use and should be bundled into methods,
or helper functions.

I think it's worth delving into the structure of these objects, as
it's fairly simple once you get used to it and writing summary
functions is not hard.

Cheers, Mike.

On Fri, Nov 5, 2010 at 10:50 AM, Craig Mundy <Craig.Mundy at utas.edu.au> wrote:
>
> Hi All,
>
> I have searched for (but not found), a function for counting polygons within each record of a multi-polygon SpatialPolygonDataFrame class? I had thought of sifting through the slot information, but thought I would check if there was a function I had overlooked before I embarked on that. I can easily do this in SQL Server but would prefer to batch process in R along with some other tasks.
>
> Thanks
>
> Craig
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list