-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash028Ecoregions.R
76 lines (54 loc) · 3.61 KB
/
dash028Ecoregions.R
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Setup to map countries/continents/oceans to Ecoregions
print(paste(date(), "-- ...finished setting up Visitor data. Starting dash028Ecoregions.R"))
# Add separate columns for DarCountry & DarContinentOcean
DarCtryContOcean <- FullDash2[,c("DarGlobalUniqueIdentifier",
"cleanDarCountry","cleanDarContinentOcean",
"cleanAccGeography", "cleanAccLocality"
)]
DarCtryContOcean[,2:NCOL(DarCtryContOcean)] <- sapply(DarCtryContOcean[,2:NCOL(DarCtryContOcean)],
function(x) gsub("(NA)+","",x))
# Only using first value from pipe-delimited Accession Geography fields (to simplify merge)
DarCtryContOcean[,4:NCOL(DarCtryContOcean)] <- sapply(DarCtryContOcean[,4:NCOL(DarCtryContOcean)],
function(x) gsub("\\s+\\|.*","",x))
# Set up merge-column for WWF/ESRI Ecoregion data
DarCtryContOcean$CountryOcean <- DarCtryContOcean$cleanDarCountry
#DarCtryContOcean$CountryOcean[which(nchar(DarCtryContOcean$CountryOcean)<1)] <- DarCtryContOcean$cleanDarContinentOcean[which(nchar(DarCtryContOcean$CountryOcean)<1)]
DarCtryContOcean$CountryOcean[which(nchar(DarCtryContOcean$CountryOcean)<1)] <- DarCtryContOcean$cleanAccGeography[which(nchar(DarCtryContOcean$CountryOcean)<1)]
DarCtryContOcean$CountryOcean[which(nchar(DarCtryContOcean$CountryOcean)<1)] <- DarCtryContOcean$cleanAccLocality[which(nchar(DarCtryContOcean$CountryOcean)<1)]
# Import Ecoregion/Realms-Country/Ocean join-tables
setwd(paste0(origdir, "/supplementary"))
Ecoregions <- read.csv("EcoRegionCountries.csv", stringsAsFactors = F)
#WWFtoESRI <- read.csv("EcoRegionWWF_ESRI.csv", stringsAsFactors = F)
Ecoregions <- unique(Ecoregions[which(nchar(Ecoregions$CountryOcean)>0),c("CountryOcean","EcoRegionsEnvironment")])
Ecocheck <- dplyr::count(Ecoregions, CountryOcean)
if (max(Ecocheck$n>1)) {
Ecoregions <- Ecoregions[order(Ecoregions$CountryOcean),]
Ecoregions$seq <- sequence(rle(as.character(Ecoregions$CountryOcean))$lengths)
Ecoregions <- spread(Ecoregions, seq, EcoRegionsEnvironment, fill="", sep="_")
Ecoregions <- unite(Ecoregions, EcoRegionsEnvironment, seq_1:seq_2, sep = " | ")
}
colnames(Ecoregions)[2] <- "Bioregion"
Ecoregions$CountryOcean <- tolower(Ecoregions$CountryOcean)
DarCtryContOcean$CountryOcean <- tolower(DarCtryContOcean$CountryOcean)
DarCtryContOcean <- merge(DarCtryContOcean, Ecoregions, by=c("CountryOcean"), all.x=T)
DarCtryContOcean <- DarCtryContOcean[,c("DarGlobalUniqueIdentifier","Bioregion")]
DarCtryContOcean$Bioregion[which(is.na(DarCtryContOcean$Bioregion)==T)] <- ""
#DarCtryContOcean <- unite(DarCtryContOcean, Bioregion, cleanDarCountry:cleanAccLocality, sep=" | ")
#DarCtryContOcean$Bioregion <- gsub("(\\|\\s+)+", "| ", DarCtryContOcean$Bioregion)
#DarCtryContOcean$Bioregion <- gsub("^\\s+\\|\\s+$|^\\s+\\||\\|\\s+$", "", DarCtryContOcean$Bioregion)
#DarCtryContOcean$Bioregion <- gsub("^\\s+|\\s+$", "", DarCtryContOcean$Bioregion)
FullDash8 <- merge(FullDash7csv, DarCtryContOcean, by=c("DarGlobalUniqueIdentifier"), all.x=T)
# Memory Cleanup ####
rm("FullDash2", "FullDash7csv")
gc()
Log028Ecoregions <- warnings()
setwd(origdir)
##install.packages("curl")
#library(curl)
#curl::curl_download("http://assets.worldwildlife.org/publications/15/files/original/official_teow.zip?1349272619", destfile = "official.zip")
#unzip("official.zip")
#shpfile <- "official/wwf_terr_ecos.shp"
##install.packages("geojsonio")
##install.packages("rgdal")
#library(geojsonio)
#shp <- geojsonio::geojson_read("official/wwf_terr_ecos.shp", method = "local", what = "sp")