Skip to content
Prev 5299 / 29559 Next

Extract coordinates from SpatialPolygonsDataFrame

Hi 

In your case I guess you only need part 1. If there are more than 1 polygon 
you would also need part 2. As far as i know there are no inbuilt functions 
to extract coordinates from a SpatialPolygon. 

##
#part 1
nc <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], 
		    proj4string=CRS("+proj=longlat +datum=NAD27"))

lonlat <- lapply(slot(nc, "polygons"), function(x) lapply(slot(x,
   "Polygons"), function(y) slot(y, "coords")))

#part 2
for (i in 1:dim(summary(lonlat))[1]) {
  size <- length(unlist(lapply(lapply(lonlat[i], lapply, dim), unlist)))
  if (size == 2) { res2 <- data.frame(unlist(lonlat[i], recursive=FALSE))
    } else lonlat.tmp <- unlist(lonlat[i], recursive=FALSE)
  if (size != 1) lonlat2 <- do.call(rbind, lapply(lonlat.tmp, data.frame))
  res2$ID <- i
  if (i == 1) lonlat3 <- lonlat2 else lonlat3 <- rbind(lonlat3, lonlat2)
}

lonlat3
##

Maybe this could be more efficient (part 2), but at least it works.

Best wishes 
Torleif
On Thursday 19 March 2009 10:14:51 pm Tyler Dean Rudolph wrote: