Skip to content

Converting Spatial Polygons to data frame for krige.conv

4 messages · Edzer Pebesma, Tom Gottfried, Ben Madin

#
G'day,

I'm re-running some old models with new data, but in the same location as previously. In the past I had a text file with the border coordinates, but this is no longer anywhere to be found - thus I'm coming unstuck with krige.conv() and the borders option.

I have otherwise moved very happily into using sp classes for everything, so can load a set of boundaries from PostGIS in using rgdal but although I find discussion about this (see below) for SpatialPoints, I am stumped for a method to convert Spatial Polygons to a list of coordinates - I guess I could manually split the object into it's component polygons and then iterate through the coords ... but I was hoping that there might be a method that I've overlooked.

I confess I have also forgotten how I originally created the text file I had - there must have been a method, but I'm not sure I can remember where it was. (> 5years ago). I thought it was maptools, but all the maptools functions seem to read into sp classes...

cheers

Ben
On 25/08/2010, at 8:53 PM, Edzer Pebesma wrote:

            
#
Have you tried
?

It retrieves the polygon centroids as a matrix.
On 05/30/2011 07:13 AM, Ben Madin wrote:

  
    
#
Hi Ben,

do you want the coordinates of all the vertices of your polygons?
Have a look at ?geometry. But this doesn't take you down to a mere list of coordinates. There might 
be a more elegant way, but you can try that little function to achieve this:

     asText.SpatialPolygonsDataFrame <- function(obj){
         coords <- matrix(double(), ncol=3)
         for (Polygons in obj at polygons){
             for (Polygon in Polygons at Polygons){
                 coords <- rbind(coords,
                     cbind(as.integer(Polygons at ID), Polygon at coords),
                     c(as.integer(Polygons at ID), NA, NA))
                 }
             }
         return(coords)
         }

regards,
Tom

Am 30.05.2011 07:13, schrieb Ben Madin:

  
    
#
Thanks Tom,
On 30/05/2011, at 2:24 PM, Tom Gottfried wrote:

            
Perfect. It's where I thought I was going, but it probably would have taken me about two days!

cheers

Ben