_dev/dev_xy2FIApoly.R

xy2FIApoly = function(idxy,names=c(plot="plot",x="x",y="y"),create_polys=T){
  
  # sqrt((120*cos(pi/6))^2+(120*sin(pi/6))^2)
  requireNamespace("plyr")

  names["subplot"]="subplot"
  idxy[,names["subplot"]]=1
  
  dat2=idxy
  dat2[,names["subplot"]]=2
  dat2[,names["y"]]=idxy[,names["y"]]+120
  
  dat3=idxy
  dat3[,names["subplot"]]=3
  dat3[,names["x"]]=dat3[,names["x"]] + 103.923
  dat3[,names["y"]]=dat3[ , names["y"] ] - 60
  
  dat4=idxy
  dat4[,names["subplot"]]=4
  dat4[,names["x"]]=dat4[,names["x"]]-103.923
  dat4[,names["y"]]=dat4[,names["y"]]-60
  
  df_all=plyr::rbind.fill(idxy,dat2,dat3,dat4)
  
  
  if(create_polys){
    
    requireNamespace("raster")
    requireNamespace("sp")
    requireNamespace("rgeos")
    
    datin0=df_all
    coordinates(datin0)=datin0[,names[c("x","y")]]
    datin1=gBuffer(datin0,width=24,byid=T)
    datin2=aggregate(datin1,by=names["plot"])
    datin3=merge(datin2,idxy,by=names["plot"],duplicateGeoms=T)
    
    return(datin3)
  }
  
  if(!create_polys) return(df_all)
  
  
}
jstrunk001/RSForInvt documentation built on April 18, 2022, 11:03 p.m.