Search code examples
rspatstat

How can I convert a 'pp3' dot pattern into an 'im' object in spatstat?


My goal is to create an im object from terrain elevation data (pp3's z-axis), similar to bei.extra ($elev and $grad). I started by creating a pp3 object from my data set:

x <- elev_test$x
y <- elev_test$y
z <- elev_test$z
X <- pp3(x, y, z, c(0, 102), c(0, 152), c(90, 102))
plot(X)

enter image description here

Then I tried to coerce the pp3 object to class im, but as.im threw an error:

> Elev_im <- as.im(X)
Error in as.im.default(X) : Can't convert X to a pixel image

How can I obtain the desired im object?


Solution

  • Conversion functions like as.im are designed to convert data from one format to another format that represents the same information.

    You are trying to infer the spatial height of a surface from a sample of (x,y,z) sample points. That's not a conversion between formats; that is some kind of interpolation.

    I suggest you create a two-dimensional point pattern using the (x,y) coordinates for the locations, and use the z coordinates as marks of this pattern. Then smooth the marks to obtain a surface.

    Something like:

    X <- ppp(elev_test$x, elev_test$y, c(0, 102), c(0, 152), marks=elev_test$z)
    Z <- Smooth(X)
    

    Then you can try different arguments to Smooth.ppp to get the best interpolant.

    Another possibility would be to use nnmark instead of Smooth.ppp to obtain a surface consisting of small flat pieces (this would be better if your points come from a rice paddy field). You'll have to make these decisions yourself as the computer is not wise enough.