Commit 3a6bca4c authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

voronoi() function borrowed from package dismo

parent 1290fa47
......@@ -19,6 +19,7 @@ Encoding: UTF-8
LazyData: true
Imports:
classInt,
deldir,
geojsonio,
geojsonlint,
igraph,
......
......@@ -18,6 +18,7 @@ export(risk_plot)
export(risk_unit)
export(rmext)
export(wlc)
import(deldir)
import(maptools)
import(methods)
import(raster)
......@@ -32,3 +33,5 @@ importFrom(igraph,edge_attr_names)
importFrom(igraph,gorder)
importFrom(igraph,graph_from_data_frame)
importFrom(igraph,is.weighted)
importFrom(raster,intersect)
importFrom(rgeos,gUnaryUnion)
......@@ -19,8 +19,13 @@
#' raster::plot(distance_map(cmr$water_bodies, boundaries = cmr$cmr_admin3))
distance_map <- function(x, boundaries, res = resolution(boundaries, min_ncells = 100)) {
ext_grid <- raster::raster(raster::extent(boundaries), resolution = res)
msk <- raster::rasterize(boundaries, ext_grid, field = 1, background = NA, fun = "mean")
if (inherits(boundaries, "Spatial")) {
ext_grid <- raster::raster(raster::extent(boundaries), resolution = res)
msk <- raster::rasterize(boundaries, ext_grid, field = 1, background = NA, fun = "mean")
} else if (inherits(boundaries, "Raster")) {
msk <- boundaries
} else stop(paste(substitute(boundaries), "must be either a SpatialPolygon* or a Raster* object."))
ans <- distance_to_vector(x, msk)
return(ans)
}
......
#' Voronoi polygons
#'
#' Compute a vornoi tesselation of a map for a set of points.
#'
#'
#' @param x SpatialPoints* or two-column matrix or data.frame with x and y coordinates.
#' @param ext Extent object.
#' @param eps Numerical tolerance used in triangulation. See \code{\link[deldir]{deldir}}.
#'
#' @author Borrowed from function \code{\link[dismo]{voronoi}} from
#' package \code{dismo} by R. Hijmans et al., which ultimately
#' uses the \code{\link[deldir]{deldir}} function by Rolf Turner.
#' @return SpatialPolygonsDataFrame.
#' @import deldir
#' @importFrom raster intersect
#' @importFrom rgeos gUnaryUnion
#'
#' @examples
voronoi <- function(x, ext, eps = 1e-09) {
if (!requireNamespace("deldir")) {
stop("you need to first install the deldir libary")
}
dat <- NULL
sp <- FALSE
if (inherits(x, "Spatial")) {
if (.hasSlot(x, "data")) {
dat <- slot(x, "data")
}
prj <- proj4string(x)
sp <- TRUE
xy <- coordinates(x)
dups <- duplicated(xy)
if (any(dups)) {
xy <- xy[!dups, , drop = FALSE]
dat <- dat[!dups, , drop = FALSE]
}
}
else {
xy <- stats::na.omit(x[, 1:2])
xy <- unique(xy)
}
ext <- as.vector(ext)
z <- deldir::deldir(xy[, 1], xy[, 2], rw = ext, eps = eps,
suppressMsge = TRUE)
index <- z$ind.orig
w <- deldir::tile.list(z)
polys <- vector(mode = "list", length = length(w))
for (i in seq(along = polys)) {
pc <- cbind(w[[i]]$x, w[[i]]$y)
pc <- rbind(pc, pc[1, ])
polys[[i]] <- Polygons(list(Polygon(pc)), as.character(index[i]))
}
if (sp) {
polys <- SpatialPolygons(polys, proj4string = CRS(prj))
}
else {
polys <- SpatialPolygons(polys)
}
if (is.null(dat)) {
dat <- data.frame(id = index)
}
else {
dat <- dat[index, , drop = FALSE]
}
rownames(dat) <- row.names(polys)
polys <- SpatialPolygonsDataFrame(polys, data = dat)
return(polys)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/voronoi.R
\name{voronoi}
\alias{voronoi}
\title{Voronoi polygons}
\usage{
voronoi(x, ext, eps = 1e-09)
}
\arguments{
\item{x}{SpatialPoints* or two-column matrix or data.frame with x and y coordinates.}
\item{ext}{Extent object.}
\item{eps}{Numerical tolerance used in triangulation. See \code{\link[deldir]{deldir}}.}
}
\value{
SpatialPolygonsDataFrame.
}
\description{
Compute a vornoi tesselation of a map for a set of points.
}
\author{
Borrowed from function \code{\link[dismo]{voronoi}} from
package \code{dismo} by R. Hijmans et al., which ultimately
uses the \code{\link[deldir]{deldir}} function by Rolf Turner.
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment