Skip to content

Instantly share code, notes, and snippets.

@geotheory
Last active February 23, 2016 14:14
Show Gist options
  • Save geotheory/a4993be862046bcb612a to your computer and use it in GitHub Desktop.
Save geotheory/a4993be862046bcb612a to your computer and use it in GitHub Desktop.
Creates a spatialLines object for a grid of given x/y values
require(rgeos)
require(rgdal)
grid_lines = function(x, y, crs='+proj=longlat +datum=WGS84'){
x1 = round(seq(min(x),max(x),length.out=50),3)
y1 = round(seq(min(y),max(y),length.out=50),3)
str = character(0)
for(i in x) for(j in 2:length(y1)) str = c(str, paste(i, y1[j], ',', i, y1[j-1]))
for(i in y) for(j in 2:length(x1)) str = c(str, paste(x1[j], i, ',', x1[j-1], i))
grid = readWKT(paste('MULTILINESTRING((', paste(str, collapse='),('), '))'))
grid@proj4string = CRS(crs)
grid
}
## Example (whole world)
# g = grid_lines(10*(-18:18), 10*(-9:9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment