tests/testthat/test-watershed_diameter_raster.R

test_that("watershed_diameter_raster works", {
  skip_if_not_installed("EBImage")
  skip_if_not_installed("raster") # I don't know why it would needs raster,
  # but apparently it fails otherwise in some test environments

  test_point_cloud_file_path <- system.file(
    "extdata", "MixedConifer.laz",
    package = "lidR"
  )
  test_point_cloud_header <- lidR::readLASheader(test_point_cloud_file_path)
  test_point_cloud <- lidR::clip_rectangle(
    lidR::readLAS(test_point_cloud_file_path),
    xleft = test_point_cloud_header@PHB$`Min X`,
    ybottom = test_point_cloud_header@PHB$`Min Y`,
    xright = test_point_cloud_header@PHB$`Min X` + 50,
    ytop = test_point_cloud_header@PHB$`Min Y` + 50
  )

  testthat::expect_silent({
    watershed_raster1 <- watershed_diameter_raster(test_point_cloud)

    assert_that_raster_fits_point_cloud(
      watershed_raster1,
      test_point_cloud
    )
  })

  # test with modified parameters
  testthat::expect_silent({
    watershed_raster2 <- watershed_diameter_raster(test_point_cloud,
      crown_diameter_constant = 10,
      limits = c(0.3, 0.4),
      smoothing_radius = 0
    )
  })

  # test if function can be called from within segment_tree_crowns
  segmented_point_cloud1 <- segment_tree_crowns(
    test_point_cloud,
    "watershed",
    0.4
  )
  segmented_point_cloud2 <- segment_tree_crowns(
    test_point_cloud,
    watershed_raster1,
    0.4
  )
  testthat::expect_equal(segmented_point_cloud1, segmented_point_cloud2)
})

Try the crownsegmentr package in your browser

Any scripts or data that you put into this service are public.

crownsegmentr documentation built on Sept. 22, 2026, 5:09 p.m.