在 R 中制作 choropleth:合并来自多个州的邮政编码 shapefile [英] making a choropleth in R: merging zip code shapefiles from multiple states
问题描述
受到此处帖子的启发,使用 R 开发地理专题地图,我正在考虑根据邮政编码构建一个区域分布图.我已经从 http://www 下载了新罕布什尔州和缅因州的形状文件.census.gov/geo/www/cob/z52000.html,但我对合并或合并来自这两个州的 .shp 文件很感兴趣.
Motivated by the post here, Developing Geographic Thematic Maps with R, I was thinking about constructing a choropleth map based on zip codes. I've downloaded the shape files for New Hampshire and Maine from http://www.census.gov/geo/www/cob/z52000.html, but I'm interested in combining or merging the .shp files from these two states.
在使用 readShapeSpatial()
读取两个 .shp 文件后,maptools
包中是否有一种机制可以对它们进行这种合并或串联?如果例如,也欢迎输入使用 RgoogleMaps
包会更容易.
Is there a mechanism in the maptools
package for doing this kind of merge or concatenation of two .shp files after you read them in using readShapeSpatial()
? Also welcome input if e.g. using the RgoogleMaps
package would be easier.
推荐答案
我跟进了 Roman Luštrik 发布的链接,下面的回答是对 http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-UNION-3-shapefiles-td5914413.html#a5916751.
I followed up on the link posted by Roman Luštrik, and the following answer is a slight modification of http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-UNION-3-shapefiles-td5914413.html#a5916751.
以下代码将允许您合并从 .shp 文件="nofollow">人口普查 2000 年 5 位邮政编码制表区 (ZCTA) 制图边界文件并绘制它们.
The following code will allow you to merge the .shp
files obtained from Census 2000 5-Digit ZIP Code Tabulation Areas (ZCTAs) Cartographic Boundary Files and plot them.
在本例中,我下载了马萨诸塞州、新罕布什尔州和缅因州的 .shp
文件和相关的 .dbf
和 .shx
文件.
In this case, I downloaded the .shp
files and associated .dbf
and .shx
files for Massachusetts, New Hampshire, and Maine.
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 中制作 choropleth:合并来自多个州的邮政编码 shapefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!