tests/testthat/test-hotspot_dbscan.R

make_dbscan_points <- function() {
  # Two four-point clusters with equal counts but different areas, surrounded
  # by four noise points that keep buffered cluster hulls inside the study hull
  coordinates <- rbind(
    c(-2.3, -0.3), c(-2.3, 0.3), c(-1.7, -0.3), c(-1.7, 0.3),
    c(1.9, -0.1), c(1.9, 0.1), c(2.1, -0.1), c(2.1, 0.1),
    c(-6, -6), c(-6, 6), c(6, -6), c(6, 6)
  )
  sf::st_as_sf(
    data.frame(id = seq_len(nrow(coordinates)), coordinates),
    coords = c("X1", "X2"),
    crs = 3857
  )
}

test_that("hotspot_dbscan returns ranked tidy polygon output", {
  result <- hotspot_dbscan(
    make_dbscan_points(),
    eps = 1,
    min_pts = 3,
    hull = "convex",
    quiet = TRUE
  )

  expect_s3_class(result, "hspt_s")
  expect_s3_class(result, "sf")
  expect_s3_class(result, "tbl_df")
  expect_named(
    result,
    c("cluster", "rank", "n", "prop", "prop_area", "geometry")
  )
  expect_type(result$cluster, "integer")
  expect_type(result$rank, "integer")
  expect_type(result$n, "integer")
  expect_type(result$prop, "double")
  expect_type(result$prop_area, "double")
  expect_identical(result$rank, 1:2)
  expect_identical(result$n, c(4L, 4L))
  expect_equal(result$prop, c(4 / 12, 4 / 12))
  expect_equal(
    result$prop_area,
    as.numeric(sf::st_area(result)) /
      as.numeric(sf::st_area(sf::st_convex_hull(sf::st_union(
        make_dbscan_points()
      ))))
  )
  expect_identical(result$cluster, c(2L, 1L))
  expect_true(all(sf::st_is(result, c("POLYGON", "MULTIPOLYGON"))))
  expect_true(all(sf::st_is_valid(result)))
})

test_that("MULTIPOINT coordinates are counted individually", {
  multipoint <- sf::st_sf(
    source = "one row",
    geometry = sf::st_sfc(sf::st_multipoint(matrix(
      c(0, 0, 0.1, 0, 0, 0.1, 0.1, 0.1, 0.05, 0.05),
      ncol = 2,
      byrow = TRUE
    )), crs = 3857)
  )

  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  result <- hotspot_dbscan(
    multipoint,
    eps = 0.2,
    min_pts = 3,
    hull = "convex",
    quiet = TRUE
  )
  expect_identical(result$n, 5L)
  expect_identical(result$prop, 1)
})

test_that("polygon counts include noise points inside the final hull", {
  # The points on the circle form one DBSCAN cluster, while the centre point is
  # noise because it is farther than eps from every point on the circle. The
  # convex cluster polygon nevertheless contains all 25 input coordinates.
  angle <- seq(0, 2 * pi, length.out = 25)[-25]
  coordinates <- rbind(
    cbind(x = 3 * cos(angle), y = 3 * sin(angle)),
    c(x = 0, y = 0)
  )
  data <- sf::st_as_sf(
    data.frame(id = seq_len(nrow(coordinates)), coordinates),
    coords = c("x", "y"),
    crs = 3857
  )

  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  result <- hotspot_dbscan(
    data, eps = 1, min_pts = 3, hull = "convex", quiet = TRUE
  )

  expect_identical(result$n, 25L)
  expect_identical(result$prop, 1)
})

test_that("automatic min_pts uses a bounded square-root rule", {
  expect_identical(set_dbscan_min_pts(2, quiet = TRUE), 2L)
  expect_identical(set_dbscan_min_pts(100, quiet = TRUE), 10L)
  expect_identical(set_dbscan_min_pts(1000, quiet = TRUE), 32L)
  expect_identical(set_dbscan_min_pts(10000, quiet = TRUE), 50L)
  expect_error(set_dbscan_min_pts(1, quiet = TRUE), "At least two")
  expect_message(
    set_dbscan_min_pts(100, quiet = FALSE),
    "Minimum points set automatically"
  )
})

