将“多功能”GeoJSON的功能转换为R空间对象

将“多功能”GeoJSON的功能转换为R空间对象,r,geojson,gdal,rgdal,ropensci,R,Geojson,Gdal,Rgdal,Ropensci,通常,您可以使用trusty readOGR将geojson文件读入R,如图所示 但是,这对于多功能GeoJSON来说是失败的 可复制示例: downloader::download("https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/data/test-multifeature.geojson", "test.geojson") test <- rgdal::readOGR("test.geojson", "OG

通常,您可以使用trusty readOGR将geojson文件读入R,如图所示

但是,这对于多功能GeoJSON来说是失败的

可复制示例:

downloader::download("https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/data/test-multifeature.geojson", "test.geojson")
test <- rgdal::readOGR("test.geojson", "OGRGeoJSON") # fails with:

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Multiple incompatible geometries: wkbPoint: 98; wkbLineString: 660; wkbPolygon: 23
错误消息足够清楚,并指示解决方案:拆分功能。 但是,除了使用正则表达式之外,我不知道如何使用正则表达式

欢迎提出任何意见

令人惊奇的是:,而R甚至看起来都无法读懂它

解决方案的替代方法:

test <- geojsonio::geojson_read("test.geojson")

您可以在命令行上使用ogr2ogr将这个巨大的奇美拉分解为合理的东西:

ogr2ogr -where "OGR_GEOMETRY='LINESTRING'" \
     -f "GeoJSON" lines.geojson  test.geojson
点和多边形也是如此

几年前,有人谈论将OGR_SQL实现到readOGR中,此时您可以从R中执行此操作,但Roger不想这样做,也没有人愿意提供帮助:

创建拆分的geojson文件后,可以将其读入单个rgeos::SpatialCollections对象:

如果要尝试使用geojsonio,则可以使用过滤器从几何体集合中选择给定几何体的列表元素

polygon_features = Filter(
    function(f){f$geometry$type=="Polygon"},
    test$features)
但是,您仍然需要构建一些可以进入单独的R实体的东西……

您可以对各种GDAL函数使用require_geomType参数来提取所需的功能:

library(rgdal)

ogrListLayers("test.geojson")
## [1] "OGRGeoJSON"
## attr(,"driver")
## [1] "GeoJSON"
## attr(,"nlayers")
## [1] 1

# This fails but you can at least see the geoms it whines about
ogrInfo("test.geojson", "OGRGeoJSON")
## Error in ogrInfo("test.geojson", "OGRGeoJSON") : 
##   Multiple incompatible geometries: wkbPoint: 98; wkbLineString: 660; wkbPolygon: 23


ogrInfo("test.geojson", "OGRGeoJSON", require_geomType="wkbPoint")
## NOTE: keeping only 98 wkbPoint of 781 features
##     note that extent applies to all features
## Source: "test.geojson", layer: "OGRGeoJSON"
## Driver: GeoJSON number of rows 781 
##   selected geometry type: wkbPoint with 98 rows
## Feature type: wkbPoint with 2 dimensions
## Extent: (12.48326 41.88355) - (12.5033 41.89629)
## CRS: +proj=longlat +datum=WGS84 +no_defs  
## Number of fields: 78 
##                        name type length typeName
## 1                      area    4      0   String
## 2                   bicycle    4      0   String
## ...
## LONG LIST - 78 total


ogrInfo("test.geojson", "OGRGeoJSON", require_geomType="wkbLineString")
## NOTE: keeping only 660 wkbLineString of 781 features
##     note that extent applies to all features
## Source: "test.geojson", layer: "OGRGeoJSON"
## Driver: GeoJSON number of rows 781 
##   selected geometry type: wkbLineString with 660 rows
## Feature type: wkbLineString with 2 dimensions
## Extent: (12.48326 41.88355) - (12.5033 41.89629)
## CRS: +proj=longlat +datum=WGS84 +no_defs  
## Number of fields: 78 
##                        name type length typeName
## 1                      area    4      0   String
## 2                   bicycle    4      0   String
## ...
## LONG LIST - 78 total (same as above)


ogrInfo("test.geojson", "OGRGeoJSON", require_geomType="wkbPolygon")
## NOTE: keeping only 23 wkbPolygon of 781 features
##     note that extent applies to all features
## Source: "test.geojson", layer: "OGRGeoJSON"
## Driver: GeoJSON number of rows 781 
##   selected geometry type: wkbPolygon with 23 rows
## Feature type: wkbPolygon with 2 dimensions
## Extent: (12.48326 41.88355) - (12.5033 41.89629)
## CRS: +proj=longlat +datum=WGS84 +no_defs  
## Number of fields: 78 
##                        name type length typeName
## 1                      area    4      0   String
## 2                   bicycle    4      0   String
## ...
## LONG LIST - 78 total (same as above)


points <- readOGR("test.geojson", "OGRGeoJSON", require_geomType="wkbPoint")
## OGR data source with driver: GeoJSON 
## Source: "test.geojson", layer: "OGRGeoJSON"
## with 781 features;
## Selected wkbPoint feature type, with 98 rows
## It has 78 fields
## NOTE: keeping only 98 wkbPoint of 781 features

lines <- readOGR("test.geojson", "OGRGeoJSON", require_geomType="wkbLineString")
## OGR data source with driver: GeoJSON 
## Source: "test.geojson", layer: "OGRGeoJSON"
## with 781 features;
## Selected wkbLineString feature type, with 660 rows
## It has 78 fields
## NOTE: keeping only 660 wkbLineString of 781 features

polygons <- readOGR("test.geojson", "OGRGeoJSON", require_geomType="wkbPolygon")
## OGR data source with driver: GeoJSON 
## Source: "test.geojson", layer: "OGRGeoJSON"
## with 781 features;
## Selected wkbPolygon feature type, with 23 rows
## It has 78 fields
## NOTE: keeping only 23 wkbPolygon of 781 features

# prove they red in things
plot(lines, col="#7f7f7f")
plot(polygons, add=TRUE)
plot(points, add=TRUE, col="red")

几年后,有两种选择——librarygeojsonsf和librarysf都将读取geojson并转换为sf对象


这是rgdal的最新版本吗?我在0.9-1中看不到这一点-刚刚升级到0.9-3,现在我有了它!我想是在0.9-2的时候。它非常有用,特别是对于那些酷酷的孩子们现在使用的疯狂GeoJSON形状文件-凉的我使用R/gdal进行的多功能/GeoJSON探索的摘要可以在这里找到:。如果你使用Ubuntu,这可以更新gdal:$sudo add apt repository ppa:ubuntugis/ubuntugis unstable&&sudo apt get update$sudo apt get install gdal bin
library(rgdal)

ogrListLayers("test.geojson")
## [1] "OGRGeoJSON"
## attr(,"driver")
## [1] "GeoJSON"
## attr(,"nlayers")
## [1] 1

# This fails but you can at least see the geoms it whines about
ogrInfo("test.geojson", "OGRGeoJSON")
## Error in ogrInfo("test.geojson", "OGRGeoJSON") : 
##   Multiple incompatible geometries: wkbPoint: 98; wkbLineString: 660; wkbPolygon: 23


ogrInfo("test.geojson", "OGRGeoJSON", require_geomType="wkbPoint")
## NOTE: keeping only 98 wkbPoint of 781 features
##     note that extent applies to all features
## Source: "test.geojson", layer: "OGRGeoJSON"
## Driver: GeoJSON number of rows 781 
##   selected geometry type: wkbPoint with 98 rows
## Feature type: wkbPoint with 2 dimensions
## Extent: (12.48326 41.88355) - (12.5033 41.89629)
## CRS: +proj=longlat +datum=WGS84 +no_defs  
## Number of fields: 78 
##                        name type length typeName
## 1                      area    4      0   String
## 2                   bicycle    4      0   String
## ...
## LONG LIST - 78 total


ogrInfo("test.geojson", "OGRGeoJSON", require_geomType="wkbLineString")
## NOTE: keeping only 660 wkbLineString of 781 features
##     note that extent applies to all features
## Source: "test.geojson", layer: "OGRGeoJSON"
## Driver: GeoJSON number of rows 781 
##   selected geometry type: wkbLineString with 660 rows
## Feature type: wkbLineString with 2 dimensions
## Extent: (12.48326 41.88355) - (12.5033 41.89629)
## CRS: +proj=longlat +datum=WGS84 +no_defs  
## Number of fields: 78 
##                        name type length typeName
## 1                      area    4      0   String
## 2                   bicycle    4      0   String
## ...
## LONG LIST - 78 total (same as above)


ogrInfo("test.geojson", "OGRGeoJSON", require_geomType="wkbPolygon")
## NOTE: keeping only 23 wkbPolygon of 781 features
##     note that extent applies to all features
## Source: "test.geojson", layer: "OGRGeoJSON"
## Driver: GeoJSON number of rows 781 
##   selected geometry type: wkbPolygon with 23 rows
## Feature type: wkbPolygon with 2 dimensions
## Extent: (12.48326 41.88355) - (12.5033 41.89629)
## CRS: +proj=longlat +datum=WGS84 +no_defs  
## Number of fields: 78 
##                        name type length typeName
## 1                      area    4      0   String
## 2                   bicycle    4      0   String
## ...
## LONG LIST - 78 total (same as above)


points <- readOGR("test.geojson", "OGRGeoJSON", require_geomType="wkbPoint")
## OGR data source with driver: GeoJSON 
## Source: "test.geojson", layer: "OGRGeoJSON"
## with 781 features;
## Selected wkbPoint feature type, with 98 rows
## It has 78 fields
## NOTE: keeping only 98 wkbPoint of 781 features

lines <- readOGR("test.geojson", "OGRGeoJSON", require_geomType="wkbLineString")
## OGR data source with driver: GeoJSON 
## Source: "test.geojson", layer: "OGRGeoJSON"
## with 781 features;
## Selected wkbLineString feature type, with 660 rows
## It has 78 fields
## NOTE: keeping only 660 wkbLineString of 781 features

polygons <- readOGR("test.geojson", "OGRGeoJSON", require_geomType="wkbPolygon")
## OGR data source with driver: GeoJSON 
## Source: "test.geojson", layer: "OGRGeoJSON"
## with 781 features;
## Selected wkbPolygon feature type, with 23 rows
## It has 78 fields
## NOTE: keeping only 23 wkbPolygon of 781 features

# prove they red in things
plot(lines, col="#7f7f7f")
plot(polygons, add=TRUE)
plot(points, add=TRUE, col="red")
url <- 'https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/data/test-multifeature.geojson'

## these give the same result
sf <- geojsonsf::geojson_sf( url )
sf <- sf::st_read( url )
library(mapdeck)

set_token( "MAPBOX_TOKEN" )

mapdeck( style = mapdeck_style("light") ) %>%
    add_sf( sf )