user9302275
user9302275

Reputation:

Converting data.frame to SpatialPolygonsDataFrame

Here is the data for reproducible purposes:

  structure(list(countyfp10 = c(1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 5, 
5, 7, 7, 7, 7), id = c(7417, 7418, 7419, 7420, 7421, 7422, 7423, 
7424, 7425, 7426, 7427, 7428, 7429, 7430, 7431, 7432), lat = c(39.4797245, 
39.5544678, 39.4681687, 39.199806, 39.4017623, 39.3093943, 39.4272021, 
39.5618129, 39.7934997, 39.4835134, 39.4989196, 39.4819145, 39.4727694, 
39.4675515, 39.4693146, 39.4644503), long = c(-118.7908571, -118.8095638, 
-118.8195712, -118.5429041, -118.754186, -118.8861865, -118.9729817, 
-117.9418517, -118.9516281, -118.8487913, -119.0205114, -118.7695846, 
-118.7938896, -118.76011, -118.7778707, -118.7902103)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -16L), spec = structure(list(
    cols = list(countyfp10 = structure(list(), class = c("collector_double", 
    "collector")), id = structure(list(), class = c("collector_double", 
    "collector")), lat = structure(list(), class = c("collector_double", 
    "collector")), long = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

This is currently store as a data.frame but I would like to convert it into a SpatialPolygonsDataFrame. What would be the best way to do it?

Upvotes: 4

Views: 4529

Answers (2)

Sada93
Sada93

Reputation: 2835

As mentioned in the comments, sf is the most convenient package to use, here is an example.

Note the data set was modified to form a closed polygon, if we try it with the original data, it throws an error.

data = tibble(countyfp10 = c(1, 1, 1, 1,1, 3, 3, 3, 3,3, 5, 5, 5,5,
                              5, 7, 7, 7, 7,7), id = c(7413,7414,7415,7416,7417, 7418, 7419, 7420, 7421, 7422, 7423, 
                                                     7424, 7425, 7426, 7427, 7428, 7429, 7430, 7431, 7432), lat = c(39.4797245, 
                                                                                                                    39.5544678, 39.4681687, 39.199806,39.4797245, 39.4017623, 39.3093943, 39.4272021, 
                                                                                                                    39.5618129,39.4017623, 39.7934997, 39.4835134, 39.4989196, 39.4819145,39.7934997, 39.4727694, 
                                                                                                                    39.4675515, 39.4693146, 39.4644503,39.4727694), lng = c(-118.7908571, -118.8095638, 
                                                                                                                                                                  -118.8195712, -118.5429041,-118.7908571, -118.754186, -118.8861865, -118.9729817, 
                                                                                                                                                                  -117.9418517,-118.754186, -118.9516281, -118.8487913, -119.0205114, -118.7695846,-118.9516281, 
                                                                                                                                                                  -118.7938896, -118.76011, -118.7778707, -118.7902103,-118.7938896))

cords = data%>%
  select(countyfp10,lng,lat)%>%
  mutate(lng = as.numeric(lng),
         lat = as.numeric(lat))%>%
  group_by(countyfp10)%>%
  summarise(coordinates = list(list(matrix(c(lng,lat),ncol = 2)))) %>%
  .$coordinates %>%
  lapply(.,st_polygon) %>%
  st_sfc(.)

cords %>% 
  leaflet() %>%
  addTiles()%>%
  addPolygons()

Upvotes: 1

SymbolixAU
SymbolixAU

Reputation: 26258

I've developed the sfheaders library for this purpose.

devtools::install_github("dcooley/sfheaders")
library(sfheaders)

sf <- sfheaders::sf_polygon(
    obj = df
    , x = "long"
    , y = "lat"
    , polygon_id = "countyfp10"
)

And to show it works in leaflet (other plotting libraries are available ;) )

library(leaflet)
leaflet() %>%
    addTiles() %>%
    addPolygons(data = sf)

enter image description here

Upvotes: 4

Related Questions