将geojson保存为R中的shapefile

问题描述 投票:0回答:1

我从 R 中的此链接读取了文件(Microsoft Office 建筑足迹)

https://minedbuildings.blob.core.windows.net/global-buildings/2023-06-27/global-buildings.geojsonl/RegionName%3DNetherlands/quadkey%3D120200313/part-00138-7b73ad6b-e7c8-4e22- 9fa2-01d140ca911d.c000.csv.gz

解压文件后,我得到一个 csv,我将其转换为 geojson。如何使用 sf 或 terra 将其保存为 shapefile

library(R.utils)
library(terra)
library(sf)
library(geojson)


file_name <- "part-00138-7b73ad6b-e7c8-4e22-9fa2-01d140ca911d.c000.csv.gz"
R.utils::gunzip(file_name))

readLines(file_name) %>% 
 paste(collapse = ", ") %>%
 {paste0('{"type": "FeatureCollection",
         "features": [', ., "]}")} -> j

x <- to_geojson(j)
class(x)

"geofeaturecollection" "geojson"

sf::st_write(x, dsn = getwd(), "temp.shp")
Error in UseMethod("st_write") : 
 no applicable method for 'st_write' applied to an object of class "c('geofeaturecollection', 'geojson')"

terra::writeVector(x, 'temp.shp')
unable to find an inherited method for function ‘writeVector’ for signature ‘"geofeaturecollection", "character"’
r geojson terra
1个回答
0
投票

sf::st_read()
的第一个参数是
dsn
(数据源名称)。文档指出:

解释因驱动程序而异 - 对于某些驱动程序,

dsn
是文件名,但也可能是文件夹,或包含数据库的名称和访问凭据);对于 GeoJSON,
dsn
可能是保存 geojson 数据的字符串。

所以你只需提供你的字符串即可。这是一个更简单、完整的示例,无需解压:

# Read some sample geojson data
x <- readLines("https://dataworks.calderdale.gov.uk/download/24mx6/94l/VenuesNov2022.geojson")

dat <- sf::st_read(x)
#   using driver `GeoJSON'
# Simple feature collection with 13 features and 9 fields
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: -2.098113 ymin: 53.70398 xmax: -1.786043 ymax: 53.74124
# Geodetic CRS:  WGS 84

sf::st_write(dat, "tmp.shp")

或者您的情况:

dat  <- sf::st_read(j)
sf::st_write(dat, "tmp.shp", append = FALSE)
© www.soinside.com 2019 - 2024. All rights reserved.