我跟隨羅馬Luštrik張貼的鏈接,下面的答案是http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-UNION-3-shapefiles-td5914413.html#a5916751的微小修改。
以下代碼將允許您合併從Census 2000 5-Digit ZIP Code Tabulation Areas (ZCTAs) Cartographic Boundary Files獲取的.shp
文件並繪製它們。
在這種情況下,我下載了馬薩諸塞州,新罕布什爾州和緬因州的.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')
此鏈接(http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-UNION-3-shapefiles-td5914413.html#a5916751)上的合併。可讀取的檔案應該是處理空間數據的金礦。 – 2011-03-05 09:14:31
嗯......不知道R-sig-geo已經把它放到Nabble上了。不幸的是,它沒有與其他R論壇分組。 – Sharpie 2011-03-05 15:51:55
我花了將近五年的時間來認識到這一點,但是......它是「choropleth」而不是「chloropleth」 – 2011-03-08 02:18:05