Thanks Roger.  This does exactly what I want, and also gives me an
understanding of the *apply functions, which will definitely come in handy.


Cam

On Mon, Aug 8, 2011 at 11:56 AM, Roger Bivand <Roger.Bivand@nhh.no> wrote:

> On Mon, 8 Aug 2011, Cam Plouffe wrote:
>
>  Hi everyone,
>>
>> I'm trying to make a function that constructs rectangular polygons based
>> on
>> the bounding boxes of other polygons from a SpatialPolygonsDataFrame.  I
>> am
>> having a number of errors right now, which I am unsure how to fix.  My
>> code
>> is basically as follows:
>>
>> temp = bbox(spdf_u[1,])
>> temp = matrix(temp, ncol = 2)
>>
>> mat = matrix(nrow = 5, ncol = 2)
>> mat[1,1] = temp[1,1]
>> mat[1,2] = temp[2,1]
>> mat[2,1] = temp[1,1]
>> mat[2,2] = temp[2,2]
>> mat[3,1] = temp[1,2]
>> mat[3,2] = temp[2,2]
>> mat[4,1] = temp[1,2]
>> mat[4,2] = temp[2,1]
>> mat[5,1] = temp[1,1]
>> mat[5,2] = temp[2,1]
>> rm(temp)
>>
>> temppoly = Polygon(mat)
>> polylist = list(temppoly)
>> temppolys = Polygons(polylist)
>>
>> I'm first encountering an error after I have constructed the Polygon
>> object
>> "temppoly".  It seems to have no problems at first, but if I try to plot
>> "temppoly", I get the following error:
>>
>> Error in as.double(y) :
>>  cannot coerce type 'S4' to vector of type 'double'
>>
>
> There is not plot method for "Polygon" objects.
>
>
>
>> I'm also encountering an error when attempting to construct the Polygons
>> object "temppolys":
>>
>> Error in Polygons(polylist) : Single ID required
>>
>>
> See ?Polygons, and ID is required.
>
> You can re-use things which are already there, and benefit from the *apply
> vectorisation way of doing things (which is worth learning, as it makes it
> easier to do things repeatedly):
>
> library(sp)
> library(maptools)
> xx <- readShapeSpatial(system.file("**shapes/sids.shp",
>  package="maptools")[1], IDvar="FIPSNO",
>  proj4string=CRS("+proj=longlat +ellps=clrk66"))
> # giving some data to play with
> bb0 <- lapply(slot(xx, "polygons"), bbox)
> # make a list of the bounding boxes of the Polygons objects in the #
> "polygons" slot of xx
> bb0[[1]] # show first component
> library(splancs)
> bb1 <- lapply(bb0, bboxx)
> # turn these into matrices using a helper function in splancs
> bb1[[1]] # show first component
> bb2 <- lapply(bb1, function(x) rbind(x, x[1,]))
> # close the matrix rings by appending the first coordinate
> bb2[[1]] # show first component
> rn <- row.names(xx)
> # get the IDs
> bb3 <- vector(mode="list", length=length(bb2))
> # make somewhere to keep the output
> for (i in seq(along=bb3)) bb3[[i]] <- Polygons(list(Polygon(bb2[[i]]**)),
>  ID=rn[i])
> # loop over the closed matrix rings, adding the IDs
> bb3[[1]] # show first component
> bbs <- SpatialPolygons(bb3, proj4string=CRS(proj4string(**xx)))
> plot(bbs)
> # make a "SpatialPolygons" object
>
> You were almost there, thanks for a clear explanation.
>
> Hope this helps,
>
> Roger
>
>  I'm very new to R, so these could be very simply problems, but I'm unable
>> to
>> find any reference material about these issues.  If anyone could offer me
>> some insight as to why I'm having these errors, and perhaps how to get
>> around them, it would be greatly appreciated.  If more code is needed, let
>> me know and I'll provide it.
>>
>> Thanks a lot,
>> Cam
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________**_________________
>> R-sig-Geo mailing list
>> R-sig-Geo@r-project.org
>> https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>>
>>
> --
> Roger Bivand
> Department of Economics, NHH Norwegian School of Economics,
> Helleveien 30, N-5045 Bergen, Norway.
> voice: +47 55 95 93 55; fax +47 55 95 95 43
> e-mail: Roger.Bivand@nhh.no
>
>

	[[alternative HTML version deleted]]

