7

ここreadOGRに示すように、通常、trusty を使用して 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

エラー メッセージは明確で、解決策を示しています: フィーチャを分割します。ただし、これを正規表現で行う以外に、その方法はわかりません。

どんなアイデアでも大歓迎です。

驚くべきこと: GitHub はデータをブラウザーにネイティブに表示しますが、R は (一見) データを読み込むことさえできません!

解決策の代替方法:

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

3 に答える 3

9

さまざまな 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")

ここに画像の説明を入力

于 2015-06-03T11:26:37.347 に答える
3

コマンドラインで ogr2ogr を使用して、この巨大なキメラを理にかなったものに分割できます。

ogr2ogr -where "OGR_GEOMETRY='LINESTRING'" \
     -f "GeoJSON" lines.geojson  test.geojson

ポイントとポリゴンについても同様です。

数年前、OGR_SQL をreadOGRに実装することについておしゃべりがあり、その時点で R からこれを実行できるようになりましたが、Roger はそれをやりたがらず、誰も助けようとしませんでした :(

分割された geojson ファイルを作成したら、それらを 1 つのrgeos::SpatialCollectionsオブジェクトに読み込むことができます。

points=readOGR("points.geojson","OGRGeoJSON")
polys=readOGR("polygons.geojson","OGRGeoJSON")
lines=readOGR("lines.geojson","OGRGeoJSON")
require(rgeos)
g = SpatialCollections(points=points, lines=lines, polygons=polys)
plot(g)

で何かを試してみたい場合は、geojsonioを使用Filterして、ジオメトリ コレクションから特定のジオメトリのリスト要素を選択できます。

polygon_features = Filter(
    function(f){f$geometry$type=="Polygon"},
    test$features)

ただし、別の R エンティティに入れることができるものを構築する必要があります....

于 2015-06-02T10:07:12.143 に答える