将所有美国块组人口普查型件混合在一起

发布于 2025-02-08 08:51:53 字数 1488 浏览 2 评论 0原文

正如标题所暗示的那样,我正在尝试从此处找到的所有人口普查中加载所有SHP文件( https://www2.census.gov/geo/tiger/tiger/tiger2019/bg/ ),并将它们全部合并为1个大SHP文件,以解决整个美国的大型SHP文件,以克服重复的多边形。

我采用了以前提出的问题中发现的代码,但无法使它起作用,因为它遇到了状态6。

下载中的错误(x,destfile = path,mode =“ wb”): 无法打开URL'ftp://ftp2.census.gov/geo/tiger/tiger/tiger2019/bg/tl_2019_06_bg.zip'

另外:警告消息:1:in doction.file(x,destfile = path, 模式=“ WB”):下载长度29680232!=报告长度 50020624

任何建议都将不胜感激。

library(RCurl) 
library(rgdal)

# get the directory listing
u <- 'ftp://ftp2.census.gov/geo/tiger/TIGER2019/BG/'
f <- paste0(u, strsplit(getURL(u, ftp.use.epsv = FALSE, ftplistonly = TRUE), 
                        '\\s+')[[1]])

# download and extract to tempdir/shps
invisible(sapply(f, function(x) {
  path <- file.path(tempdir(), basename(x))
  download.file(x, destfile=path, mode = 'wb')
  unzip(path, exdir=file.path(tempdir(), 'shps'))
}))

# read in all shps, and prepend shapefile name to IDs
shps <- lapply(sub('\\.zip', '', basename(f)), function(x) {
  shp <- readOGR(file.path(tempdir(), 'shps'), x)
  shp <- spChFIDs(shp, paste0(x, '_', sapply(slot(shp, "polygons"), slot, "ID")))
  shp
})

# rbind to a single object
shp <- do.call(rbind, as.list(shps))


# write out to wd/USA.shp
writeOGR(shp, '.', 'USA', 'ESRI Shapefile')

As the title suggests, I'm trying to load in all the SHP files from the Census found here (https://www2.census.gov/geo/tiger/TIGER2019/BG/), and merge them all together as 1 large shp file for the entire US overcoming issues with duplicate polygons.

I adopted code found from a question asked previously but could not get it to work as it stops once I hit state 6.

Error in download.file(x, destfile = path, mode = "wb") :
cannot open URL 'ftp://ftp2.census.gov/geo/tiger/TIGER2019/BG/tl_2019_06_bg.zip'

In addition: warning messages: 1: In download.file(x, destfile = path,
mode = "wb") : downloaded length 29680232 != reported length
50020624

Any suggestions would be much appreciated.

library(RCurl) 
library(rgdal)

# get the directory listing
u <- 'ftp://ftp2.census.gov/geo/tiger/TIGER2019/BG/'
f <- paste0(u, strsplit(getURL(u, ftp.use.epsv = FALSE, ftplistonly = TRUE), 
                        '\\s+')[[1]])

# download and extract to tempdir/shps
invisible(sapply(f, function(x) {
  path <- file.path(tempdir(), basename(x))
  download.file(x, destfile=path, mode = 'wb')
  unzip(path, exdir=file.path(tempdir(), 'shps'))
}))

# read in all shps, and prepend shapefile name to IDs
shps <- lapply(sub('\\.zip', '', basename(f)), function(x) {
  shp <- readOGR(file.path(tempdir(), 'shps'), x)
  shp <- spChFIDs(shp, paste0(x, '_', sapply(slot(shp, "polygons"), slot, "ID")))
  shp
})

# rbind to a single object
shp <- do.call(rbind, as.list(shps))


# write out to wd/USA.shp
writeOGR(shp, '.', 'USA', 'ESRI Shapefile')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文