相交两个 shapefile 后:`vapply(g2, st_is_empty, logical(1))` 中的错误
After intersecting two shapefiles: `Error in vapply(g2, st_is_empty, logical(1))`
我运行以下:
library(dplyr)
library(sf)
library(tigris)
library(tmap)
options(tigris_class = 'sf')
options(tigris_use_cache = TRUE)
nj = tigris::states(cb = T, year = 2015) %>%
filter(STUSPS == 'NJ')
nj_msas = tigris::core_based_statistical_areas(cb = T, year = 2015) %>%
filter(grepl('NJ', NAME)) %>%
sf::st_intersection(nj)
tmap_mode('plot')
nj_msas %>%
tm_shape() +
tm_polygons()
最后一个块给出了错误:
Error in vapply(g2, st_is_empty, logical(1)) : values must be length
1, but FUN(X[[3]]) result is length 148
当我删除 st_intersection
时,我从最后一个块中没有得到任何错误。我无法通过谷歌搜索在任何地方找到此错误消息。有谁知道怎么回事吗?
此外,如果我 运行 除了最后一个块之外的所有上述内容,并且我使用 ggplot2::geom_sf
创建地图,而不是 tmap
函数,我得到我的地图想。没有错误。
我正在使用 Ubuntu 18.04.1。 RStudio v1.1.463。 R 3.5.2。底格里斯河 0.7。地图 2.2。平方英尺 0.7-2.
似乎是由于相交产生的几何列
是“GEOMETRY”类型而不是“POLYGON”类型,可能是由于轻微的
tigris::states
和 tigris::core_based_statistical_areas
之间的错位,
导致错误 tm_shape()
:
> nj_msas
Simple feature collection with 7 features and 17 fields
geometry type: GEOMETRY
dimension: XY
bbox: xmin: -75.55961 ymin: 38.92852 xmax: -73.89398 ymax: 41.35742
epsg (SRID): 4269
proj4string: +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
CSAFP CBSAFP AFFGEOID GEOID NAME LSAD ALAND AWATER STATEFP
1 428 36140 310M200US36140 36140 Ocean City, NJ M1 651209688 955666476 34
2 408 10900 310M200US10900 10900 Allentown-Bethlehem-Easton, PA-NJ M1 3763879943 58581593 34
3 428 47220 310M200US47220 47220 Vineland-Bridgeton, NJ M1 1252937239 502091171 34
4 408 45940 310M200US45940 45940 Trenton, NJ M1 581629671 11189320 34
5 428 37980 310M200US37980 37980 Philadelphia-Camden-Wilmington, PA-NJ-DE-MD M1 11920145588 693341961 34
6 408 35620 310M200US35620 35620 New York-Newark-Jersey City, NY-NJ-PA M1 21478408908 6689374328 34
7 428 12100 310M200US12100 12100 Atlantic City-Hammonton, NJ M1 1439256827 300767732 34
STATENS AFFGEOID.1 GEOID.1 STUSPS NAME.1 LSAD.1 ALAND.1 AWATER.1 geometry
1 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 MULTIPOLYGON (((-74.55255 3...
2 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-75.12051 40.9683...
3 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-75.41956 39.4132...
4 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-74.94228 40.3408...
5 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 GEOMETRYCOLLECTION (LINESTR...
6 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-74.02454 40.7094...
7 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-74.98522 39.5148...
您可以通过仅从相交结果中提取多边形来解决此问题:
library(dplyr)
library(sf)
library(tigris)
library(tmap)
options(tigris_class = 'sf')
options(tigris_use_cache = TRUE)
nj = tigris::states(cb = T, year = 2015) %>%
filter(STUSPS == 'NJ')
nj_msas = tigris::core_based_statistical_areas(cb = T, year = 2015) %>%
filter(grepl('NJ', NAME)) %>%
sf::st_intersection(nj) %>%
sf::st_collection_extract("POLYGON")
tmap_mode('plot')
#> tmap mode set to plotting
tm_shape(nj_msas) + tm_polygons()
,或者通过在 nj
数据集上设置精度:
nj_msas = tigris::core_based_statistical_areas(cb = T, year = 2015) %>%
filter(grepl('NJ', NAME)) %>%
sf::st_intersection(sf::st_set_precision(nj ,1000))
tm_shape(nj_msas) + tm_polygons()
由 reprex package (v0.2.1)
于 2019-01-10 创建
Ibusett 的回复对我有用,添加了
%>% sf::st_collection_extract("POLYGON")
在我的转换更改结束时,尝试仅在县边界内识别学区边界。
它仍然适用于 ggplot。
谢谢
我运行以下:
library(dplyr)
library(sf)
library(tigris)
library(tmap)
options(tigris_class = 'sf')
options(tigris_use_cache = TRUE)
nj = tigris::states(cb = T, year = 2015) %>%
filter(STUSPS == 'NJ')
nj_msas = tigris::core_based_statistical_areas(cb = T, year = 2015) %>%
filter(grepl('NJ', NAME)) %>%
sf::st_intersection(nj)
tmap_mode('plot')
nj_msas %>%
tm_shape() +
tm_polygons()
最后一个块给出了错误:
Error in vapply(g2, st_is_empty, logical(1)) : values must be length 1, but FUN(X[[3]]) result is length 148
当我删除 st_intersection
时,我从最后一个块中没有得到任何错误。我无法通过谷歌搜索在任何地方找到此错误消息。有谁知道怎么回事吗?
此外,如果我 运行 除了最后一个块之外的所有上述内容,并且我使用 ggplot2::geom_sf
创建地图,而不是 tmap
函数,我得到我的地图想。没有错误。
我正在使用 Ubuntu 18.04.1。 RStudio v1.1.463。 R 3.5.2。底格里斯河 0.7。地图 2.2。平方英尺 0.7-2.
似乎是由于相交产生的几何列
是“GEOMETRY”类型而不是“POLYGON”类型,可能是由于轻微的
tigris::states
和 tigris::core_based_statistical_areas
之间的错位,
导致错误 tm_shape()
:
> nj_msas
Simple feature collection with 7 features and 17 fields
geometry type: GEOMETRY
dimension: XY
bbox: xmin: -75.55961 ymin: 38.92852 xmax: -73.89398 ymax: 41.35742
epsg (SRID): 4269
proj4string: +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
CSAFP CBSAFP AFFGEOID GEOID NAME LSAD ALAND AWATER STATEFP
1 428 36140 310M200US36140 36140 Ocean City, NJ M1 651209688 955666476 34
2 408 10900 310M200US10900 10900 Allentown-Bethlehem-Easton, PA-NJ M1 3763879943 58581593 34
3 428 47220 310M200US47220 47220 Vineland-Bridgeton, NJ M1 1252937239 502091171 34
4 408 45940 310M200US45940 45940 Trenton, NJ M1 581629671 11189320 34
5 428 37980 310M200US37980 37980 Philadelphia-Camden-Wilmington, PA-NJ-DE-MD M1 11920145588 693341961 34
6 408 35620 310M200US35620 35620 New York-Newark-Jersey City, NY-NJ-PA M1 21478408908 6689374328 34
7 428 12100 310M200US12100 12100 Atlantic City-Hammonton, NJ M1 1439256827 300767732 34
STATENS AFFGEOID.1 GEOID.1 STUSPS NAME.1 LSAD.1 ALAND.1 AWATER.1 geometry
1 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 MULTIPOLYGON (((-74.55255 3...
2 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-75.12051 40.9683...
3 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-75.41956 39.4132...
4 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-74.94228 40.3408...
5 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 GEOMETRYCOLLECTION (LINESTR...
6 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-74.02454 40.7094...
7 01779795 0400000US34 34 NJ New Jersey 00 19048075783 3543447118 POLYGON ((-74.98522 39.5148...
您可以通过仅从相交结果中提取多边形来解决此问题:
library(dplyr)
library(sf)
library(tigris)
library(tmap)
options(tigris_class = 'sf')
options(tigris_use_cache = TRUE)
nj = tigris::states(cb = T, year = 2015) %>%
filter(STUSPS == 'NJ')
nj_msas = tigris::core_based_statistical_areas(cb = T, year = 2015) %>%
filter(grepl('NJ', NAME)) %>%
sf::st_intersection(nj) %>%
sf::st_collection_extract("POLYGON")
tmap_mode('plot')
#> tmap mode set to plotting
tm_shape(nj_msas) + tm_polygons()
,或者通过在 nj
数据集上设置精度:
nj_msas = tigris::core_based_statistical_areas(cb = T, year = 2015) %>%
filter(grepl('NJ', NAME)) %>%
sf::st_intersection(sf::st_set_precision(nj ,1000))
tm_shape(nj_msas) + tm_polygons()
由 reprex package (v0.2.1)
于 2019-01-10 创建Ibusett 的回复对我有用,添加了
%>% sf::st_collection_extract("POLYGON")
在我的转换更改结束时,尝试仅在县边界内识别学区边界。
它仍然适用于 ggplot。
谢谢