我目前有CSV中的数据,其中有一列名为"Journeyroute"的数据.此列包含以下数据[因大小而被截断]:

{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [-4.095772, 50.409393]}, "properties": {"name": "start"}}, {"type": "Feature", "geometry": null, "properties": {"name": "end"}}, {"type": "Feature", "geometry": {"type": "LineString", "coordinates": [[-4.095772, 50.409393], [-4.095781, 50.409397], [-4.095792, 50.409401], [-4.095965, 50.40971], [-4.096064, 50.410069], [-4.09597, 50.410397]]}, "properties": {"distance": 4027.4, "name": "Raw", "times": [1690900467000, 1690900520000, 1690900522000, 1690900539000, 1690900550000, 1690900569000], "duration": 4923.0}}]}

有5,000行数据.我想要做的是提取线串数据以在R中使用,但我遇到了困难.有谁能帮帮忙吗?

我try 转换为JSON,然后取消嵌套,但出现了一个错误(代码改编自使用Google Earth引擎的其他答案):

new_df <- df %>%
    mutate(geo = map(Journey.Route, ~ jsonlite::fromJSON(.))) %>%
    as.data.frame() %>%
    unnest(geo) %>%
    filter(geo != "FeatureCollection") %>%
    mutate(coord = rep(c("x", "y"))) %>%
    pivot_wider(names_from = coord, values_from = coordinates)

Error in `mutate()`:
ℹ In argument: `coord = rep(c("x", "y"))`.
Caused by error:
! `coord` must be size 5000 or 1, not 2.
Run `rlang::last_trace()` to see where the error occurred.

应为线串坐标的SF几何列.

推荐答案

library(geojsonsf)可以读取Geojson的矢量,因此不需要任何行操作

  • 创建一些数据
json <- '{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [-4.095772, 50.409393]}, "properties": {"name": "start"}}, {"type": "Feature", "geometry": null, "properties": {"name": "end"}}, {"type": "Feature", "geometry": {"type": "LineString", "coordinates": [[-4.095772, 50.409393], [-4.095781, 50.409397], [-4.095792, 50.409401], [-4.095965, 50.40971], [-4.096064, 50.410069], [-4.09597, 50.410397]]}, "properties": {"distance": 4027.4, "name": "Raw", "times": [1690900467000, 1690900520000, 1690900522000, 1690900539000, 1690900550000, 1690900569000], "duration": 4923.0}}]}'

df <- data.frame(json = rep(json, 3))
  • 转换为sf个对象
sf <- geojsonsf::geojson_sf(df$json)
  • 对数据执行任何其他您想要的操作
## Remove empty geometries
sf <- sf[ !sf::st_is_empty(sf), ]

## Extract just the LINESTRINGS
sf <- sf[sf::st_geometry_type(sf) == "LINESTRING", ]

## Convert to a long data.frame
df <- sfheaders::sf_to_df(sf = sf, fill = TRUE)

R相关问答推荐

将第二个图放置在ggplot 2中另一个图的x轴上

R包terra在投影时如何决定模板格栅属性?

插入指示行之间时间间隔的新行

R -模运算后的加法

根据固定值范围在tible中添加新行

R:更新后无法运行控制台

如何对数据集进行逆向工程?

带有叠加饼图系列的Highmap

根据选中三个复选框中的一个或两个来调整绘图

提取一个列表中单个列的重复观察结果R

在R gggplot2中是否有一种方法将绘图轴转换成连续的 colored颜色 尺度?

如何删除最后一个可操作对象

打印XTS对象

以NA为通配符的R中的FULL_JOIN以匹配其他数据中的任何值.Frame

将箭头绘制在图形外部,而不是图形内部

如果COLSUM为>;0,则COLNAME为向量

从数据创建数字的命名列表.R中的框

删除数据帧中特定行号之间的每第三行和第四行

防止正则表达式覆盖以前的语句

禁用时,SelecizeInput将变得不透明