GRTS for large spatial datasets in R

I am trying to apply the generalized random tessellated sampling (GRTS) algorithm implemented in spsurvey to sample a dataset of 3 million points on a map. I am running into many vector memory exhausted errors and occasionally, on other attempts, R simply crashes without warning.

In this MWE with 1,000,000 points, you can reproduce the error and see that editing the R environment file doesn’t seem to address the problem. The objective is to sample down to 250,000 points or fewer depending on what the dataset supports given the minimum 3 km distance between points constraint. Is there another package that has the same funtionality but is more efficient than spsurvey? For example, it doesn’t appear that terra has a GRTS implementation, although often seems to be more efficient than sf.

library(usethis)
edit_r_environ()
# write following line in r environ file and restart:
# R_MAX_VSIZE=100Gb

library(spsurvey)

mwe <- data.frame(geometry = paste0("POINT (", 3000001:4000000, " ", -1500001:-2500000, ")"),
                  property = rnorm(1000000)) %>% 
  st_as_sf(wkt = "geometry", crs = 5070)
  
samples <- grts(mwe, n_base=75000, mindis=3000)

# Error: vector memory exhausted (limit reached?)

Update: turns out that 100 Gb is not nearly enough memory to sample this many points with this implementation of the GRTS algorithm. Sampling ~150,000 points took a little over 330 Gb of RAM. If there are more efficient ways of achieving the same objective, I would very much like to know!

2

I’m not familiar with grts sampling, but I did find an implementation using grtsdb https://inbo.github.io/grtsdb/index.html that might help you.

It seems to work, and relatively quickly on the sample data you provided. You’ll need to double check the sampling results and see if they meet your requirements. An (extremely) simple check was run in the code below.

library(dplyr) # for %>% pipe
library(sf)
library(grtsdb) # https://inbo.github.io/grtsdb/index.html
library(DBI)    
library(RSQLite)
library(ggplot2)

# your example data from question
mwe <- data.frame(geometry = paste0("POINT (", 3000001:4000000, " ", -1500001:-2500000, ")"),
                  property = rnorm(1000000)) %>% 
  st_as_sf(wkt = "geometry", crs = 5070)

# write mwe to sqlite database
mwedb <- dbConnect(RSQLite::SQLite(), "mwedb.sqlite")
dbWriteTable(mwedb, 'mwe', mwe)
mwe_dbfile <- 'mwedb.sqlite'
db <- connect_db(mwe_dbfile)

# grtsdb requires a bounding box as an argument
my_bbox <- rbind(
  c(3000001, 4000000),
  c(-2500000, -1500001)
)

# use extract_sample from grtsdb
ex_sample <- extract_sample(grtsdb = db, 
                            samplesize = 75000, 
                            bbox = my_bbox, 
                            cellsize = 3000)

# size & shape of ex_sample
dim(ex_sample)
#[1] 75000     3

head(ex_sample)
#       x1c      x2c ranking
#1 3705500 -2154500       1
#2 3828500 -1773500       2
#3 3216500 -2460500       3
#4 3054500 -2277500       7
#5 3555500 -1524500      10
#6 3261500 -2106500      11

# check distances from at least one point to all others, 
#  here we just use the first point in ex_sample
#  to all others, the shortest is 3000 

ex_sample_sf <- st_as_sf(ex_sample, coords = c("x1c", "x2c"))
near_points <- st_nearest_points(ex_sample_sf[1,],
                                 ex_sample_sf[-1,]) %>%
  st_as_sf() %>%
  mutate(length = st_length(.)) %>%
  arrange(length)

head(near_points, 2)

#Simple feature collection with 2 features and 1 field
#Geometry type: LINESTRING
#Dimension:     XY
#Bounding box:  xmin: 3705500 ymin: -2154500 xmax: 3708500 ymax: -2151500
#CRS:           NA
#    length                              x
#1 3000.000 LINESTRING (3705500 -215450...
#2 4242.641 LINESTRING (3705500 -215450...

# Plotting
ggplot() +
  geom_sf(data = ex_sample_sf, size = .1, alpha = .1) +
  geom_sf(data = near_points[1:2,], color = 'red') +
  geom_sf(data = ex_sample_sf[1,], color = 'blue') +
  ggtitle("all sampled points, first point in blue")

ggplot() +
  geom_sf(data = ex_sample_sf, size = 1, color = 'black') +
  geom_sf(data = near_points[1:2,], color = 'red') +
  geom_sf(data = ex_sample_sf[1,], color = 'blue') +
  coord_sf(xlim = c(3700000, 3710000), ylim = c(-2150000, -2160000)) +
  ggtitle("zoom in on first point & 2 nearest")

Showing all 75,000 sampled points, first one in blue. Lines are drawn to its two nearest neighbors, but they don’t show at this level of detail. All points look pretty well spaced out.

Here zoomed in on the first point, with the lines to the two nearest neighbors visible. The closest is 3000 (units?)

Hope this helps.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật