[R-sig-Geo] split SpatialPolygon after unionSpatialPolygons()
Paulo E. Cardoso
pecardoso at netcabo.pt
Thu Jul 16 16:35:26 CEST 2009
I think I got it working although without truly understand why some aspects
seems to be mandatory, such as assigning NA's into the SGDF to get labcon()
working.
grelha<-readGDAL(paste("A:\\",i,sep=""))
gr.topo <- slot(grelha, "grid")
#! GridTopology
codcores <- SGDF2PCT(grelha)$idx #! Convert RGB
into a single code ID for each color
codcores[codcores==1] <- NA #! Assign NA's
for ID =1
sgdf.gr<-SpatialGridDataFrame(gr.topo,data.frame(codcores)) #! Rebuild
SGDF with NA's
#image(sgdf.gr)
asc.gr = as.asc(as.matrix(sgdf.gr),xll = 0.5, yll = 0.5,) #! Build
object "asc"
grelhaID = labcon(asc.gr) #! Split
culsters (regions) with same ID's
class(grelhaID) #! classe
"asc"
#image(grelhaID)
grelhaID[is.na(grelhaID)] <- 0
#win.graph()
#image(grelhaID)
pixID = asc2spixdf(grelhaID) #! Build
SpatialPixelDataFrame from asc object
class(pixID)
names(pixID) <- "ID"
image(pixID)
____________
Paulo E. Cardoso
> -----Mensagem original-----
> De: Roger Bivand [mailto:Roger.Bivand at nhh.no]
> Enviada: quarta-feira, 15 de Julho de 2009 18:04
> Para: pecardoso
> Cc: R-sig-Geo
> Assunto: Re: [R-sig-Geo] split SpatialPolygon after
> unionSpatialPolygons()
>
> 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
>
> Checked by AVG - www.avg.com
> Version: 8.5.375 / Virus Database: 270.13.16/2241 - Release Date:
> 07/16/09 05:58:00
More information about the R-sig-Geo
mailing list