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

Roger Bivand Roger.Bivand at nhh.no
Thu Sep 10 18:19:18 CEST 2009


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
>>>
>>
>
>

-- 
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