PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ST_MaximumInscribedCircle()

Datum ST_MaximumInscribedCircle ( PG_FUNCTION_ARGS  )

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

337 {
338 #if POSTGIS_GEOS_VERSION < 30900
339 
340  lwpgerror("The GEOS version this PostGIS binary "
341  "was compiled against (%d) doesn't support "
342  "'GEOSMaximumInscribedCircle' function (3.9.0+ required)",
344  PG_RETURN_NULL();
345 
346 #else /* POSTGIS_GEOS_VERSION >= 30900 */
347  GSERIALIZED* geom;
348  GSERIALIZED* center;
349  GSERIALIZED* nearest;
350  TupleDesc resultTupleDesc;
351  HeapTuple resultTuple;
352  Datum result;
353  Datum result_values[3];
354  bool result_is_null[3];
355  double radius = 0.0;
356  int32 srid = SRID_UNKNOWN;
357  bool is3d;
358 
359  if (PG_ARGISNULL(0))
360  PG_RETURN_NULL();
361 
362  geom = PG_GETARG_GSERIALIZED_P(0);
363  srid = gserialized_get_srid(geom);
364  is3d = gserialized_has_z(geom);
365 
366  /* Empty geometry? Return POINT EMPTY with zero radius */
367  if (gserialized_is_empty(geom))
368  {
371  center = geometry_serialize(lwcenter);
372  nearest = geometry_serialize(lwnearest);
373  radius = 0.0;
374  }
375  else
376  {
377  GEOSGeometry *ginput, *gcircle, *gcenter, *gnearest;
378  double width, height, size, tolerance;
379  GBOX gbox;
380  int gtype;
381  LWGEOM *lwg;
382  lwg = lwgeom_from_gserialized(geom);
383  if (!lwgeom_isfinite(lwg))
384  {
385  lwpgerror("Geometry contains invalid coordinates");
386  PG_RETURN_NULL();
387  }
388  lwgeom_free(lwg);
389 
390  if (!gserialized_get_gbox_p(geom, &gbox))
391  PG_RETURN_NULL();
392 
393  width = gbox.xmax - gbox.xmin;
394  height = gbox.ymax - gbox.ymin;
395  size = width > height ? width : height;
396  tolerance = size / 1000.0;
397 
398  initGEOS(lwpgnotice, lwgeom_geos_error);
399 
400  ginput = POSTGIS2GEOS(geom);
401  if (!ginput)
402  HANDLE_GEOS_ERROR("Geometry could not be converted to GEOS");
403 
404  gtype = gserialized_get_type(geom);
405  if (gtype == POLYGONTYPE || gtype == MULTIPOLYGONTYPE)
406  {
407  gcircle = GEOSMaximumInscribedCircle(ginput, tolerance);
408  if (!gcircle)
409  {
410  lwpgerror("Error calculating GEOSMaximumInscribedCircle.");
411  GEOSGeom_destroy(ginput);
412  PG_RETURN_NULL();
413  }
414  }
415  else
416  {
417  gcircle = GEOSLargestEmptyCircle(ginput, NULL, tolerance);
418  if (!gcircle)
419  {
420  lwpgerror("Error calculating GEOSLargestEmptyCircle.");
421  GEOSGeom_destroy(ginput);
422  PG_RETURN_NULL();
423  }
424  }
425 
426  gcenter = GEOSGeomGetStartPoint(gcircle);
427  gnearest = GEOSGeomGetEndPoint(gcircle);
428  GEOSDistance(gcenter, gnearest, &radius);
429  GEOSSetSRID(gcenter, srid);
430  GEOSSetSRID(gnearest, srid);
431 
432  center = GEOS2POSTGIS(gcenter, is3d);
433  nearest = GEOS2POSTGIS(gnearest, is3d);
434  GEOSGeom_destroy(gcenter);
435  GEOSGeom_destroy(gnearest);
436  GEOSGeom_destroy(gcircle);
437  GEOSGeom_destroy(ginput);
438  }
439 
440  get_call_result_type(fcinfo, NULL, &resultTupleDesc);
441  BlessTupleDesc(resultTupleDesc);
442 
443  result_values[0] = PointerGetDatum(center);
444  result_is_null[0] = false;
445  result_values[1] = PointerGetDatum(nearest);
446  result_is_null[1] = false;
447  result_values[2] = Float8GetDatum(radius);
448  result_is_null[2] = false;
449  resultTuple = heap_form_tuple(resultTupleDesc, result_values, result_is_null);
450 
451  result = HeapTupleGetDatum(resultTuple);
452 
453  PG_RETURN_DATUM(result);
454 
455 #endif /* POSTGIS_GEOS_VERSION >= 30900 */
456 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
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:94
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:2681
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:107
#define POLYGONTYPE
Definition: liblwgeom.h:104
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
GSERIALIZED * GEOS2POSTGIS(GEOSGeom geom, char want3d)
GEOSGeometry * POSTGIS2GEOS(const GSERIALIZED *pglwgeom)
#define HANDLE_GEOS_ERROR(label)
unsigned int int32
Definition: shpopen.c:54
#define POSTGIS_GEOS_VERSION
Definition: sqldefines.h:11
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354

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: