davidcsterratt / RTriangle

Port of the Triangle Two-Dimensional Quality Mesh Generator and Delaunay Triangulator to R
https://cran.r-project.org/package=RTriangle
9 stars 4 forks source link

triangulating a pslg with attributes #17

Open roth-mh opened 4 years ago

roth-mh commented 4 years ago

when I am triangulating a pslg object with attributes, the triangulate function seems to (kind of) transpose the attribute matrix. in the example below i would expect PA to stay the same from the pslg object to the DT object. Am I misunderstanding the intended functionality?

sp_pts <- as.data.frame(matrix(nrow=4,ncol = 2))
colnames(sp_pts) <- c("latitude", "longitude")
sp_pts$latitude <- c(41,42,43,44)
sp_pts$longitude <- c(-120,-121,-122,-123)

att <- as.data.frame(matrix(nrow=4,ncol = 3))
colnames(att) <- c("att1", "att2", "att3")
att$att1 <- c(1, 1.1, 1.11, 1.111)
att$att2 <- c(2, 2.2, 2.22, 2.222)
att$att3 <- c(3, 3.3, 3.33, 3.333)

att
p <- pslg(P = sp_pts, PA = att)
p$PA[1,]

DT <- RTriangle::triangulate(p)
DT$PA[1,]

p$PA
DT$PA
DOSull commented 3 years ago

I am seeing the same behaviour, where I expect PA to emerge unscathed from the triangulation it ends up 'row-wise' in the output not 'column-wise':

tri <- pslg(P = tpts, PA = tattrs) %>%
  triangulate()

> head(tattrs)
           x       y
1  -428675.9 1520344
2 -1197290.8 7794188
3 -1120150.7 7234575
4 -1048583.9 6613223
5  -973499.6 5945572
6  -892969.4 5242144
> head(tri$PA)
           [,1]       [,2]
[1,]  -428675.9 -1197290.8
[2,] -1120150.7 -1048583.9
[3,]  -973499.6  -892969.4
[4,]  -807038.3  -716485.6
[5,]  -622414.4  -526061.0
[6,] -1524302.5 -1417125.3
DOSull commented 3 years ago

For what it's worth, I can untangle the attributes matrix with this function, but obviously would be a lot better to fix it at source!

untangle <- function(triPA) { 
  return ( matrix( c( t( triPA ) ), ncol = dim(triPA)[2] ) );
}

or with pipes

untangle <- function(triPA) {
  nc <- dim(triPA)[2]
  return (triPA %>% 
            t() %>%               # transpose
            c() %>%               # vectorise
            matrix(ncol = nc))    # column-wise repopulate the original shape
}
davidcsterratt commented 3 years ago

Sorry for the delay in looking at this. It would indeed be desirable to fix the issue. I have been very busy, but I hope to have some time next week (during my holiday) to look at it properly.

DOSull commented 3 years ago

This isn't more important than your holiday (at least not for me)! I assume the issue is some kind of index inversion when you are populating the output PA matrix