6

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.

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.

Uwe
  • 41,420
  • 11
  • 90
  • 134
andrewj
  • 2,965
  • 8
  • 36
  • 37
  • 1
    This this link (http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-UNION-3-shapefiles-td5914413.html#a5916751) on merging. The nabble archive should be a gold mine for handling spatial data. – Roman Luštrik Mar 05 '11 at 09:14
  • Mmm... didn't know R-sig-geo had made it onto Nabble. Unfortunate that it isn't grouped in with the other R forums. – Sharpie Mar 05 '11 at 15:51
  • 3
    It took me almost five years of GIS to realize this but... it's "choropleth" not "chloropleth" – J. Win. Mar 08 '11 at 02:18
  • 1
    @J. Winchester, good point...I just corrected the title to "choropleth." – andrewj Mar 08 '11 at 13:09

2 Answers2

4

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.

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.

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') 
andrewj
  • 2,965
  • 8
  • 36
  • 37
0

GeoMerge is a free tool for merging Shapefiles. Merges SHP and DBF parts. Seems to work OK, but I haven't pushed it too much.

xan
  • 7,511
  • 2
  • 32
  • 45