Skip to content

New syntax to translate geometry N by values N? #2499

@ThomasSoeiro

Description

@ThomasSoeiro

As previously discussed in #903 (comment), the behavior of affine transformations is not homogeneous depending on the operation:

  • translating with a vector of length two translate all geometries with these two values
  • scaling with a vector of length N scales geometry N by value N

Would it be possible to add another syntax, e.g. st_geometry(*) + list(<vector>, <vector>) that would translate geometry N by values N? This would make more straightforward to translate geometries in a data frame using data from other columns. One additional question is how vectors of length one should be treated (i.e. recycle or throw an error).

To Reproduce

library(sf)

nc <- st_read(system.file("shape/nc.shp", package = "sf"))
ncg <- st_geometry(nc)
plot(ncg, border = "grey")

# scale
cntrd <- st_centroid(ncg)
ncg2 <- (ncg - cntrd) * nc$AREA + cntrd
plot(ncg2, col = "blue", add = TRUE)

# translate
nc$move_east <- ifelse(nc$AREA > median(nc$AREA), 1, -1)
nc$move_north <- ifelse(nc$AREA > median(nc$AREA), 2, -2)
ncg[] <- Map(\(obj, x, y) obj + c(x, y), ncg, nc$move_east, nc$move_north)
# or with the new syntax proposed here:
# ncg <- ncg + list(nc$move_east, nc$move_north)

plot(ncg, col = "red", add = TRUE)

Image

Paste the output of your `sessionInfo()` and `sf::sf_extSoftVersion()`
sessionInfo()
# R version 4.4.0 (2024-04-24 ucrt)
# Platform: x86_64-w64-mingw32/x64
# Running under: Windows 10 x64 (build 19045)
# 
# Matrix products: default
# 
# 
# locale:
# [1] LC_COLLATE=French_France.utf8  LC_CTYPE=French_France.utf8    LC_MONETARY=French_France.utf8 LC_NUMERIC=C                  
# [5] LC_TIME=French_France.utf8    
# 
# time zone: Europe/Paris
# tzcode source: internal
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] sf_1.0-19
# 
# loaded via a namespace (and not attached):
#  [1] compiler_4.4.0     magrittr_2.0.3     class_7.3-22       s2_1.1.7           DBI_1.2.2          tools_4.4.0       
#  [7] units_0.8-5        proxy_0.4-27       rstudioapi_0.17.1  wk_0.9.4           Rcpp_1.0.12        KernSmooth_2.23-24
# [13] grid_4.4.0         e1071_1.7-14       classInt_0.4-11   

sf::sf_extSoftVersion()
      #     GEOS           GDAL         proj.4 GDAL_with_GEOS     USE_PROJ_H           PROJ 
      # "3.12.2"        "3.9.3"        "9.4.1"         "true"         "true"        "9.4.1" 

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions