我有一个秘鲁的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)
答案 0 :(得分:3)
您可以使用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中
答案 1 :(得分:2)