Search code examples
rdataframelistgisr-sf

Transform a dataframe with list-columns of coordinates into multiple multipolygons


I have a dataframe where every line is a list of coordinates of a neighbourhood;

t <- structure(list(lng = list(c(-43.265319264, -43.265206066, -43.265037676, 
-43.264643583, -43.2645450269999, -43.263920055, -43.2639192469999, 
-43.263965775, -43.263980804, -43.264317852, -43.264328206, -43.265107563, 
-43.2653157409999, -43.265692949, -43.266565236, -43.266706805, 
-43.2669489259999, -43.2683438619999, -43.268048516, -43.267176575, 
-43.2665180779999, -43.267464548, -43.2670880159999, -43.265996715, 
-43.265660937, -43.26551661, -43.265319264), c(-43.265319264, 
-43.265206066, -43.265037676, -43.264643583, -43.2645450269999, 
-43.263920055, -43.2639192469999, -43.263965775, -43.263980804, 
-43.264317852, -43.264328206, -43.265107563, -43.2653157409999, 
-43.265692949, -43.266565236, -43.266706805, -43.2669489259999, 
-43.2683438619999, -43.268048516, -43.267176575, -43.2665180779999, 
-43.267464548, -43.2670880159999, -43.265996715, -43.265660937, 
-43.26551661, -43.265319264), c(-43.265319264, -43.265206066, 
-43.265037676, -43.264643583, -43.2645450269999, -43.263920055, 
-43.2639192469999, -43.263965775, -43.263980804, -43.264317852, 
-43.264328206, -43.265107563, -43.2653157409999, -43.265692949, 
-43.266565236, -43.266706805, -43.2669489259999, -43.2683438619999, 
-43.268048516, -43.267176575, -43.2665180779999, -43.267464548, 
-43.2670880159999, -43.265996715, -43.265660937, -43.26551661, 
-43.265319264), c(-43.265319264, -43.265206066, -43.265037676, 
-43.264643583, -43.2645450269999, -43.263920055, -43.2639192469999, 
-43.263965775, -43.263980804, -43.264317852, -43.264328206, -43.265107563, 
-43.2653157409999, -43.265692949, -43.266565236, -43.266706805, 
-43.2669489259999, -43.2683438619999, -43.268048516, -43.267176575, 
-43.2665180779999, -43.267464548, -43.2670880159999, -43.265996715, 
-43.265660937, -43.26551661, -43.265319264), c(-43.265319264, 
-43.265206066, -43.265037676, -43.264643583, -43.2645450269999, 
-43.263920055, -43.2639192469999, -43.263965775, -43.263980804, 
-43.264317852, -43.264328206, -43.265107563, -43.2653157409999, 
-43.265692949, -43.266565236, -43.266706805, -43.2669489259999, 
-43.2683438619999, -43.268048516, -43.267176575, -43.2665180779999, 
-43.267464548, -43.2670880159999, -43.265996715, -43.265660937, 
-43.26551661, -43.265319264), c(-43.265319264, -43.265206066, 
-43.265037676, -43.264643583, -43.2645450269999, -43.263920055, 
-43.2639192469999, -43.263965775, -43.263980804, -43.264317852, 
-43.264328206, -43.265107563, -43.2653157409999, -43.265692949, 
-43.266565236, -43.266706805, -43.2669489259999, -43.2683438619999, 
-43.268048516, -43.267176575, -43.2665180779999, -43.267464548, 
-43.2670880159999, -43.265996715, -43.265660937, -43.26551661, 
-43.265319264), c(-43.265319264, -43.265206066, -43.265037676, 
-43.264643583, -43.2645450269999, -43.263920055, -43.2639192469999, 
-43.263965775, -43.263980804, -43.264317852, -43.264328206, -43.265107563, 
-43.2653157409999, -43.265692949, -43.266565236, -43.266706805, 
-43.2669489259999, -43.2683438619999, -43.268048516, -43.267176575, 
-43.2665180779999, -43.267464548, -43.2670880159999, -43.265996715, 
-43.265660937, -43.26551661, -43.265319264), c(-43.265319264, 
-43.265206066, -43.265037676, -43.264643583, -43.2645450269999, 
-43.263920055, -43.2639192469999, -43.263965775, -43.263980804, 
-43.264317852, -43.264328206, -43.265107563, -43.2653157409999, 
-43.265692949, -43.266565236, -43.266706805, -43.2669489259999, 
-43.2683438619999, -43.268048516, -43.267176575, -43.2665180779999, 
-43.267464548, -43.2670880159999, -43.265996715, -43.265660937, 
-43.26551661, -43.265319264), c(-43.265319264, -43.265206066, 
-43.265037676, -43.264643583, -43.2645450269999, -43.263920055, 
-43.2639192469999, -43.263965775, -43.263980804, -43.264317852, 
-43.264328206, -43.265107563, -43.2653157409999, -43.265692949, 
-43.266565236, -43.266706805, -43.2669489259999, -43.2683438619999, 
-43.268048516, -43.267176575, -43.2665180779999, -43.267464548, 
-43.2670880159999, -43.265996715, -43.265660937, -43.26551661, 
-43.265319264), c(-43.265319264, -43.265206066, -43.265037676, 
-43.264643583, -43.2645450269999, -43.263920055, -43.2639192469999, 
-43.263965775, -43.263980804, -43.264317852, -43.264328206, -43.265107563, 
-43.2653157409999, -43.265692949, -43.266565236, -43.266706805, 
-43.2669489259999, -43.2683438619999, -43.268048516, -43.267176575, 
-43.2665180779999, -43.267464548, -43.2670880159999, -43.265996715, 
-43.265660937, -43.26551661, -43.265319264)), lat = list(c(-22.891130748, 
-22.8911411509999, -22.891046197, -22.8907952669999, -22.89078769, 
-22.891165407, -22.891165933, -22.891241755, -22.891230881, -22.891850978, 
-22.891870027, -22.891427452, -22.8916389189999, -22.892022089, 
-22.8926351289999, -22.892664855, -22.8926343209999, -22.8917045229999, 
-22.8911884789999, -22.89174932, -22.8909129579999, -22.890431931, 
-22.8897554919999, -22.890296049, -22.890720254, -22.890984793, 
-22.891130748), c(-22.891130748, -22.8911411509999, -22.891046197, 
-22.8907952669999, -22.89078769, -22.891165407, -22.891165933, 
-22.891241755, -22.891230881, -22.891850978, -22.891870027, -22.891427452, 
-22.8916389189999, -22.892022089, -22.8926351289999, -22.892664855, 
-22.8926343209999, -22.8917045229999, -22.8911884789999, -22.89174932, 
-22.8909129579999, -22.890431931, -22.8897554919999, -22.890296049, 
-22.890720254, -22.890984793, -22.891130748), c(-22.891130748, 
-22.8911411509999, -22.891046197, -22.8907952669999, -22.89078769, 
-22.891165407, -22.891165933, -22.891241755, -22.891230881, -22.891850978, 
-22.891870027, -22.891427452, -22.8916389189999, -22.892022089, 
-22.8926351289999, -22.892664855, -22.8926343209999, -22.8917045229999, 
-22.8911884789999, -22.89174932, -22.8909129579999, -22.890431931, 
-22.8897554919999, -22.890296049, -22.890720254, -22.890984793, 
-22.891130748), c(-22.891130748, -22.8911411509999, -22.891046197, 
-22.8907952669999, -22.89078769, -22.891165407, -22.891165933, 
-22.891241755, -22.891230881, -22.891850978, -22.891870027, -22.891427452, 
-22.8916389189999, -22.892022089, -22.8926351289999, -22.892664855, 
-22.8926343209999, -22.8917045229999, -22.8911884789999, -22.89174932, 
-22.8909129579999, -22.890431931, -22.8897554919999, -22.890296049, 
-22.890720254, -22.890984793, -22.891130748), c(-22.891130748, 
-22.8911411509999, -22.891046197, -22.8907952669999, -22.89078769, 
-22.891165407, -22.891165933, -22.891241755, -22.891230881, -22.891850978, 
-22.891870027, -22.891427452, -22.8916389189999, -22.892022089, 
-22.8926351289999, -22.892664855, -22.8926343209999, -22.8917045229999, 
-22.8911884789999, -22.89174932, -22.8909129579999, -22.890431931, 
-22.8897554919999, -22.890296049, -22.890720254, -22.890984793, 
-22.891130748), c(-22.891130748, -22.8911411509999, -22.891046197, 
-22.8907952669999, -22.89078769, -22.891165407, -22.891165933, 
-22.891241755, -22.891230881, -22.891850978, -22.891870027, -22.891427452, 
-22.8916389189999, -22.892022089, -22.8926351289999, -22.892664855, 
-22.8926343209999, -22.8917045229999, -22.8911884789999, -22.89174932, 
-22.8909129579999, -22.890431931, -22.8897554919999, -22.890296049, 
-22.890720254, -22.890984793, -22.891130748), c(-22.891130748, 
-22.8911411509999, -22.891046197, -22.8907952669999, -22.89078769, 
-22.891165407, -22.891165933, -22.891241755, -22.891230881, -22.891850978, 
-22.891870027, -22.891427452, -22.8916389189999, -22.892022089, 
-22.8926351289999, -22.892664855, -22.8926343209999, -22.8917045229999, 
-22.8911884789999, -22.89174932, -22.8909129579999, -22.890431931, 
-22.8897554919999, -22.890296049, -22.890720254, -22.890984793, 
-22.891130748), c(-22.891130748, -22.8911411509999, -22.891046197, 
-22.8907952669999, -22.89078769, -22.891165407, -22.891165933, 
-22.891241755, -22.891230881, -22.891850978, -22.891870027, -22.891427452, 
-22.8916389189999, -22.892022089, -22.8926351289999, -22.892664855, 
-22.8926343209999, -22.8917045229999, -22.8911884789999, -22.89174932, 
-22.8909129579999, -22.890431931, -22.8897554919999, -22.890296049, 
-22.890720254, -22.890984793, -22.891130748), c(-22.891130748, 
-22.8911411509999, -22.891046197, -22.8907952669999, -22.89078769, 
-22.891165407, -22.891165933, -22.891241755, -22.891230881, -22.891850978, 
-22.891870027, -22.891427452, -22.8916389189999, -22.892022089, 
-22.8926351289999, -22.892664855, -22.8926343209999, -22.8917045229999, 
-22.8911884789999, -22.89174932, -22.8909129579999, -22.890431931, 
-22.8897554919999, -22.890296049, -22.890720254, -22.890984793, 
-22.891130748), c(-22.891130748, -22.8911411509999, -22.891046197, 
-22.8907952669999, -22.89078769, -22.891165407, -22.891165933, 
-22.891241755, -22.891230881, -22.891850978, -22.891870027, -22.891427452, 
-22.8916389189999, -22.892022089, -22.8926351289999, -22.892664855, 
-22.8926343209999, -22.8917045229999, -22.8911884789999, -22.89174932, 
-22.8909129579999, -22.890431931, -22.8897554919999, -22.890296049, 
-22.890720254, -22.890984793, -22.891130748))), row.names = c(NA, 
10L), class = "data.frame")

