2017-06-26 3 views
3

Ich möchte einen räumlichen Datensatz aus einem Socrata-Repository in R lesen und dann in eine simple feature object konvertieren.Konvertieren von GeoJSON in ein einfaches Feature in R

Der Datensatz besteht aus capital improvement projects als Polygone dargestellt werden: enter image description here

Die Daten in der obigen app verwendet sind über eine 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() 

und die räumlichen Daten erscheinen

# 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 

Wirft man einen Blick auf: in der the_geom.coordinates Spalte zu sein Die letzte Spalte zeigt, dass jedes Polygon als ein Array (oder eine Liste von Arrays für Multipolygone) gespeichert ist:

# 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 

I nicht in der Lage gewesen, diese Arrays in Polygone zu transformieren unter Verwendung der durch die sf Pakete bereitgestellt Werkzeuge:

# 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 

Irgendwelche Ratschläge, wie man obj in ein sf Objekt umwandelt, würde sehr geschätzt werden.

+2

Es sieht so aus, als ob dies eher eine R-Frage als eine Socrata-Frage ist. Ich bin kein R Experte, aber wenn die Frage mehr über die SODA APIs wird, lass es mich wissen! – chrismetcalf

Antwort

7

Sie haben nicht geoJSON. Diese URL scheint einen JSON-Download mit der irgendwie kodierten Geometrie zu erhalten.

Mit der neuesten sf Paket können Sie tun:

> d = read_sf("https://data.seattle.gov/resource/pdbw-sw7q.geojson") 
> 

Allerdings, wenn Sie RSocrata API-Schlüssel usw. hinzufügen müssen, oder Sie werden eine große Anzahl und müssen Charge es (dh erhalten 1000 bei antrag Zeit), dann müssen Sie das manuell tun. RSocrata wird versuchen, Daten mit mehreren Anfragen zu erhalten.

Es gibt eine offene Anfrage auf der RSocrata Github-Website für geoJSON-Funktionalität: https://github.com/Chicago/RSocrata/issues/43, aber es schien nicht sehr gut erhalten.

Verwandte Themen