Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Function for Batch Downloading Zipped Shapefiles with R

DZone's Guide to

A Function for Batch Downloading Zipped Shapefiles with R

· Big Data Zone ·
Free Resource

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

Here's a function I use to download multiple zipped shapefiles from URLs and load them to the workspace:

URLs <- c("http://gis.tirol.gv.at/ogd/umwelt/wasser/wis_gew_pl.zip",
          "http://gis.tirol.gv.at/ogd/umwelt/wasser/wis_tseepeicher_pl.zip")
 
url_shp_to_spdf <- function(URL) {
 
  require(rgdal)
 
  wd <- getwd()
  td <- tempdir()
  setwd(td)
 
  temp <- tempfile(fileext = ".zip")
  download.file(URL, temp)
  unzip(temp)
 
  shp <- dir(tempdir(), "*.shp$")
  lyr <- sub(".shp$", "", shp)
  y <- lapply(X = lyr, FUN = function(x) readOGR(dsn=shp, layer=lyr))
  names(y) <- lyr
 
  unlink(dir(td))
  setwd(wd)
  return(y)
  }
 
y <- lapply(URLs, url_shp_to_spdf)
z <- unlist(unlist(y))
 
# finally use it:
plot(z[[1]])


Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}