[R-sig-Geo] Extract coordinates from SpatialPolygonsDataFrame
Torleif Markussen Lunde
torleif.lunde at cih.uib.no
Thu Mar 19 22:47:00 CET 2009
a small correction.
in the loop;
replace if (size != 1) lonlat2 ...
with
if (size != 2) lonlat2 ...
best
Torleif
On Thursday 19 March 2009 10:37:42 pm Torleif Markussen Lunde wrote:
> 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:
> > Hi there,
> >
> > Nestled deep within the "polygons" slot of my SpatialPolygonsDataFrame
> > object is a list of xy coordinates I would like to extract. Using
> > coordinates() only produces a single xy pair associated with the polygon
> > (1st entry? Centroid?) and I cannot for the life of me manage another
> > way, though I presume it is far from impossible. Could someone please
> > direct me on this?
> >
> > Cheers
> > Tyler
> >
> > [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > 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
--
Torleif Markussen Lunde
Centre for International Health/
Bjernes Centre for Climate Research
University of Bergen, Norway
More information about the R-sig-Geo
mailing list