Title: | Find the Pole of Inaccessibility (Visual Center) of a Polygon |
---|---|
Description: | A wrapper around the C++ library 'polylabel' from 'Mapbox', providing an efficient routine for finding the approximate pole of inaccessibility of a polygon, which usually serves as an excellent candidate for labeling of a polygon. |
Authors: | Johan Larsson [aut, cre] , Kent Johnson [ctb], Mapbox [cph] (polylabel, variant, and geometry libraries) |
Maintainer: | Johan Larsson <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.0 |
Built: | 2024-11-19 18:27:53 UTC |
Source: | https://github.com/jolars/polylabelr |
This function computes and returns the approximate pole of inaccessibility for a polygon using a quadtree-based algorithm developed by the people from Mapbox.
poi(x, y = NULL, precision = 1)
poi(x, y = NULL, precision = 1)
x |
a vector of x coordinates or a matrix or data.frame
of x and y coordinates, a list of components x and y,
a time series (see |
y |
a vector of y coordinates. Only needs to be provided if
|
precision |
the precision to use when computing the center |
If there are any NA
values in the input, they
will be treated as separators for multiple paths (rings) of the
polygon, mimicking the behavior of graphics::polypath()
.
A list with items
x |
x coordinate of the center |
y |
y coordinate of the center |
dist |
distance to the enclosing polygon |
Garcia-Castellanos & Lombardo, 2007. Poles of inaccessibility: A calculation algorithm for the remotest places on earth Scottish Geographical Journal, Volume 123, 3, 227-233. doi:10.1080/14702540801897809
https://github.com/mapbox/polylabel
https://blog.mapbox.com/a-new-algorithm-for-finding-a-visual-center-of-a-polygon-7c77e6492fbc
grDevices::xy.coords()
, graphics::polypath()
plot_path <- function(x, y, ...) { plot.new() plot.window(range(x, na.rm = TRUE), range(y, na.rm = TRUE)) polypath(x, y, ...) } x <- c(5, 10, 10, 5, 5, 6, 6, 7, 7, 6, 8, 8, 9, 9, 8) y <- c(5, 5, 10, 10, 5, 6, 7, 7, 6, 6, 8, 9, 9, 8, 8) plot_path(x, y, col = "grey", border = NA) points(poi(x, y)) ## Not run: # Find visual centers for North Carolina counties library(sf) nc <- st_read(system.file("shape/nc.shp", package = "sf")) locations <- do.call(rbind, poi(nc, precision = 0.01)) plot(st_geometry(nc)) points(locations) ## End(Not run)
plot_path <- function(x, y, ...) { plot.new() plot.window(range(x, na.rm = TRUE), range(y, na.rm = TRUE)) polypath(x, y, ...) } x <- c(5, 10, 10, 5, 5, 6, 6, 7, 7, 6, 8, 8, 9, 9, 8) y <- c(5, 5, 10, 10, 5, 6, 7, 7, 6, 6, 8, 9, 9, 8, 8) plot_path(x, y, col = "grey", border = NA) points(poi(x, y)) ## Not run: # Find visual centers for North Carolina counties library(sf) nc <- st_read(system.file("shape/nc.shp", package = "sf")) locations <- do.call(rbind, poi(nc, precision = 0.01)) plot(st_geometry(nc)) points(locations) ## End(Not run)
This function is a convenience function to be used with
multi-polygons (lists of polygons). The function
simply calls poi()
for each polygon in the list
and returns the point wit the maximum distance to
the boundaries of the enclosing polygon.
poi_multi(polygons, ...)
poi_multi(polygons, ...)
polygons |
a list, each element containing
entries |
... |
Arguments passed on to
|
A list with items
x |
x coordinate of the center |
y |
y coordinate of the center |
dist |
distance to the enclosing polygon |
p1 <- rbind( c(0, 0), c(1, 0), c(3, 2), c(2, 4), c(1, 4), c(0, 0), c(NA, NA), c(1, 1), c(1, 2), c(2, 2), c(1, 1) ) p2 <- rbind( c(3, 0), c(4, 0), c(4, 1), c(3, 1), c(3, 0), c(NA, NA), c(3.3, 0.8), c(3.8, 0.8), c(3.8, 0.3), c(3.3, 0.3), c(3.3, 0.3) ) p3 <- rbind( c(3, 3), c(4, 2), c(4, 3), c(3, 3) ) mpol <- list(p1, p2, p3) plot.new() plot.window(c(0, 4), c(0, 4), asp = 1) for (i in seq_along(mpol)) { polypath(mpol[[i]]) } res <- poi_multi(mpol, precision = 1e-5) points(res, pch = 19, col = "steelblue4") res
p1 <- rbind( c(0, 0), c(1, 0), c(3, 2), c(2, 4), c(1, 4), c(0, 0), c(NA, NA), c(1, 1), c(1, 2), c(2, 2), c(1, 1) ) p2 <- rbind( c(3, 0), c(4, 0), c(4, 1), c(3, 1), c(3, 0), c(NA, NA), c(3.3, 0.8), c(3.8, 0.8), c(3.8, 0.3), c(3.3, 0.3), c(3.3, 0.3) ) p3 <- rbind( c(3, 3), c(4, 2), c(4, 3), c(3, 3) ) mpol <- list(p1, p2, p3) plot.new() plot.window(c(0, 4), c(0, 4), asp = 1) for (i in seq_along(mpol)) { polypath(mpol[[i]]) } res <- poi_multi(mpol, precision = 1e-5) points(res, pch = 19, col = "steelblue4") res