Skip to content

SpatialPolygons decomposition...

5 messages · Peter S. Hayes, Jim Burke, Roger Bivand

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

    # 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:
#
On Fri, 13 Feb 2009, Peter S. Hayes wrote:

            
Perhaps look at the code in the elide methods in maptools - they all 
access and manipulate the coordinate values in Spatial* objects. Usually 
it is a matter of using lapply() on slots containing lists (the stacked-up 
objects in Fig. 2.4, p. 40 in our book). lapply() lets you apply a 
function to each member of a list - so one just steps inwards until one 
gets to the slot with the coordinates in - updating the bbox slot on the 
way out. spTransform methods in rgdal do this too. If you flatten the 
representation, you'd get even worse spaghetti that the present 
representation, which isn't ideal anyway.
You could try Braun & Murdoch: A First Course in Statistical Programming 
with R. Cambridge University Press, Cambridge, 2007, or others on:

http://www.r-project.org/doc/bib/R-books.html

Hope this helps,

Roger

  
    
#
On Fri, 13 Feb 2009, Jim Burke wrote:

            
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

  
    
#
Thank you, everyone!

Let me go walk through some of this, look at the help for some functions 
that I haven't yet looked at, and see what I can make of it all! :-)

I let myself become trapped in a lab class with this issue... found that 
the as.data.frame() worked fine on SpatialPoints, but ran into a problem 
when someone asked to decompose a polygon similarly (I should have 
looked before class, but ran out of time - too many commitments, not 
enough time!).

Thank you again!


Pete
Roger Bivand wrote: