Skip to content

Instantly share code, notes, and snippets.

@jhpoelen
Last active August 29, 2015 14:10
Show Gist options
  • Save jhpoelen/cbcce87fbd785cc8a1ae to your computer and use it in GitHub Desktop.
Save jhpoelen/cbcce87fbd785cc8a1ae to your computer and use it in GitHub Desktop.
Generate Global Biotic Interaction Distribution Maps
# see Jorrit H. Poelen, James D. Simons and Chris J. Mungall. (2014). Global Biotic Interactions: An open infrastructure to share and
# analyze species-interaction datasets. Ecological Informatics. http://dx.doi.org/10.1016/j.ecoinf.2014.08.005
# rasterize locations, retrieve stats for bounding boxes, plot graphs
if (!'package:raster' %in% search()) {
install.packages(c('raster','maps'), repos='http://cran.us.r-project.org')
library('raster')
library('maps')
}
url.prefix <- 'http://api.globalbioticinteractions.org/'
retrieveLocations <- function() {
url.locations <- paste(url.prefix, 'locations?type=csv', sep='')
cat('locations retrieving from [')
cat(url.locations)
cat(']...')
locations <- read.csv(url.locations)
cat('done.\n')
locations
}
locations <- retrieveLocations()
# create raster with cells 5' x 5'
r <- raster()
nrow(r) <- 36
ncol(r) <- 72
xy <- cbind(locations$loc.longitude,locations$loc.latitude)
rWithData <- rasterize(xy, r, fun='count')
world <- map(interior=FALSE)
plot(rWithData)
lines(world, col=rgb(0.8,0.8,1,1.0))
pointsWithLocations <- rasterToPoints(rWithData)
getLocationInfo <- function(points) {
longitudes <- points[,1]
latitudes <- points[,2]
bboxes <- cbind(longitudes-5, latitudes-5, longitudes+5, latitudes+5)
colnames(bboxes) <- c('west', 'south', 'east', 'north')
bbox <- paste(bboxes[,'west'],bboxes[,'south'], bboxes[,'east'], bboxes[,'north'], sep=",")
request <- paste(url.prefix, 'spatialInfo?type=csv&bbox=', bbox, sep='')
location.info <- t(sapply(request, function(url) {
cat(paste('retrieving [', url, ']...', sep=''))
data <- read.csv(url)
cat(' done.\n')
data
}
))
}
points <- pointsWithLocations[,1:2]
info <- getLocationInfo(points)
logScale <- function(x, ...) { log10(x) }
rasterizeWithInfo = function(points, info, r, colname, fun = logScale) {
rasterize(points[,1:2], r, as.numeric(info[,colname]), fun=fun)
}
SetFilename <- function(n) {
filename <- paste('distribution_', gsub('\\.','_', n), '.png', sep='')
png(file=filename, width=640, height=320)
filename
}
DrawMap <- function(variable.name) {
rWithInteractions <- rasterizeWithInfo(points, info, r, variable.name)
filename <- SetFilename(variable.name)
par(bty='n')
par(mar=rep(0,4))
par(col.axis='yellow')
plot(rWithInteractions, axes=FALSE, xlim=c(-180,180), ylim=c(-90,90),bty='n')
lines(world, col=rgb(0,0,1,1.0))
dev.off()
cat(paste('wrote distribution graph to', filename, '\n'))
filename
}
DrawMaps <- function() {
variables <- list('number.of.interactions',
'number.of.interactions.with.timestamp',
'number.of.distinct.interactions',
'number.of.distinct.studies',
'number.of.distinct.study.sources',
'number.of.distinct.locations')
filenames <- lapply(variables, DrawMap)
}
DrawMaps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment