r - 在 R : merging zip code shapefiles from multiple states 中制作等值线

标签 r shapefile

受到这里帖子的启发,Developing Geographic Thematic Maps with R ,我正在考虑根据邮政编码构建一个区域分布图。我已经从 http://www.census.gov/geo/www/cob/z52000.html 下载了新罕布什尔州和缅因州的形状文件,但我对合并或合并来自这两个州的 .shp 文件很感兴趣。
maptools中是否有机制用于在使用 readShapeSpatial() 阅读后对两个 .shp 文件进行这种合并或串联的包?如果例如,也欢迎输入使用 RgoogleMaps包会更容易。

最佳答案

我跟进了 Roman Luštrik 发布的链接,下面的答案是对 http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-UNION-3-shapefiles-td5914413.html#a5916751 的轻微修改。 .

以下代码将允许您合并 .shpCensus 2000 5-Digit ZIP Code Tabulation Areas (ZCTAs) Cartographic Boundary Files 获得的文件并绘制它们。

在这种情况下,我下载了 .shp文件和关联 .dbf.shx马萨诸塞州、新罕布什尔州和缅因州的文件。

library('maptools')
library('rgdal')

setwd('c:/location.of.shp.files')

# this location has the shapefiles for zt23_d00 (Maine), zt25_d00 (Mass.), and zt33_d00 (New Hampshire).

# columns.to.keep
# allows the subsequent spRbind to work properly

columns.to.keep <- c('AREA', 'PERIMETER', 'ZCTA', 'NAME', 'LSAD', 'LSAD_TRANS')

files <- list.files(pattern="*.shp$", recursive=TRUE, full.names=TRUE) 

uid <-1 

# get polygons from first file

poly.data<- readOGR(files[1], gsub("^.*/(.*).shp$", "\\1", files[1])) 
n <- length(slot(poly.data, "polygons"))
poly.data <- spChFIDs(poly.data, as.character(uid:(uid+n-1))) 
uid <- uid + n 
poly.data <- poly.data[columns.to.keep]

# combine remaining polygons with first polygon

for (i in 2:length(files)) {
    temp.data <- readOGR(files[i], gsub("^.*/(.*).shp$", "\\1",files[i]))
    n <- length(slot(temp.data, "polygons")) 
    temp.data <- spChFIDs(temp.data, as.character(uid:(uid+n-1))) 
    temp.data <- temp.data[columns.to.keep]
    uid <- uid + n 
    poly.data <- spRbind(poly.data,temp.data) 
}

plot(poly.data)

# save new shapefile

combined.shp <- 'combined.shp'
writeOGR(poly.data, dsn=combined.shp, layer='combined1', driver='ESRI Shapefile') 

关于r - 在 R : merging zip code shapefiles from multiple states 中制作等值线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5201458/

相关文章:

r - 如何提供在本地 Shiny 服务器中生成 Shiny 应用程序的 R 代码

r - ggplot中的混合线和散点图

google-maps - GeoJson 特征坐标以米为单位,而不是 [lng, lat]。如何在谷歌地图中显示?

openlayers - 使用 Geoserver 和 Openlayers 的墨卡托投影世界地图

r - 在 data.table 中使用 CJ 的动态名称

r - 在 R 中加入参差不齐的数组

regex - R中位置序列的长度(以字符串形式给出)

python - 导入的 shapefile 之外的 mask 区域( basemap /matplotlib)

r - readOGR 和 st_read 之间的区别

python - 使用 Geopandas 将 NYC shapefile 子集到曼哈顿