英文:
Manually parsing geojson into a dataframe
问题
我想逐步将一个.geojson
文件转换为一个tibble(或数据框)。
这是一个简化的geojson示例,我将其存储在一个名为test.geojson
的文件中(请注意,geometry
字段为null
,但在这里并不重要):
{"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "VAR_1": 31,"VAR_2": "abc","VAR_3": 255 }, "geometry" : null },
{ "type": "Feature", "properties": { "VAR_1": 23,"VAR_2": "def","VAR_3": 876 }, "geometry" : null }
]}
期望的结果(在这里假设geometry
填充了两个坐标而不是null
):
# A tibble: 2 x 3
VAR_1 VAR_2 VAR_3 geometry
<dbl> <chr> <dbl> <list>
1 31 abc 255 <dbl [2]>
2 23 def 876 <dbl [2]>
我特别希望得到一个基于{tidyverse}
的解决方案。我目前尝试的是遍历每个features
,尝试构建一个数据框,但我找不到一种好的方法来添加geometry
字段:
# 读取geojson
js <- jsonlite::read_json("test.geojson")
# 遍历每个feature...
map_dfr(1:length(js$features), .f = function(i){
df <- js$features[[i]]$properties # 这个可以工作,但只导入VAR_1、VAR_2、VAR_3
df |> mutate(geometry = js$features[[i]]$geometry) # 这个不起作用
})
注意:可以使用{geojsonsf}
直接将其导入为sf
对象,使用sf <- geojsonsf::geojson_sf("test.geojson")
,但我想逐步进行,并最终得到一个tibble,并理解我在做什么。
非常感谢您的帮助!
英文:
I would like to work step by step to convert a .geojson
into a tibble (or dataframe).
Here's a minimalistic geojson example which I stored in a file called test.geojson
(note that the geometry
field is null
but this does not matter here) :
{"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "VAR_1": 31,"VAR_2": "abc","VAR_3": 255 }, "geometry" : null },
{ "type": "Feature", "properties": { "VAR_1": 23,"VAR_2": "def","VAR_3": 876 }, "geometry" : null }
]}
Desired result (here assuming that geometry
is filled with two coordinates instead of being null
)
# A tibble: 2 x 3
VAR_1 VAR_2 VAR_3 geometry
<dbl> <chr> <dbl> <list>
1 31 abc 255 <dbl [2]>
2 23 def 876 <dbl [2]>
I'd partiularly like a {tidyverse}
based solution. What I've been trying for now is iterate through each features
to try and build a dataframe but I can't find a way to nicely add the geometry
field :
# Read geojson
js <- jsonlite::read_json("test.geojson")
# Iterate through each features ...
map_dfr(1:length(js$features), .f = function(i){
df <- js$features[[i]]$properties # this works but only importing VAR_1, VAR_2, VAR_3
df |> mutate(geometry = js$features[[i]]$geometry) # this does not work
})
Note : One could use {geojsonsf}
to import this directly as sf
object with sf <- geojsonsf::geojson_sf("test.geojson")
but I want to do it step by step, ending up on a tibble and understanding what I'm doing.
Thanks a lot for helping !
答案1
得分: 2
假设数据如下所示:
x <- '{ "type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "VAR_1": 31,"VAR_2": "abc","VAR_3": 255 }, "geometry" : [-74.0060, 40.7128]},
{ "type": "Feature", "properties": { "VAR_1": 23,"VAR_2": "def","VAR_3": 876 }, "geometry" : [-74.0060, 40.7128]}
]}'
你可以这样做:
library(tidyverse)
jsonlite::fromJSON(x)$features %>%
as_tibble() %>%
select(properties, geometry) %>%
unnest(properties)
# 输出结果:
# A tibble: 2 × 4
VAR_1 VAR_2 VAR_3 geometry
<int> <chr> <int> <list>
1 31 abc 255 <dbl [2]>
2 23 def 876 <dbl [2]>
英文:
Assuming the data is something like this:
x <- '{"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "VAR_1": 31,"VAR_2": "abc","VAR_3": 255 }, "geometry" : [-74.0060, 40.7128]},
{ "type": "Feature", "properties": { "VAR_1": 23,"VAR_2": "def","VAR_3": 876 }, "geometry" : [-74.0060, 40.7128]}
]}'
You can do this:
library(tidyverse)
jsonlite::fromJSON(x)$features |>
as_tibble() |>
select(properties, geometry) |>
unnest(properties)
# Output:
# A tibble: 2 × 4
VAR_1 VAR_2 VAR_3 geometry
<int> <chr> <int> <list>
1 31 abc 255 <dbl [2]>
2 23 def 876 <dbl [2]>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论