gpt4 book ai didi

R:从 GeoJson 到 DataFrame?

转载 作者:行者123 更新时间:2023-12-05 00:12:03 25 4
gpt4 key购买 nike

我有一个秘鲁的 GeoJson 文件,它是州(西类牙语为 Departamentos)。

我可以使用传单绘制秘鲁的州,但由于 GeoJson 文件没有我需要的所有数据,我正在考虑将其转换为 data.frame 添加我需要的数据列,然后将其返回到 GeoJson 格式进行绘图。

数据:您可以从这里下载秘鲁的 GeoJson 数据:

这是我正在使用的数据,我需要将它添加到一个销售列,每个州都有一行(“NOMBDEP” - 总共 24 个)

library(leaflet)
library(jsonlite)
library(dplyr)


states <- geojsonio::geojson_read("https://raw.githubusercontent.com/juaneladio/peru-geojson/master/peru_departamental_simple.geojson", what = "sp")

我想使用“jsonlite”包将“GeoJson”转换为数据框,但收到此错误:
library(jsonlite)

states <- fromJSON(states)

Error: Argument 'txt' must be a JSON string, URL or file.

我期待在拥有数据框后,我可以执行以下操作:
states$sales #sales is a vector with the sales for every department

states <- toJson(states)

最佳答案

您可以使用 library(geojsonsf)往返于 GeoJSON 和 sf

library(geojsonsf)
library(sf) ## for sf print methods

states <- geojsonsf::geojson_sf("https://raw.githubusercontent.com/juaneladio/peru-geojson/master/peru_departamental_simple.geojson")

states$myNewValue <- 1:nrow(states)

geo <- geojsonsf::sf_geojson(states)

substr(geo, 1, 200)
# [1] "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"COUNT\":84,\"FIRST_IDDP\":\"01\",\"HECTARES\":3930646.567,\"NOMBDEP\":\"AMAZONAS\",\"myNewValue\":1},\"geometry\":{\"type\":\"Polygon\",\"coordinat"


.

你可以看到 myNewValue在 GeoJSON 中

关于R:从 GeoJson 到 DataFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com