gpt4 book ai didi

r - Tmap:无法创建国家级学区 map

转载 作者:行者123 更新时间:2023-12-05 05:43:49 24 4
gpt4 key购买 nike

我想使用库 TmapR 的国家 map 上绘制学区。我使用了 official website 中的 shapefile , 国家教育统计中心,用于学区边界。请注意,我使用的是 2019 文件而不是最新文件,您可以从前面提到的链接中的 Single Composite File 下载此文件。我首先尝试了一个州,犹他州,一切顺利,没有任何问题。我绘制这张 map 的代码和国家 map (from Bloomberg Citylab) 的引用如下;

library (sf)
library (tmap)
map <- st_read(".../schooldistrict_sy1819_tl19.shp")
ut <- map[map$STATEFP == 49, ]
tm_shape(ut) + tm_borders()

enter image description here enter image description here

但是我在尝试制作国家 map 的时候遇到了错误。我使用此代码并收到以下错误。

tm_shape(map) + tm_borders()
Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

当然,我做了一些研究。一个网站建议由于无效的几何图形而发生以下错误。我检查并试图使这些有效,但我犯了和第一次一样的错误。我试图用以下代码解决这个问题。

sum(!st_is_valid(map$geometry))
[1] 858
st_make_valid(map$geometry)
Geometry set for 13315 features
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -179.1686 ymin: -14.5487 xmax: 179.7487 ymax: 71.38961
Geodetic CRS: NAD83
First 5 geometries:
MULTIPOLYGON (((-80.11417 26.07902, -80.11423 2...
MULTIPOLYGON (((-82.19919 26.77353, -82.19748 2...
MULTIPOLYGON (((-81.45356 25.80323, -81.45388 2...
MULTIPOLYGON (((-80.39962 25.25725, -80.40002 2...
MULTIPOLYGON (((-81.56409 27.34066, -81.56423 2...

tm_shape(map) + tm_borders()
Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

由于我对 R 中的几何结构一无所知,所以我完全不知道如何让它发挥作用。如果以前有人这样做过,您能否就此分享任何意见/建议?解决此问题的任何想法将不胜感激。非常感谢您阅读我的问题!

最佳答案

感谢@mrhellmann,他发现了我犯的小错误,您需要在使用st_make_valid 后将 map 数据分配给新变量。我使用以下代码将区级数据绘制到全国 map 中。

library (SF)
library (tmap)
map <- st_read(".../schooldistrict_sy1819_tl19.shp")
map2 <- st_make_valid(map$geometry)
tm_shape(map2) + tm_borders()

关于r - Tmap:无法创建国家级学区 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71743028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com