test_that("automatic eps uses adjusted median nearest-neighbour distance", {
  data <- make_dbscan_points()
  coordinates <- sf::st_coordinates(data)[, c("X", "Y"), drop = FALSE]
  expected <- stats::median(dbscan::kNNdist(coordinates, k = 2)) / sqrt(2)

  expect_message(
    value <- set_dbscan_eps(
      coordinates,
      min_pts = 3,
      density_adjust = 2,
      data = data,
      quiet = FALSE
    ),
    "Neighbourhood distance set automatically"
  )
  expect_equal(value, expected)
  expect_no_message(set_dbscan_eps(
    coordinates,
    min_pts = 3,
    density_adjust = 2,
    data = data,
    quiet = TRUE
  ))
})

test_that("automatic eps is recorded in metadata", {
  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  result <- hotspot_dbscan(
    memphis_robberies_jan, hull = "convex", quiet = TRUE
  )
  metadata <- attr(result, "dbscan")

  expect_true(metadata$eps_auto)
  expect_true(is.finite(metadata$eps) && metadata$eps > 0)
  expect_identical(metadata$min_pts, 15L)
  expect_true(metadata$min_pts_auto)
  expect_identical(metadata$density_adjust, 2)
  expect_identical(metadata$hull, "convex")
  expect_null(metadata$hull_ratio)
})

test_that("geographic data are transformed and restored", {
  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  expect_message(
    result <- hotspot_dbscan(
      memphis_robberies_jan, hull = "convex", quiet = FALSE
    ),
    "Data transformed to"
  )
  expect_identical(sf::st_crs(result), sf::st_crs(memphis_robberies_jan))
  expect_error(
    hotspot_dbscan(memphis_robberies_jan, transform = FALSE, quiet = TRUE),
    "Cannot calculate DBSCAN clusters for lon/lat data"
  )
})

test_that("convex hulls are supported", {
  data <- make_dbscan_points()
  convex <- hotspot_dbscan(
    data, eps = 1, min_pts = 3, hull = "convex", quiet = TRUE
  )
  expect_identical(attr(convex, "dbscan")$hull, "convex")
})

test_that("concave hulls require GEOS 3.11", {
  skip_if(
    utils::compareVersion(sf::sf_extSoftVersion()[["GEOS"]], "3.11.0") >= 0,
    "installed GEOS supports concave hulls"
  )
  expect_error(
    hotspot_dbscan(
      make_dbscan_points(),
      eps = 1,
      min_pts = 3,
      hull = "concave",
      quiet = TRUE
    ),
    "Concave cluster hulls require GEOS 3.11.0 or later"
  )
})

test_that("concave hulls are supported", {
  skip_if(
    utils::compareVersion(sf::sf_extSoftVersion()[["GEOS"]], "3.11.0") < 0,
    "concave hulls require GEOS 3.11"
  )
  data <- make_dbscan_points()
  concave <- hotspot_dbscan(
    data,
    eps = 1,
    min_pts = 3,
    hull = "concave",
    hull_ratio = 0.25,
    quiet = TRUE
  )
  expect_identical(attr(concave, "dbscan")$hull, "concave")
  expect_identical(attr(concave, "dbscan")$hull_ratio, 0.25)
  expect_true(all(sf::st_is_valid(concave)))

  complete_hull <- sf::st_concave_hull(
    sf::st_union(data), ratio = 0.25, allow_holes = FALSE
  )
  complete_geometry <- sf::st_intersection(
    sf::st_buffer(complete_hull, 1),
    sf::st_convex_hull(sf::st_union(data))
  )
  expect_equal(
    concave$prop_area,
    as.numeric(sf::st_area(concave)) /
      as.numeric(sf::st_area(complete_geometry))
  )
})

test_that("DBSCAN parameters are validated", {
  data <- make_dbscan_points()
  expect_error(hotspot_dbscan(data, eps = 0), "eps.*greater than zero")
  expect_error(hotspot_dbscan(data, eps = Inf), "eps.*finite")
  expect_error(hotspot_dbscan(data, eps = 1, min_pts = 1), "min_pts")
  expect_error(hotspot_dbscan(data, eps = 1, min_pts = 2.5), "min_pts")
  expect_error(hotspot_dbscan(data, eps = 1, min_pts = 20), "cannot exceed")
  expect_error(
    hotspot_dbscan(data, eps = 1, density_adjust = 0),
    "density_adjust"
  )
  expect_error(hotspot_dbscan(data, eps = 1, hull = "triangle"), "hull")
  expect_error(hotspot_dbscan(data, eps = 1, hull_ratio = -0.1), "hull_ratio")
  expect_error(hotspot_dbscan(data, eps = 1, transform = 1), "transform")
  expect_error(hotspot_dbscan(data, eps = 1, quiet = 1), "quiet")
})

