I am trying to map the polygons for various geographic areas (i.e. county/zip codes). Based on what I have found at this blog I can easily accomplish this for counties.
library(rgdal)
library(rgeos)
library(leaflet)
url<-"http://www2.census.gov/geo/tiger/TIGER2010DP1/County_2010Census_DP1.zip"
downloaddir<-getwd()
destname<-"tiger_county.zip"
download.file(url, destname)
unzip(destname, exdir=downloaddir, junkpaths=TRUE)
filename<-list.files(downloaddir, pattern=".shp", full.names=FALSE)
filename<-gsub(".shp", "", filename)
# ----- Read in shapefile (NAD83 coordinate system)
# ----- this is a fairly big shapefile and takes 1 minute to read
dat<-readOGR(downloaddir, "County_2010Census_DP1")
# ----- Create a subset of New York counties
subdat<-dat[substring(dat$GEOID10, 1, 2) == "36",]
# ----- Transform to EPSG 4326 - WGS84 (required)
subdat<-spTransform(subdat, CRS("+init=epsg:4326"))
# ----- save the data slot
subdat_data<-subdat@data[,c("GEOID10", "ALAND10")]
# ----- simplification yields a SpatialPolygons class
subdat<-gSimplify(subdat,tol=0.01, topologyPreserve=TRUE)
# ----- to write to geojson we need a SpatialPolygonsDataFrame
subdat<-SpatialPolygonsDataFrame(subdat, data=subdat_data)
leaflet() %>%
addTiles() %>%
addPolygons(data=subdat)
But if I run the exact same code with a different file for zip codes
url <- "http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_zcta510_500k.zip"
I get a completely different area of the country instead of New York.
Not sure if someone is more familiar with these datasets and these functions to explain why this difference happens?
Given @hrbrmstr noticed that the zip codes returned are in fact zip codes in Alabama this made me second guess my previous assumption on the structure of the
GEOID10
variable. I discovered this link which says that with the zcta files theGEOID10
variable is actually just the zip codes so it is not possible to filter the same as the county file.I figured out another way to filter using the
zip_codes
dataset from thenoncensus
package. I then substituted the linefor