1
votes

I have a list of lists. I will put an example of the nested data at the bottom of this question. The main list nest_list, has 2 sub-lists. Each of the 2 sub-lists is made up of 2 or 3 matrices of lon/lat coordinates. Each of these lon/lat coordinate matrices makes up a line segment. My end goal is to export all of these line segments as one shapefile, however I am struggling to put my data into a workable format.

I found this helpful post about converting a lat/lon table to a shapefile (Convert table of coordinate to shape file using R). However my data is definitely not in the correct format to do this technique. I would like, nest_list to be one long list where each of the lon/lat matrices is one element of that list.

I've tried to use unnest(nest_list) from tidyr to get my data to do this, but I get an error because nest_list is a list, not a data.frame. I have also tried lapply(nest_list, unlist). But that combines all of the matrices in each of the sub-lists into a long vector, so I end up with two long vectors.

Do you have any ideas of how to unnest my data, resulting in a list where each lat/lon matrix is one element?

Here is my data:

 nest_list = list(list(structure(c(-163.939480000102, -163.932950000242, 
 -163.930539999721, 
 -163.93100000025, -163.933320000218, -163.935640000186, -163.941510000186, 
 -163.947380000187, -163.950175000026, -163.952969999866, -163.952909999797, 
 -163.948820000043, -163.953614999932, -163.958409999822, -163.958460000329, 
 -163.955969999716, -163.948809999582, -163.950620000313, -163.94980000027, 
 -163.945235000195, -163.94067000012, -163.93931771722, 62.4387199999281, 
  62.4343600003138, 62.4286899996438, 62.4238500003773, 62.4198250000238, 
  62.4157999996703, 62.4099199996583, 62.4040399996463, 62.3988099999316, 
  62.3935800002169, 62.387119999988, 62.3795499998327, 62.3751399997113, 
  62.3707299995899, 62.3691199999881, 62.3658599998392, 62.3633900001485, 
  62.3612500003873, 62.3598899997237, 62.3578349998354, 62.3557799999471, 
  62.3541680789363), .Dim = c(22L, 2L), .Dimnames = list(NULL, 
c("X", "Y"))), structure(c(-163.93931771722, -163.938169999946, 
 -163.93740000041, -163.938740000151, -163.944244999957, -163.949749999763, 
 -163.947819999793, -163.939500000125, -163.945110000126, -163.950720000128, 
 -163.956330000129, -163.959765000255, -163.96320000038, -163.967020000273, 
 -163.970840000166, -163.974660000059, -163.978479999952, -163.979979999877,  
 -163.981479999802, -163.985049999858, -163.988619999913, -163.988893564025, 
 62.3541680789363, 62.3528000001199, 62.3498299998546, 62.3447300002894, 
 62.3405950002591, 62.3364600002289, 62.3337599998242, 62.3315399999711, 
 62.3283500000525, 62.325160000134, 62.3219700002154, 62.3189000002849, 
 62.3158300003543, 62.3098650002446, 62.303900000135, 62.2979350000253, 
 62.2919699999156, 62.2861900000185, 62.2804100001214, 62.2773400001909, 
 62.2742700002603, 62.2739300405841), .Dim = c(22L, 2L), .Dimnames = list(
 NULL, c("X", "Y"))), structure(c(-163.988893564025, -163.992389999749, 
 -163.996159999584, -163.996669999721, -163.994829999855, -163.992989999988, 
 -163.994169999995, -163.995350000002, -163.999320000068, -164.003290000133, 
 -164.005879999962, -164.005639999686, -164.001679999632, -163.995054999888, 
 -163.988430000144, -163.980987500023, -163.973544999902, -163.966102499781, 
 -163.958659999659, 62.2739300405841, 62.2695850000478, 62.2648999998352, 
 62.2581699997457, 62.2527699998357, 62.2473699999256, 62.2422650001297, 
 62.2371600003338, 62.2304550000483, 62.2237499997627, 62.2154199996334, 
 62.2137999995704, 62.2114799996024, 62.2112999998452, 62.211120000088, 
 62.2094475000771, 62.2077750000662, 62.2061025000554, 62.2044300000445
 ), .Dim = c(19L, 2L), .Dimnames = list(NULL, c("X", "Y")))), 
list(structure(c(-162.435819999793, -162.430109999976, -162.427880000112, 
-162.425650000247, -162.425349999903, -162.42719999978, -162.428489999914, 
-162.42620000043, -162.421750000262, -162.417300000095, -162.41132999998, 
-162.405359999864, -162.399389999749, -162.390509999887, 
-162.381630000024, -162.37460000004, -162.367570000055, -162.363089999854, 
-162.358609999652, -162.352496666488, -162.346383333324, 
-162.34027000016, -162.333680000232, -162.329452505243, 61.9670499997578, 
61.9634399996563, 61.9593549996835, 61.9552699997108, 61.9520299995849, 
61.9504499995679, 61.9418499995779, 61.9350699998806, 61.9318899998237, 
61.9287099997667, 61.9272066666544, 61.9257033335421, 61.9242000004297, 
61.9244350000253, 61.9246699996208, 61.9266249998439, 61.928580000067, 
61.9307850001277, 61.9329900001884, 61.9375400001709, 61.9420900001534, 
61.946640000136, 61.9500199999731, 61.9521882750156), .Dim = c(24L, 
2L), .Dimnames = list(NULL, c("X", "Y"))), structure(c(-162.329452505243, 
-162.327090000303, -162.321173333582, -162.315256666862, 
-162.309340000141, -162.300583333454, -162.291826666767, 
-162.28307000008, -162.275282500012, -162.267494999944, -162.259707499876, 
-162.251919999808, -162.244499999825, -162.237079999842, 
-162.230319999868, -162.223559999894, -162.21679999992, -162.209842499906, 
-162.202884999893, -162.195927499879, -162.188969999866, 
61.9521882750156, 61.9533999998102, 61.9556199999631, 61.957840000116, 
61.9600600002689, 61.9619966669127, 61.9639333335566, 61.9658700002004, 
61.9665250000538, 61.9671799999071, 61.9678349997605, 61.9684899996138, 
61.9682049999608, 61.9679200003078, 61.9667066667793, 61.9654933332508, 
61.9642799997222, 61.9621174998228, 61.9599549999234, 61.9577925000239, 
61.9556300001245), .Dim = c(21L, 2L), .Dimnames = list(NULL, 
    c("X", "Y")))))
2

2 Answers

0
votes

This will make a set of SpatialLines, turn them into a bare-minimum SpatialLinesDataFrame and then write out a shapefile with the minimum metadata:

library(sp)
library(rgdal)

SpatialLinesDataFrame()

SpatialLines(
  lapply(1:length(nest_list), function(i) {
    Lines(lapply(nest_list[[i]], Line), i)
  })
) -> sl

sldf <- SpatialLinesDataFrame(sl, data.frame(id=sapply(sl@lines, function(x) slot(x, "ID"))))

writeOGR(sldf, layer="lines", dsn="lines.shp", driver="ESRI Shapefile")
1
votes

This should solve your problem:

library(tidyverse)
nest_list %>% 
  flatten()