test_that("reserved and supported dot arguments are handled", {
  data <- make_dbscan_points()
  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  expect_no_error(hotspot_dbscan(
    data,
    eps = 1,
    min_pts = 3,
    hull = "convex",
    search = "linear",
    quiet = TRUE
  ))
  expect_no_error(hotspot_dbscan(
    data,
    eps = 1,
    min_pts = 3,
    hull = "convex",
    borderPoints = FALSE,
    quiet = TRUE
  ))
  expect_error(hotspot_dbscan(data, eps = 1, weights = 1), "not supported")
  expect_error(hotspot_dbscan(data, eps = 1, minPts = 3), "reserved")
  expect_error(
    hotspot_dbscan(
      data = data,
      eps = 1,
      min_pts = 3,
      density_adjust = 1,
      hull = "convex",
      hull_ratio = 0.5,
      transform = TRUE,
      quiet = TRUE,
      10
    ),
    "must be named"
  )
})

test_that("all-noise and coincident automatic inputs give useful errors", {
  data <- make_dbscan_points()
  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  expect_error(
    hotspot_dbscan(
      data, eps = 0.01, min_pts = 3, hull = "convex", quiet = TRUE
    ),
    "No DBSCAN hotspots"
  )

  coincident <- sf::st_as_sf(
    data.frame(x = rep(1, 5), y = rep(1, 5)),
    coords = c("x", "y"),
    crs = 3857
  )
  expect_error(
    hotspot_dbscan(coincident, hull = "convex", quiet = TRUE),
    "finite positive automatic.*eps"
  )

  collinear <- sf::st_as_sf(
    data.frame(x = 1:5, y = 1:5), coords = c("x", "y"), crs = 3857
  )
  expect_s3_class(
    hotspot_dbscan(
      collinear, density_adjust = 0.25, hull = "convex", quiet = TRUE
    ),
    "hspt_s"
  )
})

test_that("hspt_s class and metadata survive hotspot_clip", {
  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  result <- hotspot_dbscan(
    make_dbscan_points(),
    eps = 1,
    min_pts = 3,
    hull = "convex",
    quiet = TRUE
  )
  boundary <- sf::st_as_sf(sf::st_sfc(
    sf::st_polygon(list(matrix(
      c(-5, -5, 5, -5, 5, 5, -5, 5, -5, -5),
      ncol = 2,
      byrow = TRUE
    ))),
    crs = 3857
  ))
  clipped <- hotspot_clip(result, boundary, quiet = TRUE)

  expect_s3_class(clipped, "hspt_s")
  expect_identical(attr(clipped, "dbscan"), attr(result, "dbscan"))
})

test_that("DBSCAN results cannot be converted to isobands", {
  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  result <- hotspot_dbscan(
    make_dbscan_points(),
    eps = 1,
    min_pts = 3,
    hull = "convex",
    quiet = TRUE
  )
  expect_error(hotspot_isoband(result, quiet = TRUE), "cannot be converted")
})

