September 30, 2013
By

(This article was first published on Landscape Ecology 2.0 » R, and kindly contributed to R-bloggers)

This post is a follow up from my latest Things I Forget post on reading in shapefiles.  That post assumed that you already had access to all the relevant files (e.g. .shp, .shx, .prj, .dbf, etc.).  A task that I routinely need to do is locate shapefiles on a website, grab those files, and read them in.  Instead of having to do this manually I wrote a function a while back to take care of this task.  The function simply requires shape_url, a link to the location of the files, and layer, the name of the shapefile. Currently the layer should not contain the .shp extension. There is also an optional parameter outfile that can be used to create a different name for the downloaded files.

I haven’t spent much time on error handling. For instance this function assumes you already have rgdal and sp installed and loaded. If you try to use this and get an error, let me know in the comments and I’ll try to fix it. Or better yet suggest a change and I’ll throw it in!

So here is the function.

download.shapefile<-function(shape_url,layer,outfile=layer)
{
#written by: jw hollister
#Oct 10, 2012

#set-up/clean-up variables
if(length(grep("/\$",shape_url))==0)
{
shape_url<-paste(shape_url,"/",sep="")
}
#creates vector of all possible shapefile extensions
shapefile_ext<-c(".shp",".shx",".dbf",".prj",".sbn",".sbx",
".shp.xml",".fbn",".fbx",".ain",".aih",".ixs",
".mxs",".atx",".cpg")

#Check which shapefile files exist
if(require(RCurl))
{
xurl<-getURL(shape_url)
xlogic<-NULL
for(i in paste(layer,shapefile_ext,sep=""))
{
xlogic<-c(xlogic,grepl(i,xurl))
}

shapefiles<-paste(shape_url,layer,shapefile_ext,sep="")[xlogic]
#Set-up output file names
outfiles<-paste(outfile,shapefile_ext,sep="")[xlogic]   }
if(sum(xlogic)>0)
{
for(i in 1:length(shapefiles))
{
method="auto",mode="wb")
}
} else
{
stop("An Error has occured with the input URL
or name of shapefile")
}
}


And now to prove it works I can do something like the following:

#Download the NH State Boundaries
"NHSenateDists2012")
#Plot it
plot(NHBnd)


New Hampshire State Boundary and Senate District

Lastly, there are some other approaches for tackling related problems listed below.