在 R 中取消嵌套 lat/lon 坐标的嵌套列表?
Unnest a nested list of lat/lon coordinates in R?
我有一个列表列表。我将在这个问题的底部放一个嵌套数据的例子。主列表 nest_list
,有 2 个子列表。 2 个子列表中的每一个都由 2 个或 3 个 lon/lat 坐标的矩阵组成。这些 lon/lat 个坐标矩阵中的每一个组成一个线段。我的最终目标是将所有这些线段导出为一个 shapefile,但是我正在努力将我的数据转换为一种可行的格式。
我发现这对 post 关于将 lat/lon table 转换为 shapefile (Convert table of coordinate to shape file using R) 很有帮助。但是我的数据绝对不是正确的格式来执行此技术。我希望 nest_list
成为一个长列表,其中每个 lon/lat 矩阵都是该列表的一个元素。
我尝试使用 tidyr
中的 unnest(nest_list)
来获取我的数据来执行此操作,但我收到错误消息,因为 nest_list
是一个列表,而不是 data.frame.我也试过lapply(nest_list, unlist)
。但这将每个子列表中的所有矩阵组合成一个长向量,所以我最终得到两个长向量。
您是否知道如何取消嵌套我的数据,从而生成一个列表,其中每个 lat/lon 矩阵都是一个元素?
这是我的数据:
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")))))
这应该可以解决您的问题:
library(tidyverse)
nest_list %>%
flatten()
这将生成一组 SpatialLines
,将它们变成最小值 SpatialLinesDataFrame
,然后用最少的元数据写出一个 shapefile:
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")
我有一个列表列表。我将在这个问题的底部放一个嵌套数据的例子。主列表 nest_list
,有 2 个子列表。 2 个子列表中的每一个都由 2 个或 3 个 lon/lat 坐标的矩阵组成。这些 lon/lat 个坐标矩阵中的每一个组成一个线段。我的最终目标是将所有这些线段导出为一个 shapefile,但是我正在努力将我的数据转换为一种可行的格式。
我发现这对 post 关于将 lat/lon table 转换为 shapefile (Convert table of coordinate to shape file using R) 很有帮助。但是我的数据绝对不是正确的格式来执行此技术。我希望 nest_list
成为一个长列表,其中每个 lon/lat 矩阵都是该列表的一个元素。
我尝试使用 tidyr
中的 unnest(nest_list)
来获取我的数据来执行此操作,但我收到错误消息,因为 nest_list
是一个列表,而不是 data.frame.我也试过lapply(nest_list, unlist)
。但这将每个子列表中的所有矩阵组合成一个长向量,所以我最终得到两个长向量。
您是否知道如何取消嵌套我的数据,从而生成一个列表,其中每个 lat/lon 矩阵都是一个元素?
这是我的数据:
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")))))
这应该可以解决您的问题:
library(tidyverse)
nest_list %>%
flatten()
这将生成一组 SpatialLines
,将它们变成最小值 SpatialLinesDataFrame
,然后用最少的元数据写出一个 shapefile:
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")