我从EnergyData下载了一个伊拉克电网的geojson文件。您可以自己下载这里文件,并访问网页这里。
我试图使用geojsonio::geojson_read将文件读入R中,但是它抛出了错误。另一位用户帮助指出,能源数据网站提供的文件格式不正确,不符合geojson格式,因此文件名具有误导性。所以他们推荐我使用jsonlite::read_json。虽然该函数确实将文件读取到R中,但它不是空间对象。这是一个多层次的名单。我也尝试过sf::st_read,但是对象是一个凌乱的数据框架。我需要我的最后一个对象是一个空间线数据帧,这样我就可以将它与另一个空间行对象合并并裁剪,并写出生成的shapefile。但是,当我尝试sp::SpatialLinesDataFrame时,我无法正确地索引到对象中,以正确的顺序连接这些行。我需要在R中的对象看起来像在网页上的图片,而不是它只是一个热的混乱。
这是我的密码:
library(geojsonio)
library(jsonlite)
library(sf)
library(sp)
Iraq_electric_json <- jsonlite::read_json("Filepath/electric-network-iraq.geojson")
Iraq_electric_df <- sf::st_read("Filepath/electric-network-iraq.geojson")
# ERRORS HERE
Sldf <- SpatialLinesDataFrame(data = Iraq_electric_df$geometry)我在这里发布了我问题的第一部分(如何在geojson文件中阅读):
但是由于问题的第2部分(更改对象格式)与第1部分非常不同,所以我决定发布一个全新的问题。
发布于 2021-12-29 20:58:37
这是一个很长的过程,因为解决方案主要是研究如何处理这个文件。这里有一些潜在的问题:
nodes和interconnection属性是数组,我认为R函数不知道如何处理这些属性;sf函数知道如何将它们作为列表列来读取,但这对于geojsonio函数可能是不可用的。最后一点很重要,因为它阻止您使用sp::Spatial*类--这些类只适用于单个几何类型(SpatialGrid、SpatialPolygons等)。这个文件将点和线混合在一起,所以您需要类似sf这样的东西,它可以容纳混合几何类型。
我试着阅读了一些方法,其中一些返回列表可以进一步挖掘,还有一些返回sf对象:
library(dplyr)
library(sf)
path <- "https://development-data-hub-s3-public.s3.amazonaws.com/ddhfiles/145188/electric-network-iraq.geojson"
# sf
iraq1 <- st_read(path, drivers = "GeoJSON")
#> options: GeoJSON
#> Reading layer `electric-network-iraq' from data source
#> `https://development-data-hub-s3-public.s3.amazonaws.com/ddhfiles/145188/electric-network-iraq.geojson'
#> using driver `GeoJSON'
#> Simple feature collection with 48 features and 6 fields
#> Geometry type: GEOMETRY
#> Dimension: XY
#> Bounding box: xmin: 40.99849 ymin: 30.50785 xmax: 47.85893 ymax: 37.88342
#> Geodetic CRS: WGS 84
# list
iraq2 <- jsonlite::read_json(path)
str(iraq2, max.level = 1)
#> List of 2
#> $ type : chr "FeatureCollection"
#> $ features:List of 48
# list
iraq3 <- readLines(path) %>%
geojson::to_geojson() %>%
jsonlite::fromJSON()
#> Warning in readLines(path): incomplete final line found on 'https://development-
#> data-hub-s3-public.s3.amazonaws.com/ddhfiles/145188/electric-network-
#> iraq.geojson'
#> Registered S3 method overwritten by 'geojsonlint':
#> method from
#> print.location dplyr
# sf, same warning about incomplete final line
iraq4 <- suppressWarnings(geojsonsf::geojson_sf(path))
#> Registered S3 method overwritten by 'geojsonsf':
#> method from
#> print.geojson geojson我将继续处理第一个对象,一个混合几何类型的sf对象(只是泛型的GEOMETRY)。请注意,有些是行,有些是点,2列是字符串的列表。
head(iraq1)
#> Simple feature collection with 6 features and 6 fields
#> Geometry type: GEOMETRY
#> Dimension: XY
#> Bounding box: xmin: 41.12771 ymin: 36.34583 xmax: 43.12683 ymax: 37.88342
#> Geodetic CRS: WGS 84
#> transmissionPower lineType name nodeType nodes
#> 1 400 single <NA> <NA> Batman, Zakho
#> 2 NA <NA> Zakho city
#> 3 400 single <NA> <NA> Mosul Dam, Zakho
#> 4 NA <NA> Mosul Dam dam
#> 5 400 double <NA> <NA> Mosul, Mosul Dam
#> 6 NA <NA> Mosul city
#> interconnection geometry
#> 1 Syria, Turkey LINESTRING (41.12771 37.883...
#> 2 POINT (42.68304 37.14215)
#> 3 LINESTRING (42.68304 37.142...
#> 4 POINT (42.83355 36.62586)
#> 5 LINESTRING (42.83355 36.625...
#> 6 POINT (43.12683 36.34583)
plot(iraq1["geometry"])

如果您确实需要一个sp::Spatial对象,则必须将点和线分开。我根据维数(点为0,行为1)将它们拆分,并写出现在可读的GeoJSON文件,然后将它们读入。最后,您将得到一个SpatialPointsDataFrame和一个SpatialLinesDataFrame。您不必将它们写入文件;您只需将每个数据帧传递给sf::as_Spatial并获得相同的sp类。
iraq1 %>%
mutate(dimension = st_dimension(geometry)) %>%
split(.$dimension) %>%
purrr::iwalk(~geojsonio::geojson_write(.x, file = sprintf("iraq_repaired_dim%s.geojson", .y)))
iraq_pts <- geojsonio::geojson_read("iraq_repaired_dim0.geojson", what = "sp")
iraq_lines <- geojsonio::geojson_read("iraq_repaired_dim1.geojson", what = "sp")
class(iraq_pts)
#> [1] "SpatialPointsDataFrame"
#> attr(,"package")
#> [1] "sp"
class(iraq_lines)
#> [1] "SpatialLinesDataFrame"
#> attr(,"package")
#> [1] "sp"https://stackoverflow.com/questions/70523047
复制相似问题