First, thanks for developing this useful package! I believe there is an issue with the calculate of boundaries for bound.dat
. This simple 3x3 grid of hexagons illustrates the problem:
library(sp)
library(marxan)
library(magrittr)
library(dplyr)
utm10 <- crs('+proj=utm +zone=10 +ellps=GRS80 +datum=NAD83 +units=m +no_defs')
pts <- extent(c(0, 5000, 0, 5000)) %>%
as('SpatialPolygons') %>%
spsample(type='hexagonal', cellsize=(1000 * sqrt(3)))
hex <- HexPoints2SpatialPolygons(pts)
plot(hex)
plot(pts, pch=19, add=T)
plot(hex[c(1),], col='red', add=T)
plot(hex[c(2,4),], col=c('green','blue'), add=T)
Calculating boundaries using the default tolerance leads to most hexagons not having any shared boundary.
calcBoundaryData(hex) %>% arrange(id1, id2)
# Results
# id1 id2 boundary
#1 1 1 6000
#2 2 2 6000
#3 3 3 6000
#4 4 4 6000
#5 5 5 5000
#6 5 8 1000
#7 6 6 6000
#8 7 7 6000
#9 8 8 5000
#10 9 9 6000
Fiddling with the tolerance yields better results but some shared boundaries are missing.
calcBoundaryData(hex, tolerance=0.1) %>% arrange(id1, id2)
# Results
# id1 id2 boundary
#1 1 1 5000
#2 1 2 1000
#3 2 2 3000
#4 2 3 1000
#5 2 4 1000
#6 3 3 4000
#7 3 5 1000
#8 4 4 3000
#9 4 5 1000
#10 4 7 1000
#11 5 5 2000
#12 5 6 1000
#13 5 8 1000
#14 6 6 4000
#15 6 9 1000
#16 7 7 4000
#17 7 8 1000
#18 8 8 3000
#19 8 9 1000
#20 9 9 4000
Most boundaries are caught, but those shared boundaries oriented in a NW-SE direction are missed. For example, hexagon 1 (red above) is missing the boundary with hexagon 4 (blue above) and the extra 1000m is assigned to the self-boundary of 1.
I have limited experience with C++, but I believe I've fixed the issues in the source code with a couple small changes, which are in this commit in my forked repository. The function appears to work fine on my machine now.