PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ ST_MaximumInscribedCircle()

Datum ST_MaximumInscribedCircle ( PG_FUNCTION_ARGS  )

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

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

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: