R loop for spatial sampling grid

Hello team,

I am quite new at R and am working on some spatial data for my MSc project.

Help would be greatly appreciated!!

I am wanting to great a regular grid of coordinate points at a distance of 50m between each over a specified sampling area.

I currently done the following
Steps taken.

  1. Specify an area
  2. Make regular matrix of points.
  3. Calculating a distance matrix
  4. Adjust number of points until the distance matrix has a minimum distance between points of 50m.

I am wondering if there is loop I could use to adjust the code so that I don't have to manually do step 6? (it is taking a while and i might changed the 50m to 40m)

I currently have the following code

#loading packages
library(tidyverse)
library(rgdal)
library(raster)
library(geosphere)

# making a bounding box of the area of interest
boundbox <- as(raster::extent(174.00117, 174.01626, -39.39970, -39.39005), "SpatialPolygons")

# setting projection and datum
proj4string(boundbox) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

### Now I need points for the sampling plots....
## the area is **about**, 1,357,167 metres squered.  - is is not exact as I only measured it from google earth by hand. 

# generating a sample of regular points in the bounded area
nplots <- spsample(boundbox, n = 670, type ="regular")

# checking the distance between points
dist.matrix <- distm(nplots, fun = distGeo) # generating a distance matrix- 

plot.dist <- dist.matrix[1,2] 
# 40 is the shortest distance between two points
#now i need to adjust manually

Thank you heaps !!

Here's my hacky solution. First, decrease the number of points by 10 to find a near optimum then search from that number+9 decreasing by 1.

#loading packages
library(tidyverse)
library(rgdal)
#> Loading required package: sp
#> Please note that rgdal will be retired by the end of 2023,
#> plan transition to sf/stars/terra functions using GDAL and PROJ
#> at your earliest convenience.
#> 
#> rgdal: version: 1.5-27, (SVN revision 1148)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
#> Path to GDAL shared files: C:/Program Files/R/R-4.1.2/library/rgdal/gdal
#> GDAL binary built with GEOS: TRUE 
#> Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
#> Path to PROJ shared files: C:/Program Files/R/R-4.1.2/library/rgdal/proj
#> PROJ CDN enabled: FALSE
#> Linking to sp version:1.4-6
#> To mute warnings of possible GDAL/OSR exportToProj4() degradation,
#> use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
#> Overwritten PROJ_LIB was C:/Program Files/R/R-4.1.2/library/rgdal/proj
library(raster)
#> 
#> Attaching package: 'raster'
#> The following object is masked from 'package:dplyr':
#> 
#>     select
library(geosphere)

# making a bounding box of the area of interest
boundbox <- as(raster::extent(174.00117, 174.01626, -39.39970, -39.39005), "SpatialPolygons")

# setting projection and datum
proj4string(boundbox) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"

### Now I need points for the sampling plots....
## the area is **about**, 1,357,167 metres squered.  - is is not exact as I only measured it from google earth by hand. 

meetCrit <- FALSE

npts <- 670 # number of points
while (!meetCrit){
   # generating a sample of regular points in the bounded area
   nplots <- spsample(boundbox, n = npts, type ="regular")
   
   # checking the distance between points
   dist.matrix.2 <- dist.matrix <- distm(nplots, fun = distGeo) # generating a distance matrix- 
   diag(dist.matrix.2) <- NA
   if (min(dist.matrix.2, na.rm=TRUE) >50){
      meetCrit <- TRUE
      break
   } else{
      npts <- npts-10 # step down by 10
   }
   
}
#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

# now we know the best is between npts and npts+9
npts
#> [1] 430
npts <- npts+9
meetCrit <- FALSE
while (!meetCrit){
   # generating a sample of regular points in the bounded area
   nplots <- spsample(boundbox, n = npts, type ="regular")
   
   # checking the distance between points
   dist.matrix.2 <- dist.matrix <- distm(nplots, fun = distGeo) # generating a distance matrix- 
   diag(dist.matrix.2) <- NA
   if (min(dist.matrix.2, na.rm=TRUE) >50){
      meetCrit <- TRUE
      break
   } else{
      npts <- npts-1 # step down by 1
   }
   
}
#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

#> Warning in proj4string(obj): CRS object has comment, which is lost in output; in tests, see
#> https://cran.r-project.org/web/packages/sp/vignettes/CRS_warnings.html

npts
#> [1] 432
min(dist.matrix.2, na.rm=TRUE)
#> [1] 50.01003

Created on 2021-12-20 by the reprex package (v2.0.1)

1 Like

Thank you heaps StatSteph!!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.