如何从 2 个 sf 对象创建一条线

问题描述 投票:0回答:1

我有 2 个具有相同行数的 shapefile。我想为每一行创建一条线。

我不知道该怎么做。

这是我的示例代码

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

创建于 2023-11-09,使用 reprex v2.0.2

我很难理解科幻小说的复杂性。这些资源对我来说大多是压倒性的。

需要注意的是,我的对象是数据框(sf 对象),我看到我们可以向

sf::st_linestring()
提供矩阵,但我无法使用该解决方案。

r geospatial r-sf
1个回答
0
投票

对于成对并集,您可以使用

st_union()
Map()
/
mapply()
,如果这些输入形状包含您想要保留的任何属性,则可以从
cbind()
开始。
Map()
返回值是多点列表,必须将其转换回 sfc,然后可以将其转换为线串:

library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
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)

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

xy$geom_line <- 
  Map(st_union, xy$geometry, xy$geometry.1) |> 
  st_sfc(crs = st_crs(xy)) |>
  st_cast("LINESTRING")

# switch active geometry to geom_line:
st_geometry(xy) <- "geom_line"
xy
#> Simple feature collection with 3 features and 0 fields
#> Active geometry column: geom_line
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -129 ymin: 56 xmax: -80 ymax: 75
#> Geodetic CRS:  WGS 84
#>          geometry      geometry.1                     geom_line
#> 1 POINT (-129 56) POINT (-100 59) LINESTRING (-129 56, -100 59)
#> 2  POINT (-80 69)  POINT (-90 75)   LINESTRING (-80 69, -90 75)
#> 3 POINT (-100 60) POINT (-120 62) LINESTRING (-100 60, -120 62)

结果:

# without input geometries:
subset(xy, select = -c(geometry, geometry.1))
#> Simple feature collection with 3 features and 0 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: -129 ymin: 56 xmax: -80 ymax: 75
#> Geodetic CRS:  WGS 84
#>                       geom_line
#> 1 LINESTRING (-129 56, -100 59)
#> 2   LINESTRING (-80 69, -90 75)
#> 3 LINESTRING (-100 60, -120 62)

创建于 2023 年 11 月 10 日,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.