`st_union(by_feature = TRUE)` between polygon and point to get a line string between point and closest point on polygon
Opened this issue · 2 comments
olivroy commented
Similar to this question on Stack Overflow:
I wonder if it was possible to get a line from combining a polygon and a point geometry. #2259
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
nc <- st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source
#> `C:\Users\RoyO\AppData\Local\R\win-library\4.4\sf\shape\nc.shp'
#> using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS: NAD27
nc2 <- st_centroid(nc)
#> Warning: st_centroid assumes attributes are constant over geometries
sf::st_union(
nc,
nc2,
by_feature = TRUE
)
#> Error in `st_geometry<-.data.frame`(`*tmp*`, value = value): nrow(x) == length(value) n'est pas TRUE
Created on 2024-08-26 with reprex v2.1.1
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.4.1 (2024-06-14 ucrt)
#> os Windows 10 x64 (build 19045)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate French_Canada.utf8
#> ctype French_Canada.utf8
#> tz America/Toronto
#> date 2024-08-26
#> pandoc 3.2 @ C:/Users/~/AppData/Local/Programs/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> class 7.3-22 2023-05-03 [2] CRAN (R 4.4.1)
#> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.4.1)
#> cli 3.6.3 2024-06-21 [1] CRAN (R 4.4.1)
#> DBI 1.2.3 2024-06-02 [1] CRAN (R 4.4.1)
#> digest 0.6.37 2024-08-19 [1] CRAN (R 4.4.1)
#> e1071 1.7-14 2023-12-06 [1] CRAN (R 4.4.1)
#> evaluate 0.24.0 2024-06-10 [1] CRAN (R 4.4.1)
#> fastmap 1.2.0 2024-05-15 [1] CRAN (R 4.4.1)
#> fs 1.6.4 2024-04-25 [1] CRAN (R 4.4.1)
#> glue 1.7.0 2024-01-09 [1] CRAN (R 4.4.1)
#> htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.4.1)
#> KernSmooth 2.23-24 2024-05-17 [2] CRAN (R 4.4.1)
#> knitr 1.48 2024-07-07 [1] CRAN (R 4.4.1)
#> lifecycle 1.0.4 2024-08-20 [1] Github (r-lib/lifecycle@9302cea)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.1)
#> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.4.1)
#> Rcpp 1.0.13 2024-07-17 [1] CRAN (R 4.4.1)
#> reprex 2.1.1 2024-07-06 [1] CRAN (R 4.4.1)
#> rlang 1.1.4 2024-06-04 [1] CRAN (R 4.4.1)
#> rmarkdown 2.28 2024-08-17 [1] CRAN (R 4.4.1)
#> rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.4.1)
#> s2 1.1.7 2024-07-17 [1] CRAN (R 4.4.1)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.1)
#> sf * 1.0-16 2024-03-24 [1] CRAN (R 4.4.1)
#> units 0.8-5 2023-11-28 [1] CRAN (R 4.4.1)
#> withr 3.0.1 2024-07-31 [1] CRAN (R 4.4.1)
#> wk 0.9.2 2024-07-09 [1] CRAN (R 4.4.1)
#> xfun 0.47 2024-08-17 [1] CRAN (R 4.4.1)
#> yaml 2.3.10 2024-07-26 [1] CRAN (R 4.4.1)
#>
#> [1] C:/Users/~/AppData/Local/R/win-library/4.4
#> [2] C:/Program Files/R/R-4.4.1/library
#>
#> ──────────────────────────────────────────────────────────────────────────────
I am wondering if there is a simple solution in sf, because st_distance(by_feature = TRUE)
works pretty well at giving the resulting length.
olivroy commented
Does st_line_interpolate()
work? #2291 (comment)
olivroy commented
I just found st_nearest_points()
, I wonder if a by_feature
argument could be added?
Edit: pairwise
exists. also s2::s2_minimum_clearance_line_between()