Topic: Survey

Topic Description:

Functions for estimating elevation from topographic survey data.



File: topography/imageJ.r

View File Source Download File No help file available

Function: fullplot.imageJ

Function Description: fullplot.imageJ

Reads quadrat maps created by imageJ, converting the digitized pixel to coordinates within a quadrat (local coordinates).
The input imageJ files must be named in a very specific way: all should have the same prefix and the quadrat names should have 4 digits. If there are subquadrats, the names should end with the same consistent suffixes. Only those files with this specific naming convention are read and converted. Updated Aug 2014 to handle files with no trees (just 4 corner labels).

Function Arguments:

ArgumentDefault Value
path""
outfile'plotLxLy.txt'
delim'
'
include.subdirT
cornersc('p1','p2','p3','p4')
colrac(0,49)
rowrangec(0,24)
prefix'Map_'
suffix".txt"
subquadsuffixc('_1','_2','_3','_4')
gridsizec(10
10)
debugNULL

Arguments Description:

  • path: the complete path name where the map files to be converted are found
  • include.subdir: whether the subfolders are to be searched for map files also
  • outfile: the name of the text file where the results will be saved. This file will be saved in the folder specified by the path. If outfile=NULL, the results will not be written to a file.
  • corners: specify the tags used for the map corners used for calibration. They must be named in a clockwise direction starting with the lower left corner. These tags must be the same in each and every map file.
  • colrange: specifies the range of the columns, found as the first two digits of the quadrat name
  • rowrange: specifies the range of the rows, found in the last two digits of the quadrat name
  • prefix: the prefix used for all the map files before the quadrat name
  • suffix: the extension used for the map files. The imageJ default is ".txt".
  • subquadratsuffix: used for map files that are smaller than 20x20m (i.e. 10x10m). They should be named clockwise from the lower left subquadrat.
  • gridsize: size of each individual map


Sample Usage:

mapfolder='/maps/rabi/' coords= fullplot.imageJ(path=mapfolder,include.subdir=T,gridsize=c(10,10),outfile='location.txt',corners=c('p2','p1','p4','p3'), prefix='Map_',colrange=c(0,49), rowrange=c(0,24),subquadsuffix=c('_1','_2','_3','_4')) head(coords) dim(coords) range(coords$lx) range(coords$ly)


Function Source:


fullplot.imageJ=function(path="",outfile='plotLxLy.txt',delim=',', include.subdir=T, corners=c('p1',
        'p2','p3','p4'), colrange=c(0,49), rowrange=c(0,24), prefix='Map_', suffix=".txt",
subquadsuffix=c('_1','_2','_3','_4'),gridsize=c(10,10),debug=NULL)
{

# If include.subdir=T, all subfolders will be checked, else only the folder specified in the path w
        ill be checked
if(include.subdir != T) include.subdir=F

if (path=="") path='.'

# If path does not end with "/", add this to path

lenpath = length(unlist(strsplit(path,'')))
if (substr(path,lenpath,lenpath+1) != '/') path=paste(path,'/',sep='')

# Get all the quadrat names
allcol=colrange[1]:colrange[2]
allrow=rowrange[1]:rowrange[2]
allquad=as.vector(t(outer(convert.rowcol(allcol),convert.rowcol(allrow),pst)))

# Get all the files specified by the path and subdirectories if included
filelist=list.files(recursive=include.subdir, path=path)

# Screen the files for those that have quadrat names, and the specified prefix and suffix.
quadfiles=character()
for(q in allquad)

quadfiles=c(quadfiles,filelist[logical.grep(q,filelist) & logical.grep(prefix,filelist) & logical
        .grep(suffix,filelist)])
nofiles = length(quadfiles)

if (nofiles==0) return('....There are NO files to convert....\n')

fullresult=data.frame()

# Get length of quadratname and prefix
lenprefix = nchar(prefix)
lenquad = nchar(allquad[1])

for(i in 1:nofiles)
{
infile=quadfiles[i]

# Get quadrat and subquadrat names from file name

# Find position of last slash
infilesplit = unlist(strsplit(infile,''))
if (length(grep('/',infilesplit))==0) locationslash=0
else locationslash=max(grep('/',infilesplit))

# browser()

startposition= locationslash+lenprefix

quadrat = substr(infile,startposition+1,startposition+lenquad)

lastchar = gregexpr(suffix,tolower(infile),fixed=TRUE)
# lastchar = gregexpr(suffix,tolower(infile))
subquad = substr(infile,startposition+lenquad+1,lastchar[[1]]-1)

if(!is.null(debug)) if(quadrat==debug) browser()

completefile=paste(path,infile,sep='')


onemap=imageJ.to.lxly(textfile=completefile,lowerleft=corners[1],upperleft=corners[2],upperrig
        ht=corners[3],lowerright=corners[4], delim=delim,gridsize=gridsize)

if(onemap$missing!=0) browser()
if(dim(onemap$coord)[1]>0)
{
# Correct for subquadrat
coord=SectionCorrection(onemap$coord,subquad,gridsize,subquadsuffix=subquadsuffix)
coord$quadrat=I(quadrat)

fullresult=rbind(fullresult,coord)
}

cat('Finished calculating coordinates for quadrat ', quadrat, '\n')
}

if(!is.null(outfile))
{
if (path=='.') path=''
output=pst(path,outfile)
write.table(fullresult,file=output,quote=FALSE,sep='\t',row.names=FALSE,col.names=TRUE)
}

return(fullresult)
}