[R-sig-Geo] measuring distances between several patches in an area

Carlos Alberto Arnillas carlosalberto.arnillas at gmail.com
Sat Jan 9 15:15:06 CET 2016


Hello
I have been trying to find a function to measure the distance between
several patches simultaneously.
I tried with the raster packages iterating by every patch, measuring the
distance from there to every single pixel out of the patch (using the
function distance) and then looking for the minimum distance to all the
other patches. but it's extremely time consuming. It is also extremely
inefficient because I'm measuring every distance twice and because I'm
measuring unneeded distances also (if the only way to go from patch A to
patch C crosses patch B, I don't need the distance between patch A and C).
Below is the code I'm using...

Thanks for any advice...

Carlos Alberto


region <- raster(mapFinal) # the landscape map.
patch <- region
biome <- region
biomes <- unique(region)
areas <- area(region)

map.distances <-function (i) {
  dA <- data.frame(biome = integer(0),
                   patch = integer(0),
                   area = numeric(0))
  dD <- data.frame(biome = integer(0),
                   from = integer(0),
                   to = integer(0),
                   dist = numeric(0))
  biome[] <- NA_integer_
  biome[region == i] <- i
  biomeC <- clump(biome, directions=8)
  dA <- rbind(dA, cbind(biome = i,
              zonal(areas, biomeC, 'sum')))
  patches <- as.integer(unique(biomeC))
  for (j in patches[-1]) {
    patch[] <- NA_integer_
    patch[biomeC == j] <- 1L
    dists <- distance(patch)
    d <- zonal(dists, biomeC, "min")
    f <- j > d[,1]
    dD <- rbind(dD, data.frame(from = j,
                   to = d[f,1],
                   dist = d[f,2], biome = i))
  }
  try(save(dD, dA,
      file=paste0("data/toGraph.",i,".RData")), T)
  return(list(edges=dD, vertices=dA))
}

db.distances1 <- lapply (biomes[1], map.distances)

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list