[R-sig-Geo] split SpatialPolygon after unionSpatialPolygons()

Roger Bivand Roger.Bivand at nhh.no
Wed Jul 15 19:03:33 CEST 2009


On Tue, 14 Jul 2009, pecardoso wrote:

> I've used unionSpatialPolygons to dissolve polygons and get a grid as a 
> spatialpolygon.
>
> A simple example:
> grelha<-readGDAL(paste("C:/...","teste.bmp",sep=""))

Please only use examples that refer to a publically available data set.

> grd <- slot(grelha, "grid")
> pol <- as.SpatialPolygons.GridTopology(grd)
> pol <- unionSpatialPolygons(pol, SGDF2PCT(grelha)$idx)
>

Exactly. This creates a rectangular polygon for each raster cell specified 
by grd, then removes all internal boundaries between those raster cells 
with the same value of the index, additionally placing all the separate 
Polygon objects with the same value of the index in a Polygons object. 
No-one has given them IDs, all the function has to work on are the index 
values.

I think that this is the same question that you asked recently, to which I 
replied indicating that you should use labcon() in adehabitat to give a 
separate index to each disconnected cluster with the same index values.

The alternative is to re-format the output SpatialPolygons object to find 
out how many Polygon objects there are in each Polygons object, and then 
to flatten those with more than one - that is make each such Polygon 
object into a singleton Polygons object with a unique ID slot value. It is 
possible, but you will need some knowledge of the way these objects are 
constructed. If you can post a sample image, it may not be too hard, 
either using labcon() or by re-formatting the representation.

Roger

> When the original image have more than one region or 'islands' of a 
> single color (imagine a small logo), the unionSpatialPolygons() will 
> dissolve polygons based on the ID's provided, in such a way that 
> coordinates(pol) will return N lines, as much as different ID's or 
> colors retrieved with SGDF2PCT(grelha)$idx.
>
> It means that one cannot get centroids of each individual regions. 
> Beside this, if the original image have this pattern, each number is a 
> pixel of a single color:
>
> 00100
> 02000
> 00020
> 10000
>
> coordinates() will give only 3 centroids and only for three of the 
> polygons (not even a common centroid for 1 and 2 ID's).
>
> I'd like to split the spatialpolygon object obtained with 
> unionSpatialPolygons(pol, SGDF2PCT(grelha)$idx) but retaining the ID's 
> in order to get individual centroids for each region, in this simple 
> case, two centroids for 1, two for 2 and two for 0.
>
> Any idea will be welcome
>
>
>
> 	[[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
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list