R/getnodeind.rivernet.r

Defines functions getnodeind.rivernet getnodeind

Documented in getnodeind getnodeind.rivernet

getnodeind <- function(net,x,y, ...) UseMethod("getnodeind")


getnodeind.rivernet <- function(net,x,y,...)
{
  n <- length(x)
  if ( n != length(y) ) return(NA)
  if ( n == 0 )         return(NA)
  
  ind  <- rep(NA,n)
  dist <- rep(NA,n)
  for ( i in 1:n )
  {
    for ( j in 1:length(net$nodes) )
    {
      dist.new <- sqrt( (x[i]-net$nodes[[j]]$x)^2 + (y[i]-net$nodes[[j]]$y)^2 )
      if ( !is.na(dist.new) )
      {
        if ( is.na(dist[i]) )
        {
          dist[i] <- dist.new
          ind[i]  <- j
        }  
        else
        {
          if ( dist.new < dist[i] )
          {
            dist[i] <- dist.new
            ind[i]  <- j
          }
        }
      }
    }
  }
  return(data.frame(node.ind=ind,dist=dist))
}

Try the rivernet package in your browser

Any scripts or data that you put into this service are public.

rivernet documentation built on Aug. 28, 2023, 5:06 p.m.