Skip to content

Instantly share code, notes, and snippets.

@jlisic
Created January 16, 2018 18:23
Show Gist options
  • Save jlisic/18c5671106da2076d8d2a261a2148c3d to your computer and use it in GitHub Desktop.
Save jlisic/18c5671106da2076d8d2a261a2148c3d to your computer and use it in GitHub Desktop.
Create a tessellation from a subset of sampled data.
# create a tessalation
sampleVoronoi <- function(x, sampled,rw=NULL) {
require(raster)
vor <- tile.list( deldir(x[sampled,1], x[sampled,2],rw=rw) )
vorPoly <- lapply( 1:length(vor) ,
function(i) {
tmp <- cbind( vor[[i]]$x, vor[[i]]$y )
tmp <- rbind( tmp, tmp[1,])
colnames(tmp) <- c('x','y')
return( Polygons(list(Polygon(tmp)), ID=i) )
}
)
vorDat <- x[sampled,]
vorDat <- as.data.frame(vorDat)
rownames(vorDat) <- sapply(slot(SpatialPolygons(vorPoly), 'polygons'), slot,'ID')
return( SpatialPolygonsDataFrame(SpatialPolygons(vorPoly),data=vorDat) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment