[R-sig-Geo] SpatialPolygons decomposition...

Roger Bivand Roger.Bivand at nhh.no
Sat Feb 14 11:06:02 CET 2009


On Fri, 13 Feb 2009, Jim Burke wrote:

> Hi Peter,
>
> I think I am three days ahead of you on the learning curve. So come join me! 
> Below is a cumulation of suggestions from Rodger Bivand.
>
> Perhaps the code below may help. Start out with your own SpatialPolygon. 
> Change it to a dataframe. Then do something data like to the dataframe. Then 
> coerce back to a SpatialPolygon dataframe. Then dump the SpatialPolygon in 
> various ways. Or you could simply dump the SpatialPolygon. 
> # the following code takes an sp to a df to add the two columns
> # then the df is coerced back into an sp. This solves a merge
> # issue when merging an sp and df together. R thinks the result
> # should be a data.frame so good bye SpatialPolygons
>   tx2_df <- as(tx2_sp, "data.frame")   #make a sp into a df
>
>   tx2_df1      <- merge(tx2_df, votes2_df, sort=FALSE, by.x="PCT",
>   by.y="PCT", all.x=TRUE, all.y=TRUE)
>   remove(tx2_df)
>   remove(votes2_df)
>   # notice that the data frame row IDs we print are sequential
>   # 1,2,3,4... and not proper precinct names like 1234....
>   rownames(as(tx2_df1, "data.frame"))  #show us the row IDs

Have you looked at spRbind and spChFIDs methods in maptools? I would be 
worried about ignoring the IDs unless you are very confident that the 
order of the geometric objects and the rows in the data frame are 
identical. Since Peter mentioned our book, there is an extensive example 
on pp. 120-126; the code and data are available in the Chapter 5 set on 
www.asdar-book.org, but without the explanations of the steps involved.

Roger

>
>   # key here is match.ID = FALSE so that it does not try to take
>   # the 1,2,3 data frame sequence numbers and think they are IDs.    # both 
> sp and df must have rows aligned the same.
>   tx3_sp <- SpatialPolygonsDataFrame(as(tx2_sp,"SpatialPolygons"),
>             data=tx2_df1, match.ID = FALSE)
>   remove(tx2_sp)
>   remove(tx2_df1)
>
>   # debug tx3_sp a little, lets make sure its a SpatialPologonsDataFrame!
>   sapply(slot(tx3_sp, "polygons"), function(x) slot(x, "ID")) #what are row 
> "ID"s?
>   str(as(tx3_sp, "data.frame"))         #show representation of variables
>   (str(tx3_sp))                         #shows representation of geometries 
> too.
>   names(tx3_sp)                         #nice but lengthy 
> Let us know if this helps you any.
>
> Good luck,
> Jim Burke
>
>
>
> Peter S. Hayes wrote:
>> Hi All,
>> 
>> I'm trying to teach a spatial analysis class using R as a means of learning 
>> some details... I'm not an expert in R myself, but am learning it while 
>> using it as a 'tool' for lessons in the class...
>> 
>> In getting the students familiar with some of the SP classes, we began 
>> looking at the class components and manipulating some of the components 
>> (such as translating by modifying coordinates..).
>> 
>> Is there a means of decomposing SpatialPolygons to access the list of 
>> Polygons and contained classes? The help files (for example, polygons()) 
>> appear to hint so, but not function so... the SpatialPolygons isn't quite a 
>> traditional R dataframe and won't flatten with a call to as.data.frame() to 
>> allow access to the individual slots... but there must be a means of doing 
>> that...
>> 
>> Also, I have "Applied Spatial Data Analysis with R" as one of the texts... 
>> any thoughts for more references... especially for non-programmers: I've 
>> some software experience (C/C++...) but most of the students are 
>> environmental study, environmental science, or biology students and this is 
>> one of their first experiences with anything having a command line. We've 
>> been taking things slow, but R is still a bit cryptic... any thoughts on 
>> that would be appreciated!
>> 
>> Thank you for all!
>> 
>> 
>> Pete
>> 
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> 
>> 
>
> _______________________________________________
> 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