r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.35k stars 299 forks source link

Small scoping bug in `st_filter()` #2416

Closed kbvernon closed 3 months ago

kbvernon commented 4 months ago

Describe the bug If the sf table includes x and y variables, they get selected by the predicate function instead of the geometry.

To Reproduce

library(dplyr)
library(sf)

a <- st_sfc(
  st_point(c(1,1)), 
  st_point(c(2,2)), 
  st_point(c(3,3))
) |> st_sf(a = 1:3, geometry = _)

b <- st_sfc(
  st_point(c(10,10)), 
  st_point(c(2,2)), 
  st_point(c(2,2)), 
  st_point(c(3,3))
) |> st_sf(a = 11:14, geom = _)

xy <- st_coordinates(a) |> 
  as_tibble() |> 
  rename_with(str_to_lower)

a <- bind_cols(a, xy)

st_filter(a |> select(-x, -y), b)
# Simple feature collection with 2 features and 1 field
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 2 ymin: 2 xmax: 3 ymax: 3
# CRS:           NA
#   a    geometry
# 1 2 POINT (2 2)
# 2 3 POINT (3 3)

st_filter(a, b)
# Error in `stopifnot()`:
# ℹ In argument: `lengths(.predicate(x, y, ...)) > 0`.
# Caused by error in `UseMethod()`:
# ! no applicable method for 'st_intersects' applied to an object of class "c('double', 'numeric')"
# Run `rlang::last_trace()` to see where the error occurred.

If reporting a change from previous versions NA

Additional context From Edzer on Discord: "it seems like .predicate(!!x, !!y, ...) in st_filter.sf solves the issue"

```r > sessionInfo() R version 4.3.1 (2023-06-16 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 11 x64 (build 22631) Matrix products: default locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C [5] LC_TIME=English_United States.utf8 time zone: America/Denver tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] dplyr_1.1.4 sf_1.0-16 loaded via a namespace (and not attached): [1] utf8_1.2.4 R6_2.5.1 tidyselect_1.2.1 e1071_1.7-14 [5] magrittr_2.0.3 glue_1.7.0 tibble_3.2.1 KernSmooth_2.23-21 [9] pkgconfig_2.0.3 generics_0.1.3 lifecycle_1.0.4 classInt_0.4-10 [13] cli_3.6.2 fansi_1.0.6 grid_4.3.1 vctrs_0.6.5 [17] DBI_1.2.3 proxy_0.4-27 class_7.3-22 compiler_4.3.1 [21] rstudioapi_0.16.0 tools_4.3.1 pillar_1.9.0 Rcpp_1.0.12 [25] rlang_1.1.4 units_0.8-5 > sf::sf_extSoftVersion() GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H PROJ "3.11.2" "3.8.2" "9.3.1" "true" "true" "9.3.1" ```
edzer commented 3 months ago

Thanks; I think this has been fixed in https://github.com/r-spatial/sf/commit/84f3a3dcfb32e762ab37836cad4278367e33ac23

edzer commented 3 months ago

Quite likely we'll have to do the same for st_join()?