2011-03-05 23 views
6

受這篇文章的驅動,Developing Geographic Thematic Maps with R,我正在考慮根據郵政編碼構建一個等值線地圖。我已經從http://www.census.gov/geo/www/cob/z52000.html下載了新罕布什爾州和緬因州的形狀文件,但我有興趣組合或合併來自這兩種狀態的.shp文件。R:合併來自多個州的郵政編碼形狀文件

在使用readShapeSpatial()讀取它們之後,maptools軟件包中是否存在一種機制來執行這種兩個.shp文件的合併或串聯?如果例如也歡迎輸入。使用RgoogleMaps包將會更容易。

+1

此鏈接(http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-UNION-3-shapefiles-td5914413.html#a5916751)上的合併。可讀取的檔案應該是處理空間數據的金礦。 – 2011-03-05 09:14:31

+0

嗯......不知道R-sig-geo已經把它放到Nabble上了。不幸的是,它沒有與其他R論壇分組。 – Sharpie 2011-03-05 15:51:55

+3

我花了將近五年的時間來認識到這一點,但是......它是「choropleth」而不是「chloropleth」 – 2011-03-08 02:18:05

回答

4

我跟隨羅馬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') 
0

GeoMerge是一個合併Shapefile的免費工具。合併SHP和DBF零件。似乎工作正常,但我沒有把它推得太多。

相關問題