Search code examples
spatialr-rasterterra

Difference between count and crosses in terra::rasterizeGeom()


I would like to convert roads (SpatVector; lines) to raster (SpatRaster) using terra R package. The terra::rasterizeGeom() function supports four functions: "count", "area", "length", or "crosses".

  • "area" does not make sense in this context.
  • "length" is intuitive.

I do not understand the difference between "count" and "crosses". Could you please explain the difference in the output map using either function options.

Cheers, Ahmed


Solution

  • "crosses" counts the number of lines that cross the border of a cell. "count" also includes lines that do not cross the border. Illustrated below

    library(terra)
    r <- rast(ncol=2, nrow=2, xmin=0, xmax=10, ymin=0, ymax=10)
    v <- vect(c("LINESTRING (1 7, 3 8)", "LINESTRING (8 7, 4 6, 3 3)", "LINESTRING (7 2, 8 4)"))
    
    cross <- rasterizeGeom(v, r, "crosses")
    count <- rasterizeGeom(v, r, "count")
    
    x <- c(cross, count)
    names(x) <- c("cross", "count")
      
    plot(x, fun=\(){lines(x, col="gray"); lines(v, col=rainbow(3), lwd=2)})
    

    enter image description here