achubaty / grainscape

Efficient Modelling of Landscape Connectivity, Habitat, and Protected Area Networks
https://alexchubaty.com/grainscape
19 stars 2 forks source link

GOC() produces incorrect centroids and links when cost contains NA #46

Closed ecologics closed 7 years ago

ecologics commented 7 years ago

Here's the original cost surface raster with NA regions the rectangular chunks at the borders of the image. image

Here's what it looks like when grain() is run on it, and the various spatial outputs are plotted. Note the errant centroid in the top middle region. It appears to be associated with the centroid of the NA cells on the raster. Links are wacky as well, corresponding likely to this centroid throwing everything off. This is a hard to notice problem on simple rasters like this, but very obvious on large real-world rasters, which tend to have NAs around the edges as they are irregular regions of interest. The result is cases where links go every which way in a meaningless pattern when grain() is used to extract a GOC. Visualizations of MPG() outputs are perfect however!

image

Here is some reproducible code:

## Make a raster with NA values around edge
## as would typically occur in most GIS applications
tiny <- raster(system.file("extdata/tiny.asc", package = "grainscape"))
tinyCost <- reclassify(tiny, rcl = cbind(c(1, 2, 3, 4), c(1, 5, 10, 12)))
tinyCostNA <- tinyCost
tinyCostNA[0:40, 0:40] <- NA
tinyCostNA[0:20, 60:100] <- NA
tinyCostNA[50:80, 90:100] <- NA
tinyCostNA[90:100, 0:80] <- NA

## Extract MPG and GOC
tinyCostNAMPG <- MPG(tinyCostNA, tinyCostNA==1)
tinyCostNAGOC <- GOC(tinyCostNAMPG, 5, verbose=10)

## Use new export function to create simple to visualize things
spExport <- export(grain(tinyCostNAGOC,4), R=TRUE, vorBound=TRUE)
badCentroids <- spExport$nodes
badLinks <- spExport$linksCentroid
vorBound <- spExport$vorBound

## Plot
plot(vorBound, col=c("NA", "black"))
plot(badCentroids, add=TRUE, col="red", cex=2, pch=20)
plot(badLinks, add=TRUE, col="red")
ecologics commented 7 years ago

Fixed! Bug was in GOC(). Will commit and push. image

achubaty commented 7 years ago

this might make a good test: verify number of centroids = number of voronois. I'll put one together