tracks.r

# Written: R Branton, Jan 2012
# Purpose: Demonstrate use of RStudio, RCurl and RgoogleMaps packages for plotting OTN detection data ...
# Notes: 1st time users must edit and execute the following command line to create .OTNUserID file ...
# cat('USERNAME:PASSWORD@',file=".OTNUserID")

# Set these variables to specify desired server
my.serviceName<-'kil-otn-2w1.its.dal.ca'  # use this when running from RStudio on OTN server
my.serviceName<-'kil-otn-2w1-x.its.dal.ca' # use either this or the next when running from RStudio on desktop

# Remaining code is fixed ...

library(RCurl)
library(RgoogleMaps)
library(manipulate)
options(useFancyQuotes = FALSE)

get.data<-function(typeName=''){
my.url<-paste('http://',scan(".OTNUserID","character",quiet=TRUE),my.serviceName,":8080/geoserver/wfs?request=getfeature&service=wfs&version=1.1.0&outputformat=CSV&typename=",typeName,sep="")
cat(getURL(my.url),file="temp.csv")
read.csv(file="temp.csv",sep=",",header=TRUE,as.is=TRUE)}

x<-rbind(
#  get.data('dasUnit:mv_anm_compressed'),
#  get.data('mpdunit:mv_anm_compressed'),
#  get.data('nspunit:mv_anm_compressed'),
#  get.data('pbnUnit:mv_anm_compressed'),
#  get.data('pbuUnit:mv_anm_compressed'),
#  get.data('smrunit:mv_anm_compressed'),
get.data('wdgunit:mv_anm_compressed'))

cat(paste(
'manipulate(my.plot(releasedBy,detectedBy,yearCollected,scientificName,catalogNumber,colorBy,mapType,showTitle,zoomIn)',',\n',
'releasedBy=picker("all",',paste(dQuote(names(table(x$collectioncode))),collapse=","),')',',\n',
'detectedBy=picker("all",',paste(dQuote(names(table(x$detectedby[x$detectedby!='release']))),collapse=","),')',',\n',
'yearCollected=picker("all",',paste(dQuote(names(table(substr(x$datestart,1,4)))),collapse=","),')',',\n',
'scientificName=picker("all",',paste(dQuote(names(table(x$scientific))),collapse=","),')',',\n',
'catalogNumber=picker("all",',paste(dQuote(names(table(x$catalognumber))),collapse=","),'),','\n',
'colorBy=picker("detectedBy","releasedBy"),','\n',
'mapType=picker("satellite","hybrid","terrain","mobile"),','\n',
'showTitle=picker("top","bottom","none"),','\n',
'zoomIn=picker("out","out+","in",',paste(dQuote(names(table(x$detectedby))),collapse=","),')','\n',
')',
collapse=''),file='temp.r')

################

