[R-sig-Geo] Extract coordinates from SpatialPolygonsDataFrame

Stephanie stephajm at gmail.com
Fri Mar 20 22:26:15 CET 2009


Hi all,

I've had to extract coordinates from a SpatialPolygonsDataFrame for my
work as well.

# example from Sp Vignettes (Pebesma & Bivand)
 Sr1 = Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2)))
 Sr2 = Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
 Sr3 = Polygon(cbind(c(4, 4, 5, 10, 4), c(5, 3, 2, 5, 5)))
 Sr4 = Polygon(cbind(c(5, 6, 6, 5, 5), c(4, 4, 3, 3, 4)), hole = TRUE)
 Srs1 = Polygons(list(Sr1), "s1")
 Srs2 = Polygons(list(Sr2), "s2")
 Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
 SpP = SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3)
 plot(SpP, col = 1:3, pbg = "white")
class(SpP)

# extracting coordinates
test<-SpP at polygons[[1]]
test2<-test at Polygons[[1]] # here '1' is the first polygon in your
shapefile (you can loop through them)
test2 at coords

# Works for a SpatialPolygonsDataFrame as well. I've tried it.

Cheers,
Stephanie Melles



More information about the R-sig-Geo mailing list