我正在试图提取包含坐标信息的列表到一个sf数据框架中。
这是清单
feature <- list(type = "Feature", properties = list(`_leaflet_id` = 26065L,
feature_type = "polyline"), geometry = list(type = "LineString",
coordinates = list(list(-74.210091, 40.382121), list(-73.942375,
40.661889))))我想把它转换成三列的sf数据(leaflet_id,feature_type,几何学)
我尝试过使用purrr::flatten_dfr(特性),但得到了错误:参数1必须有名称。
我已经复习过其他类似于下面的帖子,这些帖子看上去很有希望,但没有起作用。
谢谢
发布于 2020-10-04 07:57:47
您的feature列表看起来像使用类似于jsonlite::fromJSON()的东西读取geojson的结果。
如果您使用geojson直接读取sf::st_read(),您将得到sf对象。
要按原样修复代码,您可以这样做。
library(jsonlite)
library(sf)
sf <- jsonlite::toJSON( feature, auto_unbox = TRUE ) %>%
sf::st_read()
sf
# Simple feature collection with 1 feature and 2 fields
# geometry type: LINESTRING
# dimension: XY
# bbox: xmin: -74.21009 ymin: 40.38212 xmax: -73.94238 ymax: 40.66189
# z_range: zmin: NA zmax: NA
# m_range: mmin: NA mmax: NA
# geographic CRS: WGS 84
# _leaflet_id feature_type geometry
# 1 26065 polyline LINESTRING (-74.21009 40.38...发布于 2020-10-04 03:46:16
您的列表中有两个问题会引发flatten_dfr错误。
coordinates中的两个列表需要名称。type两次。这是行不通的,因为flatten之后的数据框架将有两个名称相同的列.如果您修复了这些问题,flatten_dfr将毫无错误地运行:
feature <- list(type = "Feature",
properties = list(`_leaflet_id` = 26065L,
feature_type = "polyline"),
geometry = list(type1 = "LineString",
coordinates = list(foo=list(-74.210091, 40.382121),
bar=list(-73.942375,
flatten_dfr(feature)
# A tibble: 2 x 5
type `_leaflet_id` feature_type type1 coordinates
<chr> <int> <chr> <chr> <named list>
1 Feature 26065 polyline LineString <list [2]>
2 Feature 26065 polyline LineString <list [2]> https://stackoverflow.com/questions/64190647
复制相似问题