从R中的geojson FeatureCollection读取单个特征的一般方法

时间:2018-08-22 13:05:43

标签: r geojson

我正在尝试读取包含FeatureCollection的geojson文件。我正在寻找一种将单个要素转换为Spatial对象的通用方法。

具体来说,该文件是由Overpass查询http://overpass-turbo.eu/s/BdB生成的总线路线。我尝试了来自geoj​​sonio库的各种命令组合,例如

x<-geojson_atomize(file_to_geojson(name))

但是它们会给出各种错误(在这种情况下,即使file_to_geojson转换声称成功,“ x无效的JSON”也是如此)。

此问题How to load *part* of a multifeature geojson file in R?的答案提供了一种解决方法,但它对一个特定情况有效,并且需要了解特定geojson的结构。此外,答案是3岁,我认为此后就开发了软件包。

1 个答案:

答案 0 :(得分:2)

如果使用sf软件包,这很容易:

#install.packages("sf)
library(sf)
# I have downloaded the Overpass query as a GeoJSON file
route <- st_read("export.geojson")

结果是一个数据帧,每个要素都表示为一行,因此您可以对其进行子集化(即route [1:3,])以仅提取您感兴趣的要素。 要素属性存储为列,因此您也可以轻松地选择这些属性的子集。

就是这样!

让我们看一下您的特殊情况。

有89个功能:

nrow(route)

[1] 89 

具有12个属性(最后一列,数字13,出现在所有sf对象中,并保存几何数据)

names(route)

[1] "id"                       "X.id"                     "from"                    
 [4] "name"                     "network"                  "public_transport.version"
 [7] "ref"                      "route"                    "source"                  
[10] "to"                       "type"                     "X.relations"             
[13] "geometry"  

看起来像这样:

plot(route)

(除非您使用plot(),否则前十个属性仅显示max.plot = 10

enter image description here

检查数据框,您可以看到有两条折线要素和87点要素-我认为是运输路线和运输站。

summary(route$geometry)
MULTILINESTRING           POINT       epsg:4326   +proj=long... 
              2              87               0               0 

前两行包含线条,其余代表点。

head(route)

Simple feature collection with 6 features and 12 fields
geometry type:  GEOMETRY
dimension:      XY
bbox:           xmin: 20.89744 ymin: 52.21596 xmax: 21.10796 ymax: 52.25929
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
                id             X.id                    from
1 relation/4254149 relation/4254149 PKP Olszynka Grochowska
2 relation/4254150 relation/4254150            Stare Bemowo
3    node/32920674    node/32920674                    <NA>
4   node/209094035   node/209094035                    <NA>
5   node/251880529   node/251880529                    <NA>
6   node/302874515   node/302874515                    <NA>
                                              name      network public_transport.version  ref route
1 Bus 523: PKP Olszynka Grochowska => Stare Bemowo ZTM Warszawa                        2  523   bus
2 Bus 523: Stare Bemowo => PKP Olszynka Grochowska ZTM Warszawa                        2  523   bus
3                                             <NA>         <NA>                     <NA> <NA>  <NA>
4                                             <NA>         <NA>                     <NA> <NA>  <NA>
5                                             <NA>         <NA>                     <NA> <NA>  <NA>
6                                             <NA>         <NA>                     <NA> <NA>  <NA>
                                                    source                      to  type
1 Rozkład jazdy ZTM Warszawa, trasa wygenerowana przez bot            Stare Bemowo route
2 Rozkład jazdy ZTM Warszawa, trasa wygenerowana przez bot PKP Olszynka Grochowska route
3                                                     <NA>                    <NA>  <NA>
4                                                     <NA>                    <NA>  <NA>
5                                                     <NA>                    <NA>  <NA>
6                                                     <NA>                    <NA>  <NA>
                                                                                                                                                                                                                                                                                                                                                  X.relations
1                                                                                                                                                                                                                                                                                                                                                        <NA>
2                                                                                                                                                                                                                                                                                                                                                        <NA>
3 [ { "role": "stop", "rel": 4254149, "reltags": { "from": "PKP Olszynka Grochowska", "name": "Bus 523: PKP Olszynka Grochowska => Stare Bemowo", "network": "ZTM Warszawa", "public_transport:version": "2", "ref": "523", "route": "bus", "source": "Rozkład jazdy ZTM Warszawa, trasa wygenerowana przez bot", "to": "Stare Bemowo", "type": "route" } } ]
4 [ { "role": "stop", "rel": 4254150, "reltags": { "from": "Stare Bemowo", "name": "Bus 523: Stare Bemowo => PKP Olszynka Grochowska", "network": "ZTM Warszawa", "public_transport:version": "2", "ref": "523", "route": "bus", "source": "Rozkład jazdy ZTM Warszawa, trasa wygenerowana przez bot", "to": "PKP Olszynka Grochowska", "type": "route" } } ]
5 [ { "role": "stop", "rel": 4254150, "reltags": { "from": "Stare Bemowo", "name": "Bus 523: Stare Bemowo => PKP Olszynka Grochowska", "network": "ZTM Warszawa", "public_transport:version": "2", "ref": "523", "route": "bus", "source": "Rozkład jazdy ZTM Warszawa, trasa wygenerowana przez bot", "to": "PKP Olszynka Grochowska", "type": "route" } } ]
6 [ { "role": "stop", "rel": 4254150, "reltags": { "from": "Stare Bemowo", "name": "Bus 523: Stare Bemowo => PKP Olszynka Grochowska", "network": "ZTM Warszawa", "public_transport:version": "2", "ref": "523", "route": "bus", "source": "Rozkład jazdy ZTM Warszawa, trasa wygenerowana przez bot", "to": "PKP Olszynka Grochowska", "type": "route" } } ]
                        geometry
1 MULTILINESTRING ((21.10695 ...
2 MULTILINESTRING ((20.89804 ...
3      POINT (21.07858 52.23575)
4      POINT (21.08457 52.24583)
5      POINT (21.07847 52.23509)
6      POINT (21.08454 52.24331)

例如,如果您只想要第一个功能,则可以相应地简化数据框的子集:

myfeature <- route[1,]

您还可能需要一组功能,例如所有行。在这里选择它们很容易,因为我们知道它们在前两行中:

route_lines <- route[1:2,]

plot(route_lines)

enter image description here

或要点:

route_stations <- route[3:89,]

plot(route_stations)

enter image description here

最后,如果要保存提取的特征,请执行以下操作:

st_write(route_stations, "route_stations.geojson")

有关sf可用的非常酷的功能的更多信息,请参见https://cran.r-project.org/web/packages/sf/vignettes/sf1.html