[R-sig-Geo] adding ids and df to hexagonal polys ?

Horacio Samaniego horacio.samaniego at gmail.com
Thu Sep 10 18:43:08 CEST 2009


Thanks a lot Roger, I had overlooked spChFIDs()

I just run the packages off the repositories in ubuntu jaunty

I have sp = 2.8.1

it seem to be an old version with no rownames for my hexagon polys...
however, this an easy fix.

> # For a simple sequential numbering of freshly created polygons
> hpoly2 <-
spChFIDs(hpoly,as.character(1:length(slot(hpoly,"polygons"))))

did the trick and I now understand better how to create hexagonal
polys, 

thanks again!!


On Thu, 2009-09-10 at 18:19 +0200, Roger Bivand wrote:
> On Thu, 10 Sep 2009, Horacio Samaniego wrote:
> 
> > Thanks for the hints here... but I'm guessing that there's somethings
> > missing...
> >
> > the 5th command line (row.names(o) <- row.names(hpoly)) does not make
> > sense to me as hpoly is a spatialPolygon which has no row names
> 
> The overlay() step was just for illustration. Spatial* objects may have 
> row.names() methods, but they are not row.names()<- methods. Look for 
> spChFIDs() methods in maptools for changing or assigning IDs. However, the 
> hexagon polygons do have IDs, just look at them (assuming you have a 
> recent sp - you do update regularly?).
> 
> Roger
> 
> 
> >
> >> str(hpoly,max.lev=2)
> > Formal class 'SpatialPolygons' [package "sp"] with 4 slots
> >  ..@ polygons   :List of 965
> >  ..@ plotOrder  : int [1:965] 1 2 3 4 5 6 7 8 9 10 ...
> >  ..@ bbox       : num [1:2, 1:2] 446230 3778428 1331230 8047934
> >  .. ..- attr(*, "dimnames")=List of 2
> >  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
> >> summary(row.names(hpoly))
> > Length  Class   Mode
> >     0   NULL   NULL
> >
> > I thought, perhaps there's a spelling problem? but no...
> >> row.names(o) <- row.names(hpoly)
> >> hpoly1 <- SpatialPolygonsDataFrame(hpoly, data=o)
> > Error in SpatialPolygonsDataFrame(hpoly, data = o) :
> >  row.names of data and Polygons IDs do not match
> >
> > How is this possible?
> >
> >
> > So I tried a "brute force" approach, with no success
> >
> >> row.names(slot(cl,"polygons")) <- 1:length(slot(cl,"polygons"))
> > Error in `rownames<-`(x, value) :
> >  attempt to set rownames on object with no dimensions
> >
> > I seem to have the concept wrong to begin with here. How are IDs
> > assigned to a 'SpatialPolygons' class? I should be able to assign them
> > regardless of the template shape used, right? Which would make the
> > overlay() step unnecessary?
> >
> >
> > I guess that I need some clarification on how the class is built..
> > my apologies if it's too obvious of a  question... I just hope not to be
> > the only one missing this
> > thanks
> >
> > On Thu, 2009-09-10 at 09:22 +0200, Roger Bivand wrote:
> >> On Wed, 9 Sep 2009, Horacio Samaniego wrote:
> >>
> >>> Hi,
> >>>
> >>> I am trying to understand the different data types in the sp package.
> >>> However, I do not seem to get the way that spdf are built.
> >>>
> >>> For example, let say that I can build a hexagonal grid. That work just
> >>> fine using
> >>>
> >>>> regio <- readShapePoly("regions.shp")
> >>>> hpts <- spsample(regio,type="hexagonal",cells=30000)
> >>>> hpoly = HexPoints2SpatialPolygons(hpts)
> >>>
> >>> I'm unclear about adding a df with IDs to this new SpetialPolygons class
> >>
> >> library(maptools)
> >> xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
> >>   IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
> >> hpts <- spsample(xx, type="hexagonal", n=100)
> >> hpoly <- HexPoints2SpatialPolygons(hpts)
> >> o <- overlay(xx, hpts)
> >> row.names(o) <- row.names(hpoly)
> >> hpoly1 <- SpatialPolygonsDataFrame(hpoly, data=o)
> >> summary(hpoly1)
> >> spplot(hpoly1, "BIR74")
> >>
> >> After making sure that the IDs agree, just use the constructor for
> >> SpatialPolygonsDataFrame objects.
> >>
> >> Hope this helps,
> >>
> >> Roger
> >>
> >>>
> >>> I guess that I'm not understanding how ids are assigned... any hint
> >>> would be appreciated
> >>>
> >>> thanks
> >>>
> >>> _______________________________________________
> >>> R-sig-Geo mailing list
> >>> R-sig-Geo at stat.math.ethz.ch
> >>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >>>
> >>
> >
> >
>



More information about the R-sig-Geo mailing list