0

I have a sf style data frame with this structure:

Classes ‘sf’ and 'data.frame':  1672 obs. of  3 variables:
 '$ grid.100:sfc_POLYGON of length 1672; first list element: List of 1
 ' ..$ : num [1:5, 1:2] 542829 543129 543129 542829 542829 ...
  ..- attr(*, "class")= chr  "XY" "POLYGON" "sfg"
' $ id      : int  1 2 3 4 5 6 7 8 9 10 ...
' $ count   : int  0 0 0 0 0 0 0 0 1 0 ...
 - attr(*, "sf_column")= chr "grid.100"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA
  ..- attr(*, "names")= chr  "id" "count"

What I want to do is extract the individual coordinates from the POLYGON feature, so each side of the polygon is it's own column in the data frame.

Any thoughts?

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • An sf polygon is a matrix of X and Y values; you have three. You can extract them, e.g. with `st_coordinates`, but short of turning it into a graph you can't put it in the format you want, as it's a matrix of points, not sides. – alistaire Sep 24 '18 at 19:17
  • @alistaire what if one of the listed columns was COORDINATES, ie: a list of 1 x and 1 y coordinates? Could you cast those to columns? – beavertrapper07 Sep 24 '18 at 19:19
  • You really need to make [a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), though that can be tricky with sf. Using the `nc` data (`nc <- st_read(system.file("shape/nc.shp", package="sf"))`) would make it easier. – alistaire Sep 24 '18 at 19:54

1 Answers1

1

I figured this out I think:

grid.sff <- grid.sf %>%
  dplyr::mutate(latt = sf::st_coordinates(grid.sf$center)[,1]) %>%
  dplyr::mutate(lonn = sf::st_coordinates(grid.sf$center)[,2])