Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created January 25, 2016 15:01
Show Gist options
  • Save mdsumner/6608756280d419aeebe3 to your computer and use it in GitHub Desktop.
Save mdsumner/6608756280d419aeebe3 to your computer and use it in GitHub Desktop.
## My answer to https://stat.ethz.ch/pipermail/r-sig-geo/2016-January/023953.html
library(raster)
## for example
#r <- getData('SRTM', lon=147, lat=-42)
# or
r <- raster("C:/temp/srtm_66_21.tif")
## make a line
lin <- structure(c(147.278215113875, 147.22345324458, 147.08654857134,
146.9496438981, 146.840120159509, 146.689525018945, 146.580001280354,
146.56631081303, 146.525239411058, 146.49785847641, -43.0914896421773,
-43.0006466005827, -42.869428873835, -42.7886795035286, -42.6776491193575,
-42.5464313926097, -42.3849326519971, -42.1628718836547, -42.0013731430421,
-41.8499680737177), .Dim = c(10L, 2L), .Dimnames = list(NULL,
c("coords.x1", "coords.x2")))
##
sp <- spLines(lin, crs = projection(r))
## buffer the line
library(rgeos)
lbuf <- gBuffer(sp, width = 0.05)
lbuf <- SpatialPolygonsDataFrame(lbuf, data.frame(name = "buffer"), match.ID = FALSE)
## triangulate the buffer and add a vertex to record distance from the start
library(gris)
gobj <- gris(lbuf) ## relational table version of polygonal topology
tri <- gris::triangulate(gobj, a = 0.002)
tri$v$l <- spDistsN1(as.matrix(tri$v[,c("x", "y")]), lin[1,,drop = FALSE])
tri$tXv$rasterval <- 0
for (i in seq(nrow(tri$oXt))) {
tri$tXv$rasterval[i] <- extract(r, spPolygons(tri$v[as.matrix(tri$tXv[i, c(".vx1", ".vx2", ".vx3")]), ] %>% select(x, y) %>% as.matrix), fun = mean)
}
vv <- apply(matrix(tri$v$l[t(as.matrix(tri$tXv[, c(".vx1", ".vx2", ".vx3")]))], nrow = 3), 2, mean)
plot(cbind(vv, tri$tXv$rasterval)[order(vv), ], type = "l")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment