Name

ST_VoronoiPolygons — Returns the cells of the Voronoi diagram of the vertices of a geometry.

Synopsis

geometry ST_VoronoiPolygons( geometry geom , float8 tolerance = 0.0 , geometry extend_to = NULL );

Description

Computes a two-dimensional Voronoi diagram from the vertices of the supplied geometry. The result is a GEOMETRYCOLLECTION of POLYGONs that covers an envelope larger than the extent of the input vertices. Returns null if input geometry is null. Returns an empty geometry collection if the input geometry contains only one vertex. Returns an empty geometry collection if the extend_to envelope has zero area.

Optional parameters:

  • tolerance: The distance within which vertices will be considered equivalent. Robustness of the algorithm can be improved by supplying a nonzero tolerance distance. (default = 0.0)

  • extend_to: If present, the diagram is extended to cover the envelope of the supplied geometry, unless smaller than the default envelope (default = NULL, default envelope is the bounding box of the input expanded by about 50%).

Performed by the GEOS module.

Availability: 2.3.0

Examples

Points overlaid on top of the Voronoi diagram.

Code
SELECT ST_VoronoiPolygons('MULTIPOINT (50 30,60 30,100 100,10 150,110 120)'::geometry
            ) AS geom;
Output
GEOMETRYCOLLECTION(POLYGON((-110 270,100.5 270,59.34782608695652 132.82608695652175,36.81818181818182 92.27272727272727,-110 43.33333333333332,-110 270)),
POLYGON((-110 -90,-110 43.33333333333332,36.81818181818182 92.27272727272727,55 79.28571428571429,55 -90,-110 -90)),
POLYGON((230 -20.71428571428572,55 79.28571428571429,36.81818181818182 92.27272727272727,59.34782608695652 132.82608695652175,230 47.50000000000001,230 -20.71428571428572)), POLYGON((230 -90,55 -90,55 79.28571428571429,230 -20.71428571428572,230 -90)),
POLYGON((230 270,230 47.50000000000001,59.34782608695652 132.82608695652175,100.5 270,230 270)))
Figure
Geometry figure for visual-st-voronoipolygons-01

Voronoi diagram with a tolerance of 30 units.

Code
SELECT ST_VoronoiPolygons('MULTIPOINT (50 30,60 30,100 100,10 150,110 120)'::geometry,
            30) AS geom;
Output
GEOMETRYCOLLECTION(POLYGON((-110 270,135.55555555555557 270,36.81818181818182 92.27272727272727,-110 43.33333333333332,-110 270)),
POLYGON((230 270,230 -45.71428571428574,36.81818181818182 92.27272727272727,135.55555555555557 270,230 270)),
POLYGON((230 -90,-110 -90,-110 43.33333333333332,36.81818181818182 92.27272727272727,230 -45.71428571428574,230 -90)))
Figure
Geometry figure for visual-st-voronoipolygons-02

Build the diagram using all sites that can influence the area of interest. A site outside a tile can still be the nearest site to a location inside it. The extend_to argument does not select sites or clip the diagram to a smaller tile. For a tiled result, compute the diagram from the full input and then clip the cells to the tiles.

Code
WITH inputs(source, sites) AS (
    VALUES ('all sites', 'MULTIPOINT((0 0),(0.6 0),(2 0))'::geometry),
           ('tile only', 'MULTIPOINT((0 0),(0.6 0))'::geometry)
)
SELECT source, ST_X(site.geom) AS nearest_site_x
FROM inputs
CROSS JOIN LATERAL ST_Dump(
    ST_VoronoiPolygons(sites, 0, ST_MakeEnvelope(0, -1, 1.6, 1))
) AS cell
CROSS JOIN LATERAL ST_Dump(sites) AS site
WHERE ST_Covers(cell.geom, ST_Point(1.5, 0))
  AND ST_Covers(cell.geom, site.geom)
ORDER BY source;
Output
  source   | nearest_site_x
-----------+----------------
 all sites |              2
 tile only |            0.6
(2 rows)
Figure
Geometry figure for visual-st-voronoipolygons-03