url_shp_to_spdf: Mass download zipped .shp files from URL

Description Usage Arguments Details Examples

View source: R/url_shp_to_spdf.R

Description

The function allows you to download shapefile zip files into a temporary folder and directory, which are then unzipped and ready to use in R. Temp folders are automatically unlinked (deleted) and the working directory is set back to original within every iteration of the function. This means no shapefiles are stored to the drive.

Usage

1

Arguments

URL

A single URL or a vector of URLs

Details

Source of function: Kay Cichini (https://www.r-bloggers.com/batch-downloading-zipped-shapefiles-with-r/)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#this example uses a dummy sample from the Smithsonian Conservation Biology
#Institute, specifically a df containing all the species of trees found in the
#ForestGEO forest ecology study plot. The goal of this example is to load the
#shapefiles of each species into R global environment.
library(data.table)
fullsp <- fread("https://raw.githubusercontent.com/SCBI-ForestGEO/SCBI-ForestGEO-Data/master/species_lists/Tree%20ecology/SCBI_ForestGEO_sp_ecology.csv")

#1. Quick data formatting (e.g. changing species names to match BIEN database)
subsp <- fullsp[,c(1:3,5)]
subsp$species <- as.character(subsp$species)
subsp$sp <- c(paste0(fullsp$genus, sep="_", fullsp$species))

##change species names if need be (synonyms in BIEN, as determined
##from http://vaplantatlas.org/index.php?do=start&search=Search)
##this is done because sometimes the species names that we use are synonyms
##for the data that BIEN has
subsp$sp <- gsub("Carya_ovalis", "Carya_glabra", subsp$sp)
subsp$sp <- gsub("Carya_tomentosa", "Carya_alba", subsp$sp)
subsp$sp <- gsub("Sambucus_canadensis var. canadensis",
  "Sambucus_canadensis", subsp$sp)

splist <- c(subsp$sp)
splist <- sample(splist, 10) #for sake of this example

test <- fread("http://vegbiendev.nceas.ucsb.edu/bien/data/ranges/shapefiles/")
colnames(test) <- c("full.string", "align", "breaks")
test$sp <- sub(".*zip\">", "", test$full.string)
test$sp <- sub("_range.*$", "", test$sp)
test$mapsource <- "BIEN"

##species in the BIEN database will be marked
subsp$mapsource <- test$mapsource[match(subsp$sp, test$sp)]

#2. Create a list of the URLs
##this creates a vector of species that match between the fullsp list and
##the species available on the BIEN website.
matches <- unique (grep(paste(splist,collapse="|"), test$sp, value=TRUE))

##create list of URLs based on the matches
URLs <-
c(paste0("http://vegbiendev.nceas.ucsb.edu/bien/data/ranges/shapefiles/",
  matches, "_range.zip"))

#3. Define function, then use with sapply
y <- sapply(URLs, url_shp_to_spdf)
names(y) <- matches

mcgregorian1/sealR documentation built on July 5, 2021, 6:31 a.m.