我有两个具有相同行数的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()
,但我不能使用该解决方案。
1条答案
按热度按时间cclgggtu1#
对于成对联合,你可以使用
st_union()
到Map()
/mapply()
,如果这些输入形状包含你想要保留的任何属性,也许可以从cbind()
开始。Map()
返回值是一个多点列表,它必须转换回sfc,然后可以转换为linestrings:字符串
测试结果:
型
创建于2023-11-10使用reprex v2.0.2