将数据转换为 geojson 错误

Convert data to geojson error

我正在尝试使用 geojsonio 包从 data.frames 从其他 R 包中提取一些 geojson 文件。

library(ggplot2)
library(geojsonio)
us_state <- map_data('state')

geojson_write(us_state, 
              geometry="polygon", 
              grouping="group", 
              file="path/file.geojson")

我遇到的问题是 geometry=polygon 参数。我收到以下错误:

Error in .subset2(x, i, exact = exact) : 
  attempt to select less than one element in integerOneIndex

geometry=point 它工作正常,但当然我只有一百万个独立的点而不是 geojson 文件中的状态多边形。

有什么想法吗?

编辑:

如果我先使用 file<-geojson_json(data.frame),然后使用 geojson_write(file)

,我可以获得一个有效的 geojson 文件

您只需要使用正确的参数名称 group,而不是 grouping。由于该函数有一个 ...,因此可以传入一个错误的参数名称而不会抛出任何 errors/etc。

library(ggplot2)
library(geojsonio)
us_state <- map_data('state')
geojson_write(us_state, 
          geometry = "polygon", 
          group = "group", 
          file = "file.geojson")

#> {
#> "type": "FeatureCollection",
#>                                                                                 
#> "features": [
#> { "type": "Feature", "id": 0, "properties": { "dummy": 0.000000 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -87.462005615234375, 30.389680862426758 ], [ -87.484931945800781, 30.372491836547852 ], [ -87.525032043457031, 30.372491836547852 ], [ -87.53076171875, 30.332386016845703 ], [ -87.570869445800781, 30.326654434204102 ], [ -87.588058471679688, 30.326654434204102 ], [ -87.593788146972656, 30.309467315673828 ], [ -87.593788146972656, 30.286548614501953 ], [ -8
#> ... cutoff