Data structure:

# A data.frame: 10 × 2
#    lng        lat       
#    <list>     <list>    
#  1 <dbl [27]> <dbl [27]>
#  2 <dbl [27]> <dbl [27]>
#  3 <dbl [27]> <dbl [27]>
#  4 <dbl [27]> <dbl [27]>
#  5 <dbl [27]> <dbl [27]>
#  6 <dbl [27]> <dbl [27]>
#  7 <dbl [27]> <dbl [27]>
#  8 <dbl [27]> <dbl [27]>
#  9 <dbl [27]> <dbl [27]>
# 10 <dbl [27]> <dbl [27]>

I want to transform each row into a multipolygon; so that I can then cross the coordinates with data about the polygons for an interactive map. How do I do that? I tried st_as_sf() but it doesn't work on lists; The Shapefile that I have is a POLYGON(list of numbers) though, so I am confused on what form of data I need to do st_as_sf().


Solution

  • You can use Map() + st_multipolygon() to create a list of multipolygons, and then st_sfc() to get a geometry list-column.

    library(sf)
    
    st_sfc(Map(\(x, y) st_multipolygon(list(list(cbind(x, y)))), t$lng, t$lat))
    
    # Geometry set for 10 features 
    # Geometry type: MULTIPOLYGON
    # Dimension:     XY
    # Bounding box:  xmin: -43.26834 ymin: -22.89266 xmax: -43.26392 ymax: -22.88976
    # CRS:           NA
    # First 5 geometries:
    # MULTIPOLYGON (((-43.26532 -22.89113, -43.26521 ...
    # MULTIPOLYGON (((-43.26532 -22.89113, -43.26521 ...
    # MULTIPOLYGON (((-43.26532 -22.89113, -43.26521 ...
    # MULTIPOLYGON (((-43.26532 -22.89113, -43.26521 ...
    # MULTIPOLYGON (((-43.26532 -22.89113, -43.26521 ...