my.plot<-function(releasedBy="all",detectedBy="all",yearCollected="all",scientificName="all",catalogNumber="",colorBy="detectedBy",mapType="satellite",showTitle="top",zoomIn="out"){

xx<-x

x$color.detectedby<-as.numeric(factor(x$detectedby))+1
x$color.releasedby<-as.numeric(factor(x$collectioncode))+1
x$color.all<-x$color.detectedby
if (colorBy=='releasedBy'){x$color.all<-x$color.releasedby}

if (catalogNumber!='all'){
x<-x[x$catalognumber==catalogNumber,]
} else {
if (releasedBy!='all'){x<-x[x$collectioncode==releasedBy,]}
if (detectedBy!='all'){x<-x[x$detectedby==detectedBy,]}
if (yearCollected!='all'){x<-x[substr(x$datestart,1,4)==yearCollected,]}
if (scientificName!='all'){x<-x[x$scientificname==scientificName,]}
}

if(substr(zoomIn,1,3)=="out"){
if(zoomIn=="out+"){    
MyMap<-GetMap.bbox(lonR=c(min(xx$lon)-0.25,max(xx$lon)+0.25),latR=c(min(xx$lat)-0.25,max(xx$lat)+0.25),destfile='temp.png',maptype=paste(mapType))   
}else{
MyMap<-GetMap.bbox(lonR=range(xx$lon),latR=range(xx$lat),destfile='temp.png',maptype=paste(mapType))       
}
} else {
if(zoomIn=="in"){
MyMap<-GetMap.bbox(lonR=range(x$lon),latR=range(x$lat),destfile='temp.png',maptype=paste(mapType))       
} else {
MyMap<-GetMap.bbox(
lonR=range(xx$lon[xx$detectedby==paste(zoomIn)&xx$detectedby!='release']),
latR=range(xx$lat[xx$detectedby==paste(zoomIn)&xx$detectedby!='release']),
destfile='temp.png',maptype=paste(mapType))       
}
}
if (colorBy=='detectedBy'){    
PlotOnStaticMap(MyMap,lon=x$lon,lat=x$lat,FUN=points,col=x$color.detectedby,lwd=1.5,verbose=0,cex=log(x$total_count)+1)
}else{
PlotOnStaticMap(MyMap,lon=x$lon,lat=x$lat,FUN=points,col=x$color.releasedby,lwd=1.5,verbose=0,cex=log(x$total_count)+1)  
}
if(catalogNumber=='all'){
for (x2 in split(x,x$catalognumber)){
PlotOnStaticMap(MyMap,lon=x2$lon,lat=x2$lat,FUN=lines,col="grey",verbose=0,add=TRUE,lwd=2)
}
if(showTitle!='none'){
legend(paste(showTitle),legend=paste('releasedBy=',releasedBy,' ,yearCollected=',yearCollected,' ,scientificName=',scientificName,' colorBy=',colorBy,sep=''))
}
cat('\n',paste(x$scientificname[1],' (n=',length(table(x$catalognumber)),')',sep=''),'\n')
print(table(x$catalognumber))
}else{
n<-(dim(x)[1])-1
PlotArrowsOnStaticMap(MyMap,lon0=x$lon[1:n],lat0=x$lat[1:n],lon1=x$lon[2:(n+1)],lat1=x$lat[2:(n+1)],FUN=arrows,col="grey",verbose=0,add=TRUE,length=.1,lwd=2)
legend("top",legend=paste(x$catalognumber[1],': ',x$scientificname[1],' / ',x$commonname[1],sep=''))
cat('\n',x$catalognumber[1],substr(x$startdatecollected,1,4)[1],'\n')
print(paste(x$station,' (n=',x$total_count,')',sep=""))
}
PlotOnStaticMap(MyMap,lon=x$lon[x$detectedby=='release'],lat=x$lat[x$detectedby=='release'],FUN=points,col="yellow",pch=8,cex=2,lwd=2,verbose=0,add=TRUE)
PlotOnStaticMap(MyMap,lon=x$lon[x$detectedby=='release'],lat=x$lat[x$detectedby=='release'],FUN=points,col=x$color.releasedby[x$detectedby=='release'],cex=log(1)+1,lwd=1.5,verbose=0,add=TRUE)
if(colorBy=='detectedBy'){
x3<-x[x$detectedby!='release',]            
my.legend<-aggregate(x3$total_count,list(legend=x3$detectedby,col=x3$color.detectedby),sum)                
} else {
x3<-x[x$detectedby=='release',]            
my.legend<-aggregate(x3$total_count,list(legend=x3$collectioncode,color=x3$color.releasedby),sum)                
}              
my.legend<-my.legend[order(my.legend$legend),]
legend("bottomright",title=paste('TotalDetected','=',sum(x3$total_count)),legend=paste(my.legend$legend,'-',my.legend$x),col=my.legend$col,pch=1,pt.cex=1.5,pt.lwd=2)
}
source('temp.r')