quicks
quicks

Reputation: 67

Error plotting NYC boroughs using OSM in R

I'm trying to plot the boroughs of NYC using OSM in R.

bb <- getbb("New York City, New York")

boundaries <- opq(bbox = bb) %>% 
  add_osm_feature(key = "boundary", value = "administrative") %>% 
  osmdata_sf() 

boroughs <- boundaries[["osm_multipolygons"]] %>% 
  filter(admin_level == 7)

ggplot() +
  geom_sf(data = boroughs)

This gets: Error in do.call(rbind, x) : variable names are limited to 10000 bytes

When I just write plot(boroughs), I get: enter image description here

When I isolate some of the boroughs (ex. Staten Island) I can plot the polygon. However, some specific boroughs are triggering this message. Specifically, I get this whenever Brooklyn is included. Can anyone explain this error? Besides the area and the literal names, I don't see any difference in variable names for Brooklyn and Staten Island. Can anyone explain what this error is saying in the context and how I should deal with it?

Edit to add reprex after adding unname_osm_sf() per agila's suggestion:

library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(ggplot2)
library(reprex)

# download and extract data
bb <- getbb("New York City, New York")

boundaries <- opq(bbox = bb, timeout = 50) %>% 
  add_osm_feature(key = "boundary", value = "administrative") %>% 
  osmdata_sf() %>% 
  unname_osmdata_sf()
#> Error in FUN(X[[i]], ...): is.numeric(x) is not TRUE

Created on 2020-07-28 by the reprex package (v0.3.0)

sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18362)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] reprex_0.3.0  ggplot2_3.3.2 osmdata_0.1.3 sf_0.9-5     
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.4.6       compiler_4.0.2     pillar_1.4.6       highr_0.8         
#>  [5] class_7.3-17       tools_4.0.2        digest_0.6.25      gtable_0.3.0      
#>  [9] lubridate_1.7.9    jsonlite_1.7.0     evaluate_0.14      lifecycle_0.2.0   
#> [13] tibble_3.0.1       lattice_0.20-41    pkgconfig_2.0.3    rlang_0.4.6       
#> [17] DBI_1.1.0          curl_4.3           yaml_2.2.1         xfun_0.16         
#> [21] e1071_1.7-3        withr_2.2.0        xml2_1.3.2         dplyr_1.0.0       
#> [25] stringr_1.4.0      httr_1.4.2         knitr_1.29         fs_1.4.1          
#> [29] generics_0.0.2     vctrs_0.3.1        classInt_0.4-3     grid_4.0.2        
#> [33] tidyselect_1.1.0   glue_1.4.1         R6_2.4.1           rmarkdown_2.3     
#> [37] sp_1.4-2           purrr_0.3.4        magrittr_1.5       scales_1.1.1      
#> [41] htmltools_0.5.0    ellipsis_0.3.1     units_0.6-7        rvest_0.3.6       
#> [45] colorspace_1.4-1   KernSmooth_2.23-17 stringi_1.4.6      munsell_0.5.0     
#> [49] crayon_1.3.4

Edit to respond to agilia's comment:

opq(bbox = bb, timeout = 50) %>% 
   add_osm_feature(key = "boundary", value = "administrative") %>% 
   osmdata_sf()
Object of class 'osmdata' with:
                 $bbox : 40.477399,-74.25909,40.9161785,-73.7001809
        $overpass_call : The call submitted to the overpass API
                 $meta : metadata including timestamp and version numbers
           $osm_points : 'sf' Simple Features Collection with 55055 points
            $osm_lines : 'sf' Simple Features Collection with 1937 linestrings
         $osm_polygons : 'sf' Simple Features Collection with 28 polygons
       $osm_multilines : NULL
    $osm_multipolygons : 'sf' Simple Features Collection with 229 multipolygons

Upvotes: 0

Views: 588

Answers (1)

agila
agila

Reputation: 3472

You can find a detailed explanation of the error here.

This should solve the problem:

# packages
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# download and extract data
bb <- getbb("New York City, New York")

boundaries <- opq(bbox = bb) %>% 
  add_osm_feature(key = "boundary", value = "administrative") %>% 
  osmdata_sf() %>% 
  unname_osmdata_sf()

boroughs <- boundaries[["osm_multipolygons"]] %>% 
  filter(admin_level == 7)

plot(st_geometry(boroughs), col = sf.colors(7, categorical = TRUE))

ggplot() +
  geom_sf(data = boroughs, aes(fill = name))

Created on 2020-07-28 by the reprex package (v0.3.0)

Notice that I added unname_osmdata_sf() after osmdata_sf()

Upvotes: 1

Related Questions