将 GeoJSON 转换为 R 中的简单特征
Converting GeoJSON into a Simple Feature in R
我想从 Socrata 存储库中读取空间数据集到 R,然后将其转换为 simple feature object。
数据集由 capital improvement projects 表示为多边形:
上述应用中使用的数据可通过 Socrata Open Data API (SODA) 访问:
library(tibble)
library(dplyr)
library(purrr)
library(sf)
library(RSocrata)
obj <- read.socrata("https://data.seattle.gov/resource/pdbw-sw7q.json") %>% as_tibble()
并且空间数据似乎在 the_geom.coordinates
列中:
# Inspect the object
glimpse(obj)
#> Observations: 113
#> Variables: 13
#> $ creationdate <chr> "2017-06-02T13:51:50.854Z", "2017-06-02T1...
#> $ creator <chr> "Transportation_SeattleCityGIS", "Transpo...
#> $ editdate <chr> "2017-06-02T13:51:50.854Z", "2017-06-02T1...
#> $ editor <chr> "Transportation_SeattleCityGIS", "Transpo...
#> $ globalid <chr> "4a78a16a-9ea4-4a81-8011-ad974c80b357", "...
#> $ objectid <chr> "967", "646", "968", "11862", "11521", "1...
#> $ project_id <chr> "TC36717008", "TC367240", "TC36659003", "...
#> $ projectname <chr> "Safe Routes to School - S Fisher Place S...
#> $ shape_area <chr> "4.53868971176276E-8", "0.000002901627518...
#> $ shape_length <chr> "0.000918371270091608", "0.02024322483978...
#> $ status <chr> "ACTIVE", "ACTIVE", "ACTIVE", "ACTIVE", "...
#> $ the_geom.type <chr> "Polygon", "Polygon", "Polygon", "Polygon...
#> $ the_geom.coordinates <list> [<-122.26983, -122.26984, -122.27015, -1... <-- here
查看最后一列会发现每个多边形都存储为一个数组(或多个多边形的数组列表):
# Inspect the spatial data
obj %>% select(the_geom.type, the_geom.coordinates) %>%
mutate(class = map_chr(the_geom.coordinates, class))
#> # A tibble: 113 x 3
#> the_geom.type the_geom.coordinates class
#> <chr> <list> <chr>
#> 1 Polygon <dbl [1 x 5 x 2]> array
#> 2 Polygon <dbl [1 x 16 x 2]> array
#> 3 Polygon <dbl [1 x 5 x 2]> array
#> 4 Polygon <dbl [1 x 35 x 2]> array
#> 5 Polygon <dbl [1 x 24 x 2]> array
#> 6 Polygon <dbl [1 x 15 x 2]> array
#> 7 Polygon <list [2]> list
#> 8 <NA> <NULL> NULL
#> 9 Polygon <list [2]> list
#> 10 Polygon <dbl [1 x 10 x 2]> array
#> # ... with 103 more rows
obj %>% slice(1) %>% pull
#> [[1]]
#> , , 1
#>
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] -122.2698 -122.2698 -122.2702 -122.2702 -122.2698
#>
#> , , 2
#>
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 47.52145 47.5213 47.52131 47.52145 47.52145
我无法使用 sf
包提供的工具将这些数组转换为多边形:
# Try to convert one row from the `the_geom.coordinates` column
# into a POYLGON or MULTIPOLYGON
obj[1, "the_geom.coordinates"] %>% st_polygon
#> Error in vapply(x, ncol, 0L): values must be length 1,
#> but FUN(X[[1]]) result is length 0
obj[1, "the_geom.coordinates"] %>% st_multipolygon
#> Error in MtrxSetSet(x, dim, type = "MULTIPOLYGON", needClosed = TRUE):
#> polygons not (all) closed
任何有关如何将 obj
转换为 sf
对象的建议都将不胜感激。
您没有地理信息JSON。 URL 似乎得到了一个 JSON 下载,其中几何以某种方式编码。
使用最新的 sf
软件包,您可以:
> d = read_sf("https://data.seattle.gov/resource/pdbw-sw7q.geojson")
>
但是,如果您需要添加 RSocrata API 密钥等,或者您请求的数量很大并且需要对其进行批处理(即一次获取 1000 个),则您必须手动执行此操作。 RSocrata 将尝试通过多个请求获取批处理数据。
RSocrata github 网站上有一个关于地理JSON 功能的开放请求:https://github.com/Chicago/RSocrata/issues/43 但它似乎没有得到很好的接受。
我想从 Socrata 存储库中读取空间数据集到 R,然后将其转换为 simple feature object。
数据集由 capital improvement projects 表示为多边形:
上述应用中使用的数据可通过 Socrata Open Data API (SODA) 访问:
library(tibble)
library(dplyr)
library(purrr)
library(sf)
library(RSocrata)
obj <- read.socrata("https://data.seattle.gov/resource/pdbw-sw7q.json") %>% as_tibble()
并且空间数据似乎在 the_geom.coordinates
列中:
# Inspect the object
glimpse(obj)
#> Observations: 113
#> Variables: 13
#> $ creationdate <chr> "2017-06-02T13:51:50.854Z", "2017-06-02T1...
#> $ creator <chr> "Transportation_SeattleCityGIS", "Transpo...
#> $ editdate <chr> "2017-06-02T13:51:50.854Z", "2017-06-02T1...
#> $ editor <chr> "Transportation_SeattleCityGIS", "Transpo...
#> $ globalid <chr> "4a78a16a-9ea4-4a81-8011-ad974c80b357", "...
#> $ objectid <chr> "967", "646", "968", "11862", "11521", "1...
#> $ project_id <chr> "TC36717008", "TC367240", "TC36659003", "...
#> $ projectname <chr> "Safe Routes to School - S Fisher Place S...
#> $ shape_area <chr> "4.53868971176276E-8", "0.000002901627518...
#> $ shape_length <chr> "0.000918371270091608", "0.02024322483978...
#> $ status <chr> "ACTIVE", "ACTIVE", "ACTIVE", "ACTIVE", "...
#> $ the_geom.type <chr> "Polygon", "Polygon", "Polygon", "Polygon...
#> $ the_geom.coordinates <list> [<-122.26983, -122.26984, -122.27015, -1... <-- here
查看最后一列会发现每个多边形都存储为一个数组(或多个多边形的数组列表):
# Inspect the spatial data
obj %>% select(the_geom.type, the_geom.coordinates) %>%
mutate(class = map_chr(the_geom.coordinates, class))
#> # A tibble: 113 x 3
#> the_geom.type the_geom.coordinates class
#> <chr> <list> <chr>
#> 1 Polygon <dbl [1 x 5 x 2]> array
#> 2 Polygon <dbl [1 x 16 x 2]> array
#> 3 Polygon <dbl [1 x 5 x 2]> array
#> 4 Polygon <dbl [1 x 35 x 2]> array
#> 5 Polygon <dbl [1 x 24 x 2]> array
#> 6 Polygon <dbl [1 x 15 x 2]> array
#> 7 Polygon <list [2]> list
#> 8 <NA> <NULL> NULL
#> 9 Polygon <list [2]> list
#> 10 Polygon <dbl [1 x 10 x 2]> array
#> # ... with 103 more rows
obj %>% slice(1) %>% pull
#> [[1]]
#> , , 1
#>
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] -122.2698 -122.2698 -122.2702 -122.2702 -122.2698
#>
#> , , 2
#>
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 47.52145 47.5213 47.52131 47.52145 47.52145
我无法使用 sf
包提供的工具将这些数组转换为多边形:
# Try to convert one row from the `the_geom.coordinates` column
# into a POYLGON or MULTIPOLYGON
obj[1, "the_geom.coordinates"] %>% st_polygon
#> Error in vapply(x, ncol, 0L): values must be length 1,
#> but FUN(X[[1]]) result is length 0
obj[1, "the_geom.coordinates"] %>% st_multipolygon
#> Error in MtrxSetSet(x, dim, type = "MULTIPOLYGON", needClosed = TRUE):
#> polygons not (all) closed
任何有关如何将 obj
转换为 sf
对象的建议都将不胜感激。
您没有地理信息JSON。 URL 似乎得到了一个 JSON 下载,其中几何以某种方式编码。
使用最新的 sf
软件包,您可以:
> d = read_sf("https://data.seattle.gov/resource/pdbw-sw7q.geojson")
>
但是,如果您需要添加 RSocrata API 密钥等,或者您请求的数量很大并且需要对其进行批处理(即一次获取 1000 个),则您必须手动执行此操作。 RSocrata 将尝试通过多个请求获取批处理数据。
RSocrata github 网站上有一个关于地理JSON 功能的开放请求:https://github.com/Chicago/RSocrata/issues/43 但它似乎没有得到很好的接受。