PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ lwgeom_voronoi_diagram()

LWGEOM* lwgeom_voronoi_diagram ( const LWGEOM g,
const GBOX env,
double  tolerance,
int  output_edges 
)

Take vertices of a geometry and build the Voronoi diagram.

Parameters
gthe input geometry
envan optional envelope for clipping the results
tolerancean optional snapping tolerance for improved robustness
output_edgesif non-zero the result will be a MULTILINESTRING, otherwise it'll be a COLLECTION of polygons.

Requires GEOS-3.5.0 or higher

Definition at line 1843 of file liblwgeom/lwgeom_geos.c.

References COLLECTIONTYPE, GBOX2GEOS(), GEOS2LWGEOM(), LW_FALSE, lwcollection_as_lwgeom(), lwcollection_construct_empty(), lwerror(), lwgeom_count_vertices(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwgeom_get_geos_coordseq_2d(), lwgeom_get_srid(), lwgeom_set_srid(), and lwnotice().

Referenced by lwgeom_delaunay_triangulation(), ST_Voronoi(), test_lwgeom_voronoi_diagram(), and test_lwgeom_voronoi_diagram_custom_envelope().

1844 {
1845  uint32_t num_points = lwgeom_count_vertices(g);
1846  LWGEOM* lwgeom_result;
1847  char is_3d = LW_FALSE;
1848  int srid = lwgeom_get_srid(g);
1849  GEOSCoordSequence* coords;
1850  GEOSGeometry* geos_geom;
1851  GEOSGeometry* geos_env = NULL;
1852  GEOSGeometry* geos_result;
1853 
1854  if (num_points < 2)
1855  {
1857  return lwcollection_as_lwgeom(empty);
1858  }
1859 
1860  initGEOS(lwnotice, lwgeom_geos_error);
1861 
1862  /* Instead of using the standard LWGEOM2GEOS transformer, we read the vertices of the LWGEOM directly and put
1863  * them into a single GEOS CoordinateSeq that can be used to define a LineString. This allows us to process
1864  * geometry types that may not be supported by GEOS, and reduces the memory requirements in cases of many
1865  * geometries with few points (such as LWMPOINT).*/
1866  coords = lwgeom_get_geos_coordseq_2d(g, num_points);
1867  if (!coords) return NULL;
1868 
1869  geos_geom = GEOSGeom_createLineString(coords);
1870  if (!geos_geom)
1871  {
1872  GEOSCoordSeq_destroy(coords);
1873  return NULL;
1874  }
1875 
1876  if (env) geos_env = GBOX2GEOS(env);
1877 
1878  geos_result = GEOSVoronoiDiagram(geos_geom, geos_env, tolerance, output_edges);
1879 
1880  GEOSGeom_destroy(geos_geom);
1881  if (env) GEOSGeom_destroy(geos_env);
1882 
1883  if (!geos_result)
1884  {
1885  lwerror("GEOSVoronoiDiagram: %s", lwgeom_geos_errmsg);
1886  return NULL;
1887  }
1888 
1889  lwgeom_result = GEOS2LWGEOM(geos_result, is_3d);
1890  GEOSGeom_destroy(geos_result);
1891 
1892  lwgeom_set_srid(lwgeom_result, srid);
1893 
1894  return lwgeom_result;
1895 }
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, uint8_t want3d)
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
GEOSGeometry * GBOX2GEOS(const GBOX *box)
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:916
static GEOSCoordSequence * lwgeom_get_geos_coordseq_2d(const LWGEOM *g, uint32_t num_points)
unsigned int uint32_t
Definition: uthash.h:78
void lwgeom_geos_error(const char *fmt,...)
#define LW_FALSE
Definition: liblwgeom.h:76
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1235
void lwgeom_set_srid(LWGEOM *geom, int srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:300
Here is the call graph for this function:
Here is the caller graph for this function: