2011-03-05 6 views
6

ここに投稿されたDeveloping Geographic Thematic Maps with Rの動機によって、私は郵便番号に基づいてchoroplethマップを構築することを考えていました。私はhttp://www.census.gov/geo/www/cob/z52000.htmlからニューハンプシャー州とメイン州のシェイプファイルをダウンロードしましたが、私はこれら2つの州の.shpファイルを結合またはマージすることに興味があります。R:複数の州の郵便番号シェープファイルをマージすることでチョコレートを作る

maptoolsパッケージに、readShapeSpatial()を使用して読み取った後、この種の2つの.shpファイルをマージまたは連結するメカニズムがありますか。たとえば、 RgoogleMapsパッケージを使用する方が簡単です。

+1

このリンク(http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-UNION-3-シェイプファイル--td5914413.html#a5916751)をマージします。 nabbleアーカイブは、空間データを処理するための金鉱でなければなりません。 –

+0

Mmm ... R-sig-geoがNabbleにそれを作ったことは知らなかった。残念なことに、他のRフォーラムではグループ化されていません。 – Sharpie

+3

これを実現するには、GISには約5年かかったのですが... "クロロプラテス"ではなく "コオロープ"です。 –

答えて

4

私はRomanLuš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

は、シェイプファイルをマージするための無料ツールです。 SHPとDBFの部分をマージします。うまくいくように見えますが、あまりにもそれを押し込んだわけではありません。

関連する問題