Skip to content

FR: Implement by_feature to st_union() when x and y are provided #2259

Closed
@olivroy

Description

@olivroy

Hi,

I was wondering if it would be possible to implement a method by_feature argument to st_union() when x and y a provided.

My use case would be to have two sf objects as data frame. and to compute pairwise union

I have 2 shapefiles that have the same number of rows. I want to create a line for each row.

I can't figure out how to do it.

Here is my example code

library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
# For the sake of reproducibility
tab1 <- data.frame(
  long = c(-129, -80, -100),
  lat = c(56, 69, 60)
)
tab2 <- data.frame(
  long = c(-100, -90, -120),
  lat = c(59, 75, 62)
)
my_x <- sf::st_as_sf(tab1, coords = c("long", "lat"), crs = 4326)
my_y <- sf::st_as_sf(tab2, coords = c("long", "lat"), crs = 4326)

# I have to start with sf objects

my_x
#> Simple feature collection with 3 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -129 ymin: 56 xmax: -80 ymax: 69
#> Geodetic CRS:  WGS 84
#>          geometry
#> 1 POINT (-129 56)
#> 2  POINT (-80 69)
#> 3 POINT (-100 60)
my_y
#> Simple feature collection with 3 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -120 ymin: 59 xmax: -90 ymax: 75
#> Geodetic CRS:  WGS 84
#>          geometry
#> 1 POINT (-100 59)
#> 2  POINT (-90 75)
#> 3 POINT (-120 62)

sf::st_join(my_x, my_y)
#> Simple feature collection with 3 features and 0 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -129 ymin: 56 xmax: -80 ymax: 69
#> Geodetic CRS:  WGS 84
#>          geometry
#> 1 POINT (-129 56)
#> 2  POINT (-80 69)
#> 3 POINT (-100 60)

# Doesn't work. it just does all combinations...
sf::st_union(my_x, my_y)
#> Simple feature collection with 9 features and 0 fields
#> Geometry type: MULTIPOINT
#> Dimension:     XY
#> Bounding box:  xmin: -129 ymin: 56 xmax: -80 ymax: 75
#> Geodetic CRS:  WGS 84
#>                         geometry
#> 1 MULTIPOINT ((-100 59), (-12...
#> 2 MULTIPOINT ((-80 69), (-100...
#> 3 MULTIPOINT ((-100 60), (-10...
#> 4 MULTIPOINT ((-90 75), (-129...
#> 5 MULTIPOINT ((-80 69), (-90 ...
#> 6 MULTIPOINT ((-90 75), (-100...
#> 7 MULTIPOINT ((-120 62), (-12...
#> 8 MULTIPOINT ((-80 69), (-120...
#> 9 MULTIPOINT ((-100 60), (-12...


# I would like a linestring sf object of length 3

Created on 2023-11-09 with reprex v2.0.2

Maybe

st_union(my_x, my_y, by_feature= TRUE)

could work?

Similar to sf::st_distance()

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions