在R中取消嵌套纬度/经度坐标的嵌套列表?

Posted

技术标签:

【中文标题】在R中取消嵌套纬度/经度坐标的嵌套列表?【英文标题】:Unnest a nested list of lat/lon coordinates in R? 【发布时间】:2017-12-18 20:19:06 【问题描述】:

我有一个列表列表。我将在这个问题的底部放置一个嵌套数据的示例。主列表nest_list,有2个子列表。 2 个子列表中的每一个都由 2 或 3 个 lon/lat 坐标矩阵组成。这些 lon/lat 坐标矩阵中的每一个都构成一条线段。我的最终目标是将所有这些线段导出为一个 shapefile,但是我正在努力将我的数据转换为一种可行的格式。

我发现这篇关于将 lat/lon 表转换为 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)。但这将每个子列表中的所有矩阵组合成一个长向量,所以我最终得到了两个长向量。

您对如何取消嵌套我的数据有任何想法,从而生成一个列表,其中每个纬​​度/经度矩阵都是一个元素?

这是我的数据:

 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")))))

【问题讨论】:

【参考方案1】:

这应该可以解决您的问题:

library(tidyverse)
nest_list %>% 
  flatten()

【讨论】:

感谢您的回复。不幸的是,这并不完全符合我的要求。这可能是我没有更清楚地询问事情的坏处。每个矩阵都有多个纬度/经度对。因此,一条线段由矩阵中的所有 lat lon 对组成。因此,理想情况下,最终结果是一个列表,其中每个矩阵都是列表中的一个项目。 我已经修改了我的答案 - 这更像是它吗? 就是这样!谢谢。 @hrbrmstr 我最终想制作一个 shapefile,但我需要先取消嵌套列表。我想我知道如何获取 shapefile,因为事情没有嵌套! 编辑: 我在下面看到了您的帖子。谢谢!这比我计划的要好得多。【参考方案2】:

这将创建一组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")

【讨论】:

这实际上比我做的要简洁得多。谢谢! 我也有一个“tidyverse”版本,但不知道你是喜欢那个还是 base R ops(而且 base R 是最便携的,所以我选择了那个)。 提供的数据 nest_list 实际上只包含我的完整数据集的前两个子列表(这是 87 个子列表,每个子列表有 2 到 25 个纬度/经度矩阵)。此代码适用于nest_list。但是当我将此代码应用于我的完整数据集时,我得到了一个Error in FUN(X[[i]], ...) : coords must be a two-column matrix。它们具有相同的格式。事实上,nest_list 是通过 nest_list=dput(head(original_list,2)) 创建的。你认为 tidyverse 在更大的数据集上会更好吗? 没关系,我意识到整套格式存在一些问题,所以应该很容易修复!基础 R 运行良好,谢谢!

以上是关于在R中取消嵌套纬度/经度坐标的嵌套列表?的主要内容,如果未能解决你的问题,请参考以下文章

从显示纬度和经度的2个单独列表中制作坐标列表[重复]

2个纬度/经度点(坐标)列表之间的地理/地理空间距离

R - 如何将纬度和经度坐标转换为地址/人类可读的位置?

从R中的已知中心点查找半径内的纬度/经度

将经度和纬度与 R 中的国家/地区匹配

R中具有测地线或大圆距离的空间测地纬度经度聚类的方法