test_that("DBSCAN results have configurable plot fills and labels", {
  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  result <- hotspot_dbscan(
    make_dbscan_points(),
    eps = 1,
    min_pts = 3,
    hull = "convex",
    quiet = TRUE
  )

  expect_s3_class(autoplot(result), "ggplot")
  expect_equal(autolayer(result)[[1]]$data$.plot_value, result$n)
  expect_equal(autolayer(result, col_fill = "prop")[[1]]$data$.plot_value,
               result$prop)
  expect_equal(autolayer(result, col_fill = "rank")[[1]]$data$.plot_value,
               result$rank)
  expect_equal(
    autoplot(result)$scales$get_scales("fill")$palette(c(0, 1)),
    c("#EFF3FF", "#084594")
  )

  expect_length(autoplot(result)$layers, 1)
  expect_equal(autolayer(result, col_label = "n")[[2]]$data$.plot_label,
               as.character(result$n))
  expect_equal(autolayer(result, col_label = "prop")[[2]]$data$.plot_label,
               c("33%", "33%"))
  expect_equal(autolayer(result, col_label = "rank")[[2]]$data$.plot_label,
               c("1st", "2nd"))
  expect_equal(
    autolayer(result, col_label = c("n", "prop"))[[2]]$data$.plot_label,
    c("n = 4\n33%", "n = 4\n33%")
  )
  expect_equal(
    autolayer(result, col_label = c("rank", "n"))[[2]]$data$.plot_label,
    c("1st\nn = 4", "2nd\nn = 4")
  )
  expect_equal(
    format_dbscan_labels(c(0.1514, 0.1171, 0.07349, 0.03697), "prop"),
    c("15%", "12%", "7.3%", "3.7%")
  )
  light_label <- autolayer(result, col_label = "prop")[[2]]
  expect_s3_class(light_label$geom, "GeomLabel")
  expect_equal(light_label$aes_params$fill, "#FFFFFFBF")
  expect_equal(light_label$aes_params$colour, "black")
  expect_equal(light_label$aes_params$linewidth, 0)

  dark_plot <- autoplot(
    result,
    col_label = "prop",
    basemap_type = "cartodark"
  )
  dark_label <- dark_plot$layers[[length(dark_plot$layers)]]
  expect_s3_class(dark_label$geom, "GeomLabel")
  expect_equal(dark_label$aes_params$fill, "#333333BF")
  expect_equal(dark_label$aes_params$colour, "white")
  expect_length(autoplot(result, col_label = "rank")$layers, 2)
  expect_no_condition(
    ggplot2::ggplot_build(autoplot(result, col_label = "prop"))
  )

  outline <- autolayer(result, col_fill = "none")
  expect_false(".plot_value" %in% names(outline[[1]]$data))
  expect_true(is.na(outline[[1]]$aes_params$fill))
  expect_equal(outline[[1]]$aes_params$linewidth, 0.8)
  expect_false("colour" %in% names(outline[[1]]$mapping))
  expect_null(outline[[1]]$aes_params$colour)
  custom_outline <- autolayer(result, col_fill = "none", colour = "red")
  expect_equal(custom_outline[[1]]$aes_params$colour, "red")
  static_fill <- autolayer(result, fill = "red")
  expect_equal(static_fill[[1]]$aes_params$fill, "red")
  outline_plot <- autoplot(
    result,
    col_fill = "none",
    col_label = c("rank", "n")
  )
  expect_null(outline_plot$scales$get_scales("fill"))
  expect_null(outline_plot$scales$get_scales("colour"))
  expect_null(outline_plot$labels$colour)
  proportion_plot <- autoplot(result, col_fill = "prop")
  expect_equal(proportion_plot$labels$fill, "proportion")
  expect_equal(
    proportion_plot$scales$get_scales("fill")$labels(
      c(0.03, 0.06, 0.09, 0.12, 0.15)
    ),
    c("3%", "6%", "9%", "12%", "15%")
  )

  geographic <- sf::st_transform(result, 4326)
  expect_no_warning(
    ggplot2::ggplot_build(autoplot(geographic, col_label = "n"))
  )
})

test_that("DBSCAN plotting arguments and columns are validated", {
  # Use convex hulls so this non-concave test also runs with GEOS < 3.11.
  result <- hotspot_dbscan(
    make_dbscan_points(),
    eps = 1,
    min_pts = 3,
    hull = "convex",
    quiet = TRUE
  )

  expect_error(autoplot(result, col_fill = "density"), "col_fill")
  expect_error(autolayer(result, col_label = "cluster"), "col_label")
  expect_error(
    autolayer(result, col_label = c("none", "n")),
    "cannot be combined"
  )
  expect_error(autolayer(result, col_label = c("n", "n")), "duplicated")
  expect_error(autolayer(result[, "geometry"]), "n")
  result$n <- as.character(result$n)
  expect_error(autoplot(result), "must be numeric")
})

Try the sfhotspot package in your browser

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

sfhotspot documentation built on Oct. 1, 2026, 1:06 a.m.