沿线生成经纬度 shapefile

generate lat-lon along a line shapefile

我有一个来自以下位置的道路形状文件

https://www.globio.info/download-grip-dataset

我下载了北美的 shapefile 并将加拿大的道路子集化如下:

  library(raster)
  library(ggplot2)
  library(sf)
  library(sp)
  road <- read_sf(file.path(dir_ls$base,"roadshapefile","GRIP4_Region1_vector_shp","GRIP4_region1.shp"))

  road_canada <- road %>%
                  dplyr::filter(GP_RTP %in% c(1,2), # 1 for highways 
                                GP_REX == 1, # 1 for Open 
                                GP_RRG == 1, # 1 is for Canada
                                GP_RCY == 124 # for Canada
                  ) 

  ggplot(road_canada) + geom_sf()

我想在道路上生成 20 个随机 latlon

  spsample(road_canada, n=20, type="random")

  Error in (function (classes, fdef, mtable)  : 
              unable to find an inherited method for function ‘spsample’ for signature ‘"sf"’
            

我了解到由于 sf class 导致的错误,此方法无效。我不知道 任何其他能让我做到这一点的方法,想知道是否有人知道任何替代方法?

使用 st_sample 可行,但并不完全简单。道路数据的大小使其难以使用 in-memory。

下面的road_canada对象被简化,仅使用几何列,组合,采样,最后不得不转换为POINT才能显示坐标。

# Use only the geometry column, simplify for a smaller object.
road_canada <- st_geometry(road_canada) %>% rmapshaper::ms_simplify()

road_sample <- road_canada %>%
  st_combine() %>%
  st_sample(20) %>%  ## Returns MULTIPOINT EMPTY, fixed with st_cast('POINT')
  st_cast('POINT')

head(road_sample)
#Geometry set for 6 features 
#Geometry type: POINT
#Dimension:     XY
#Bounding box:  xmin: -117.3209 ymin: 52.32145 xmax: -109.5217 ymax: 58.59094
#Geodetic CRS:  WGS 84
#First 5 geometries:
#POINT (-109.5217 52.4055)
#POINT (-117.3209 52.32145)
#POINT (-112.3959 53.01749)
#POINT (-112.9708 53.5703)
#POINT (-113.9 54.38767)