PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ST_MaximumInscribedCircle()

Datum ST_MaximumInscribedCircle ( PG_FUNCTION_ARGS  )

Definition at line 341 of file postgis/lwgeom_geos.c.

342 {
343 #if POSTGIS_GEOS_VERSION < 30900
344 
345  lwpgerror("The GEOS version this PostGIS binary "
346  "was compiled against (%d) doesn't support "
347  "'GEOSMaximumInscribedCircle' function (3.9.0+ required)",
349  PG_RETURN_NULL();
350 
351 #else /* POSTGIS_GEOS_VERSION >= 30900 */
352  GSERIALIZED* geom;
353  GSERIALIZED* center;
354  GSERIALIZED* nearest;
355  TupleDesc resultTupleDesc;
356  HeapTuple resultTuple;
357  Datum result;
358  Datum result_values[3];
359  bool result_is_null[3];
360  double radius = 0.0;
361  int32 srid = SRID_UNKNOWN;
362  bool is3d;
363 
364  if (PG_ARGISNULL(0))
365  PG_RETURN_NULL();
366 
367  geom = PG_GETARG_GSERIALIZED_P(0);
368  srid = gserialized_get_srid(geom);
369  is3d = gserialized_has_z(geom);
370 
371  /* Empty geometry? Return POINT EMPTY with zero radius */
372  if (gserialized_is_empty(geom))
373  {
376  center = geometry_serialize(lwcenter);
377  nearest = geometry_serialize(lwnearest);
378  radius = 0.0;
379  }
380  else
381  {
382  GEOSGeometry *ginput, *gcircle, *gcenter, *gnearest;
383  double width, height, size, tolerance;
384  GBOX gbox;
385  int gtype;
386  LWGEOM *lwg;
387  lwg = lwgeom_from_gserialized(geom);
388  if (!lwgeom_isfinite(lwg))
389  {
390  lwpgerror("Geometry contains invalid coordinates");
391  PG_RETURN_NULL();
392  }
393  lwgeom_free(lwg);
394 
395  if (!gserialized_get_gbox_p(geom, &gbox))
396  PG_RETURN_NULL();
397 
398  width = gbox.xmax - gbox.xmin;
399  height = gbox.ymax - gbox.ymin;
400  size = width > height ? width : height;
401  tolerance = size / 1000.0;
402 
403  initGEOS(lwpgnotice, lwgeom_geos_error);
404 
405  ginput = POSTGIS2GEOS(geom);
406  if (!ginput)
407  HANDLE_GEOS_ERROR("Geometry could not be converted to GEOS");
408 
409  gtype = gserialized_get_type(geom);
410  if (gtype == POLYGONTYPE || gtype == MULTIPOLYGONTYPE)
411  {
412  gcircle = GEOSMaximumInscribedCircle(ginput, tolerance);
413  if (!gcircle)
414  {
415  lwpgerror("Error calculating GEOSMaximumInscribedCircle.");
416  GEOSGeom_destroy(ginput);
417  PG_RETURN_NULL();
418  }
419  }
420  else
421  {
422  gcircle = GEOSLargestEmptyCircle(ginput, NULL, tolerance);
423  if (!gcircle)
424  {
425  lwpgerror("Error calculating GEOSLargestEmptyCircle.");
426  GEOSGeom_destroy(ginput);
427  PG_RETURN_NULL();
428  }
429  }
430 
431  gcenter = GEOSGeomGetStartPoint(gcircle);
432  gnearest = GEOSGeomGetEndPoint(gcircle);
433  GEOSDistance(gcenter, gnearest, &radius);
434  GEOSSetSRID(gcenter, srid);
435  GEOSSetSRID(gnearest, srid);
436 
437  center = GEOS2POSTGIS(gcenter, is3d);
438  nearest = GEOS2POSTGIS(gnearest, is3d);
439  GEOSGeom_destroy(gcenter);
440  GEOSGeom_destroy(gnearest);
441  GEOSGeom_destroy(gcircle);
442  GEOSGeom_destroy(ginput);
443  }
444 
445  get_call_result_type(fcinfo, NULL, &resultTupleDesc);
446  BlessTupleDesc(resultTupleDesc);
447 
448  result_values[0] = PointerGetDatum(center);
449  result_is_null[0] = false;
450  result_values[1] = PointerGetDatum(nearest);
451  result_is_null[1] = false;
452  result_values[2] = Float8GetDatum(radius);
453  result_is_null[2] = false;
454  resultTuple = heap_form_tuple(resultTupleDesc, result_values, result_is_null);
455 
456  result = HeapTupleGetDatum(resultTuple);
457 
458  PG_RETURN_DATUM(result);
459 
460 #endif /* POSTGIS_GEOS_VERSION >= 30900 */
461 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
Read the box from the GSERIALIZED or calculate it if necessary.
Definition: gserialized.c:65
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:174
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
void lwgeom_geos_error(const char *fmt,...)
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoint.c:151
#define LW_FALSE
Definition: liblwgeom.h:109
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
Definition: lwgeom.c:2707
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:122
#define POLYGONTYPE
Definition: liblwgeom.h:119
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:230
#define HANDLE_GEOS_ERROR(label)
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
GEOSGeometry * POSTGIS2GEOS(const GSERIALIZED *pglwgeom)
unsigned int int32
Definition: shpopen.c:54
#define POSTGIS_GEOS_VERSION
Definition: sqldefines.h:11
double ymax
Definition: liblwgeom.h:372
double xmax
Definition: liblwgeom.h:370
double ymin
Definition: liblwgeom.h:371
double xmin
Definition: liblwgeom.h:369

References GEOS2POSTGIS(), gserialized_get_gbox_p(), gserialized_get_srid(), gserialized_get_type(), gserialized_has_z(), gserialized_is_empty(), HANDLE_GEOS_ERROR, LW_FALSE, lwgeom_free(), lwgeom_from_gserialized(), lwgeom_geos_error(), lwgeom_isfinite(), lwpoint_construct_empty(), MULTIPOLYGONTYPE, POLYGONTYPE, POSTGIS2GEOS(), POSTGIS_GEOS_VERSION, result, SRID_UNKNOWN, GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Here is the call graph for this function: