simpleobismaps/simpleobismapsCCP.R

81 lines
1.9 KiB
R

# simpleobismapsCCP.R
# Simple example for CCP integration
#
# Author: Giancarlo Panichi
###############################################################################
library(robis)
library(ggplot2)
library(rgeos)
library(stringr)
library(utils)
createplot <- function(sname,usedPolygon){
result = tryCatch({
result=paste("/ccp_data/maps/",sname,".png",sep="")
wktP <-readWKT(usedPolygon)
print(wktP@bbox)
#By scientific name and geometry
y=occurrence(sname, geometry = usedPolygon)
occplot<-map_ggplot(y, color = "#ff3399")+coord_sf(xlim = wktP@bbox[1,], ylim = wktP@bbox[2,])
ggsave(result, plot = occplot)
}, warning = function(w) {
print(w)
}, error = function(e) {
print(e)
}, finally = {
print(paste(sname,"done.", sep=" "))
})
}
readfile <- function(filename, usedPolygon) {
con = file(filename, "r")
while ( TRUE ) {
line = readLines(con, n = 1)
line = str_trim(line)
if ( length(line) == 0 ) {
break
}
print(line)
createplot(line, usedPolygon)
}
close(con)
}
checkmapsfolder <- function(){
if (!dir.exists("/ccp_data/maps")){
dir.create("/ccp_data/maps")
}else{
print("maps dir exists")
}
}
#url="https://data.d4science.net/Y8WK"
#usedPolygon="POLYGON((-5.657713322217344 45.88175260533238,38.99072417778265 45.88175260533238,38.99072417778265 29.60135866020714,-5.657713322217344 29.60135866020714,-5.657713322217344 45.88175260533238))"
main <- function(){
args = commandArgs(trailingOnly=TRUE)
print(args)
if (length(args)<=1) {
stop("At least two arguments must be supplied", call.=FALSE)
}
filename="fishScientificName.csv"
url=args[1]
usedPolygon=args[2]
mapszip="/ccp_data/maps.zip"
download.file(url, filename)
checkmapsfolder()
readfile(filename,usedPolygon)
zip(zipfile = mapszip, files = "/ccp_data/maps")
unlink("/ccp_data/maps",recursive = TRUE)
}
main()