R语言 如何从2个SF对象创建一条线

sxissh06  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(108)

我有两个具有相同行数的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
我很难在科幻小说的复杂性中导航,这些资源对我来说几乎是压倒性的。
重要的是要注意,我的对象是 Dataframe (sf对象),我看到我们可以提供一个矩阵sf::st_linestring(),但我不能使用该解决方案。

cclgggtu

cclgggtu1#

对于成对联合,你可以使用st_union()Map()/mapply(),如果这些输入形状包含你想要保留的任何属性,也许可以从cbind()开始。Map()返回值是一个多点列表,它必须转换回sfc,然后可以转换为linestrings:

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_as_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

相关问题