在 R 中使用 OSM 绘制纽约市自治市镇时出错
Error plotting NYC boroughs using OSM in R
我正在尝试使用 R 中的 OSM 绘制纽约市的行政区。
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)
这得到:do.call(rbind, x) 中的错误:变量名被限制为 10000 字节
当我只写 plot(boroughs) 时,我得到:
当我隔离一些行政区(例如史泰登岛)时,我可以绘制多边形。但是,一些特定的行政区正在触发此消息。具体来说,只要包括布鲁克林,我就会得到这个。谁能解释这个错误?除了区域名称和文字名称外,我看不出布鲁克林和史泰登岛的变量名称有任何区别。任何人都可以解释这个错误在上下文中说的是什么以及我应该如何处理它吗?
根据 agila 的建议,在添加 unname_osm_sf() 之后编辑以添加 reprex:
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
由 reprex package (v0.3.0)
于 2020 年 7 月 28 日创建
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
编辑以回应 agilia 的评论:
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
您可以找到错误的详细解释 here。
这应该可以解决问题:
# 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))
由 reprex package (v0.3.0)
于 2020-07-28 创建
请注意,我在 osmdata_sf()
之后添加了 unname_osmdata_sf()
我正在尝试使用 R 中的 OSM 绘制纽约市的行政区。
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)
这得到:do.call(rbind, x) 中的错误:变量名被限制为 10000 字节
当我只写 plot(boroughs) 时,我得到:
当我隔离一些行政区(例如史泰登岛)时,我可以绘制多边形。但是,一些特定的行政区正在触发此消息。具体来说,只要包括布鲁克林,我就会得到这个。谁能解释这个错误?除了区域名称和文字名称外,我看不出布鲁克林和史泰登岛的变量名称有任何区别。任何人都可以解释这个错误在上下文中说的是什么以及我应该如何处理它吗?
根据 agila 的建议,在添加 unname_osm_sf() 之后编辑以添加 reprex:
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
由 reprex package (v0.3.0)
于 2020 年 7 月 28 日创建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
编辑以回应 agilia 的评论:
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
您可以找到错误的详细解释 here。
这应该可以解决问题:
# 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))
由 reprex package (v0.3.0)
于 2020-07-28 创建请注意,我在 osmdata_sf()
unname_osmdata_sf()