如何将来自FeatureCollection的geojson数据加载到data.frame中?

时间:2018-11-21 13:36:06

标签: r json geojson

您好,我希望使用httr包将英国国家统计局API的geojson FeatureCollection引入data.frame。

library(httr)

HealthGeog <- GET("https://opendata.arcgis.com/datasets/f0095af162f749ad8231e6226e1b7e30_0.geojson")

并获得成功的响应:

> HealthGeog
Response [https://opendata.arcgis.com/datasets/f0095af162f749ad8231e6226e1b7e30_0.geojson]
  Date: 2018-11-21 13:28
  Status: 200
  Content-Type: application/json; charset=utf-8
  Size: 9.59 MB

但是刚开始使用JSON,不确定如何导航到FeatureCollection中的列表并将其加载到data.frame中吗?

1 个答案:

答案 0 :(得分:1)

我们可以使用R空间工具来阅读它,但是请参阅此部分之后的部分,了解为什么您可能不需要:

       const EMPTY_OBJECT = {};                               
       this.getSelectedUser = Reselect.createSelector( 
          this.getUserRecords, 
          (record = EMPTY_OBJECT) => record.selectedUser || EMPTY_OBJECT 
       );

这似乎是一个没有几何形状的GeoJSON文件,因此可能意味着它实际上只是“数据”。这些library(sf) library(tidyverse) health_geog_url <- "https://opendata.arcgis.com/datasets/f0095af162f749ad8231e6226e1b7e30_0.geojson" # don't be one of 'those people' and waste bandwidth that isn't yours: httr::GET( url = health_geog_url, httr::write_disk(basename(health_geog_url)), httr::progress() ) health_geog <- st_read(basename(health_geog_url)) ## Reading layer `OGRGeoJSON' from data source `/Users/bob/Desktop/f0095af162f749ad8231e6226e1b7e30_0.geojson' using driver `GeoJSON' ## replacing null geometries with empty geometries ## Simple feature collection with 32844 features and 10 fields (with 32844 geometries empty) ## geometry type: GEOMETRYCOLLECTION ## dimension: XY ## bbox: xmin: NA ymin: NA xmax: NA ymax: NA ## epsg (SRID): 4326 ## proj4string: +proj=longlat +datum=WGS84 +no_defs health_geog ## Simple feature collection with 32844 features and 10 fields (with 32844 geometries empty) ## geometry type: GEOMETRYCOLLECTION ## dimension: XY ## bbox: xmin: NA ymin: NA xmax: NA ymax: NA ## epsg (SRID): 4326 ## proj4string: +proj=longlat +datum=WGS84 +no_defs ## First 10 features: ## LSOA11CD LSOA11NM CCG18CD CCG18CDH CCG18NM STP18CD ## 1 E01011388 Leeds 019B E38000225 15F NHS Leeds CCG E54000005 ## 2 E01011865 Wakefield 042D E38000190 03R NHS Wakefield CCG E54000005 ## 3 E01011833 Wakefield 025E E38000190 03R NHS Wakefield CCG E54000005 ## 4 E01011390 Leeds 087A E38000225 15F NHS Leeds CCG E54000005 ## 5 E01011866 Wakefield 045B E38000190 03R NHS Wakefield CCG E54000005 ## 6 E01011834 Wakefield 015A E38000190 03R NHS Wakefield CCG E54000005 ## 7 E01011391 Leeds 087B E38000225 15F NHS Leeds CCG E54000005 ## 8 E01011867 Wakefield 042E E38000190 03R NHS Wakefield CCG E54000005 ## 9 E01011835 Wakefield 012A E38000190 03R NHS Wakefield CCG E54000005 ## 10 E01011392 Leeds 087C E38000225 15F NHS Leeds CCG E54000005 ## STP18NM LAD18CD LAD18NM FID geometry ## 1 West Yorkshire E08000035 Leeds 1001 GEOMETRYCOLLECTION EMPTY ## 2 West Yorkshire E08000036 Wakefield 1002 GEOMETRYCOLLECTION EMPTY ## 3 West Yorkshire E08000036 Wakefield 1003 GEOMETRYCOLLECTION EMPTY ## 4 West Yorkshire E08000035 Leeds 1004 GEOMETRYCOLLECTION EMPTY ## 5 West Yorkshire E08000036 Wakefield 1005 GEOMETRYCOLLECTION EMPTY ## 6 West Yorkshire E08000036 Wakefield 1006 GEOMETRYCOLLECTION EMPTY ## 7 West Yorkshire E08000035 Leeds 1007 GEOMETRYCOLLECTION EMPTY ## 8 West Yorkshire E08000036 Wakefield 1008 GEOMETRYCOLLECTION EMPTY ## 9 West Yorkshire E08000036 Wakefield 1009 GEOMETRYCOLLECTION EMPTY ## 10 West Yorkshire E08000035 Leeds 1010 GEOMETRYCOLLECTION EMPTY 端点中的许多端点也都有CSV选项,而这些选项确实做到了:

opendata.arcgis.com

我将使用CSV选项。