[R-sig-Geo] Antw: Creating a spatialPolygonsDataFrame from a data frame

Matteo Mattiuzzi matteo.mattiuzzi at boku.ac.at
Sun Oct 7 18:51:42 CEST 2012


Hi Steve, what about this?


DF <- structure(list(v = c(4L, 4L, 4L, 4L, 4L, 4L, 4L), h = 8:14, ll_lon =
c(-131.0149,
-117.7464, -104.5202, -91.3388, -78.2083, -65.15, -52.1199),
   ll_lat = c(39.7081, 39.7342, 39.7557, 39.7728, 39.7858, 39.7937,
   39.7994), ul_lon = c(-156.8405, -140.7952, -124.8854, -109.0855,
   -93.3822, -77.7862, -62.229), ul_lat = c(49.8983, 49.9394,
   49.9677, 49.9863, 49.9972, 50, 50), ur_lon = c(-140.2398,
   -124.6153, -109.0021, -93.3968, -77.7506, -62.1191, -46.5357
   ), ur_lat = c(50.1258, 50.1159, 50.1047, 50.0921, 50.0754,
   50.0582, 50.0429), lr_lon = c(-117.2848, -104.2354, -91.191,
   -78.1497, -65.0781, -52.0094, -38.9707), lr_lat = c(39.8699,
   39.8624, 39.8554, 39.8489, 39.8411, 39.8337, 39.828)), .Names = c("v",
"h", "ll_lon", "ll_lat", "ul_lon", "ul_lat", "ur_lon", "ur_lat",
"lr_lon", "lr_lat"), row.names = 153:159, class = "data.frame")




SPDF <- list()
id <- matrix(NA,nrow(DF),ncol=2)
colnames(id)<- c("h","v")
      for (u in 1:nrow(DF)) 
{
SPDF[[u]] <- Polygons(list(Polygon(cbind(
c(DF$ll_lon[u], DF$ul_lon[u], DF$ur_lon[u], DF$lr_lon[u], DF$ll_lon[u]),
c(DF$ll_lat[u], DF$ul_lat[u], DF$ur_lat[u], DF$lr_lat[u], DF$ll_lat[u])), 
hole = FALSE)),u)
id[u,] <- c(DF$h[u], DF$v[u]) # probably h/v information can be taken direcly from DF
      }
SPDF <- SpatialPolygons(SPDF,1:length(SPDF),proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
      SPDF <- SpatialPolygonsDataFrame(SPDF, as.data.frame(id),match.ID=FALSE)
plot(SPDF,col="green")




>>> steven mosher  07.10.12 8.18 Uhr >>>
Hi,

  I have a dataframe that has a few hundred rows like this.

DF <- structure(list(v = c(4L, 4L, 4L, 4L, 4L, 4L, 4L), h = 8:14, ll_lon =
c(-131.0149,
-117.7464, -104.5202, -91.3388, -78.2083, -65.15, -52.1199),
    ll_lat = c(39.7081, 39.7342, 39.7557, 39.7728, 39.7858, 39.7937,
    39.7994), ul_lon = c(-156.8405, -140.7952, -124.8854, -109.0855,
    -93.3822, -77.7862, -62.229), ul_lat = c(49.8983, 49.9394,
    49.9677, 49.9863, 49.9972, 50, 50), ur_lon = c(-140.2398,
    -124.6153, -109.0021, -93.3968, -77.7506, -62.1191, -46.5357
    ), ur_lat = c(50.1258, 50.1159, 50.1047, 50.0921, 50.0754,
    50.0582, 50.0429), lr_lon = c(-117.2848, -104.2354, -91.191,
    -78.1497, -65.0781, -52.0094, -38.9707), lr_lat = c(39.8699,
    39.8624, 39.8554, 39.8489, 39.8411, 39.8337, 39.828)), .Names = c("v",
"h", "ll_lon", "ll_lat", "ul_lon", "ul_lat", "ur_lon", "ur_lat",
"lr_lon", "lr_lat"), row.names = 153:159, class = "data.frame")


The columns  are  v and h, two attributes,  and then  4 points in lat and
lon    ll_lon  is lower left lon, lr_lon is lower right,
ul_lon is upper left and ur_lon is upper right.

What I want to do is create a spatialPolygonsDataFrame from this data.
As best as I can figure I have to take the  lon/lat coordinates and put
them in to a matrix format, replicate the first point to close
the ring and create a Polygon, and then Create a Polygons() object with an
ID and then a SpatialPolygon, and then a
SpatialPolygonDataFrame  from a list of the spatial polygons.

What I'm struggling with is how to do this for the several hundred rows
that I have.

Steve

    [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list