Skip to content

Error in sf.kde function #74

Description

@pereGelabert

Dear all,
I am writing to report a bug where sf.kde fails with a "CRS do not match" error, despite the input sf object and the reference SpatRaster sharing the exact same Coordinate Reference System (EPSG:25831). With spatialEco_2.0-1 version runs without any error.

  1. Data Overview
    Reference Raster (ref = r.ref):
    class : SpatRaster
    dimensions : 617, 415, 1 (nrow, ncol, nlyr)
    resolution : 20, 20 (x, y)
    extent : 403912.4, 412212.4, 4612836, 4625176 (xmin, xmax, ymin, ymax)
    coord. ref. : ETRS89 / UTM zone 31N (EPSG:25831)
    source : cbd.tif
    Point Data (x = ignit_20):
    Simple feature collection with 343 features and 25 fields
    Geometry type: POINT
    Dimension: XY
    Bounding box: xmin: 396200 ymin: 4605400 xmax: 419000 ymax: 4632947
    Projected CRS: ETRS89 / UTM zone 31N

  2. CRS Verification
    Before running the density function, I verified that the sf coordinate system natively matches the terra raster coordinate system:

st_crs(ignit_20) == st_crs(r.ref)
[1] TRUE

  1. The Code and Error
    When executing sf.kde, the function immediately halts and throws a CRS mismatch error:
    R

dens <- sf.kde(

  • x = ignit_20,
  • ref = r.ref,
  • res = 20,
  • bw = fitted_variogram$var_model$range[2],
  • standardize = F,
  • scale.factor = 1
  • )

Using r.ref as reference raster
Error in h(simpleError(msg, call)):
error in evaluating the argument 'x' in selecting a method for function 'sf.kde': CRS do not match
Suspected Cause
Because st_crs(ignit_20) == st_crs(r.ref) evaluates to TRUE, it seems the internal CRS check inside sf.kde might be applying an overly strict comparison (perhaps comparing WKT strings directly or converting formats under the hood) between the sf and terra objects, resulting in a false negative.
I have attached my sessionInfo() below for your reference. Please let me know if you need a reproducible example or if there is a known workaround.

Thank you for your time and for your work on the package.

Best regards,
Pere Gelabert

sessionInfo()
R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Spain.utf8 LC_CTYPE=Spanish_Spain.utf8
[3] LC_MONETARY=Spanish_Spain.utf8 LC_NUMERIC=C
[5] LC_TIME=Spanish_Spain.utf8

time zone: Europe/Madrid
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] tmap_3.3-4 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[6] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1
[11] tidyverse_2.0.0 gstat_2.1-2 terra_1.7-78 sf_1.1-0 spatialEco_2.0-5

loaded via a namespace (and not attached):
[1] gtable_0.3.5 raster_3.6-30 htmlwidgets_1.6.4 lattice_0.22-6
[5] tzdb_0.4.0 vctrs_0.6.5 tools_4.4.1 crosstalk_1.2.1
[9] generics_0.1.3 parallel_4.4.1 proxy_0.4-27 spacetime_1.3-2
[13] fansi_1.0.6 xts_0.14.0 pkgconfig_2.0.3 KernSmooth_2.23-24
[17] RColorBrewer_1.1-3 leaflet_2.2.2 lifecycle_1.0.4 compiler_4.4.1
[21] FNN_1.1.4.1 automap_1.1-12 munsell_0.5.1 codetools_0.2-20
[25] leafsync_0.1.0 stars_0.6-6 htmltools_0.5.8.1 class_7.3-22
[29] pillar_1.9.0 classInt_0.4-10 lwgeom_0.2-14 abind_1.4-8
[33] tidyselect_1.2.1 digest_0.6.37 stringi_1.8.4 fastmap_1.2.0
[37] grid_4.4.1 colorspace_2.1-1 cli_3.6.3 magrittr_2.0.3
[41] base64enc_0.1-3 dichromat_2.0-0.1 XML_3.99-0.17 utf8_1.2.4
[45] leafem_0.2.3 e1071_1.7-16 withr_3.0.1 scales_1.3.0
[49] sp_2.1-4 timechange_0.3.0 png_0.1-8 zoo_1.8-12
[53] hms_1.1.3 tmaptools_3.1-1 viridisLite_0.4.2 rlang_1.1.4
[57] Rcpp_1.0.13 glue_1.8.0 DBI_1.2.3 reshape_0.8.9
[61] rstudioapi_0.16.0 plyr_1.8.9 R6_2.5.1 intervals_0.15.5
[65] units_0.8-5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions