Search code examples
spatstat

Combining two point pattern objects in spatstat creating a combined mark


Suppose there are two point patterns in spatstat. I understand we can superimpose these two point patterns to get a single point pattern. Now if there are common coordinates for these patterns but with different marks, then the points will be duplicated when superimposed. Is there a way in spatstat to get a unique set of points while creating a common mark for the coordinates that coincide?


Solution

  • I’m not aware of a built-in solution to do this, so you have to do a bit of manual work as detailed below.

    Load package and make example data with overlapping points:

    library(spatstat)
    X1 <- cells[1:22]
    marks(X1) <- factor("a")
    X2 <- cells[20:42]
    marks(X2) <- factor("b")
    plot(superimpose(X1, X2), main = "")
    

    For each point in X1 find the nearest point in X2:

    nn <- nncross(X1, X2)
    tail(nn)
    #>         dist which
    #> 17 0.1386110     4
    #> 18 0.1802776     5
    #> 19 0.1069766     5
    #> 20 0.0000000     1
    #> 21 0.0000000     2
    #> 22 0.0000000     3
    id1 <- which(nn$dist==0) ## Tests EXACT equality. Consider small tolerance.
    id2 <- nn$which[id1]
    

    Add extra mark level to X1 and assign it to points with duplicates in X2:

    levels(marks(X1)) <- c("a", "c")
    marks(X1)[20:22] <- factor("c")
    X <- superimpose(X1, X2[-id2])
    plot(X, main = "")