PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ST_MaximumInscribedCircle()

Datum ST_MaximumInscribedCircle ( PG_FUNCTION_ARGS  )

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

267 {
268 #if POSTGIS_GEOS_VERSION < 30900
269 
270  lwpgerror("The GEOS version this PostGIS binary "
271  "was compiled against (%d) doesn't support "
272  "'GEOSMaximumInscribedCircle' function (3.9.0+ required)",
274  PG_RETURN_NULL();
275 
276 #else /* POSTGIS_GEOS_VERSION >= 30900 */
277  GSERIALIZED* geom;
278  GSERIALIZED* center;
279  GSERIALIZED* nearest;
280  TupleDesc resultTupleDesc;
281  HeapTuple resultTuple;
282  Datum result;
283  Datum result_values[3];
284  bool result_is_null[3];
285  double radius = 0.0;
286  int32 srid = SRID_UNKNOWN;
287  bool is3d;
288 
289  if (PG_ARGISNULL(0))
290  PG_RETURN_NULL();
291 
292  geom = PG_GETARG_GSERIALIZED_P(0);
293  srid = gserialized_get_srid(geom);
294  is3d = gserialized_has_z(geom);
295 
296  /* Empty geometry? Return POINT EMPTY with zero radius */
297  if (gserialized_is_empty(geom))
298  {
301  center = geometry_serialize(lwcenter);
302  nearest = geometry_serialize(lwnearest);
303  radius = 0.0;
304  }
305  else
306  {
307  GEOSGeometry *ginput, *gcircle, *gcenter, *gnearest;
308  double width, height, size, tolerance;
309  GBOX gbox;
310  int gtype;
311  LWGEOM *lwg;
312  lwg = lwgeom_from_gserialized(geom);
313  if (!lwgeom_isfinite(lwg))
314  {
315  lwpgerror("Geometry contains invalid coordinates");
316  PG_RETURN_NULL();
317  }
318  lwgeom_free(lwg);
319 
320  if (!gserialized_get_gbox_p(geom, &gbox))
321  PG_RETURN_NULL();
322 
323  width = gbox.xmax - gbox.xmin;
324  height = gbox.ymax - gbox.ymin;
325  size = width > height ? width : height;
326  tolerance = size / 1000.0;
327 
328  initGEOS(lwpgnotice, lwgeom_geos_error);
329 
330  ginput = POSTGIS2GEOS(geom);
331  if (!ginput)
332  HANDLE_GEOS_ERROR("Geometry could not be converted to GEOS");
333 
334  gtype = gserialized_get_type(geom);
335  if (gtype == POLYGONTYPE || gtype == MULTIPOLYGONTYPE)
336  {
337  gcircle = GEOSMaximumInscribedCircle(ginput, tolerance);
338  if (!gcircle)
339  {
340  lwpgerror("Error calculating GEOSMaximumInscribedCircle.");
341  GEOSGeom_destroy(ginput);
342  PG_RETURN_NULL();
343  }
344  }
345  else
346  {
347  gcircle = GEOSLargestEmptyCircle(ginput, NULL, tolerance);
348  if (!gcircle)
349  {
350  lwpgerror("Error calculating GEOSLargestEmptyCircle.");
351  GEOSGeom_destroy(ginput);
352  PG_RETURN_NULL();
353  }
354  }
355 
356  gcenter = GEOSGeomGetStartPoint(gcircle);
357  gnearest = GEOSGeomGetEndPoint(gcircle);
358  GEOSDistance(gcenter, gnearest, &radius);
359  GEOSSetSRID(gcenter, srid);
360  GEOSSetSRID(gnearest, srid);
361 
362  center = GEOS2POSTGIS(gcenter, is3d);
363  nearest = GEOS2POSTGIS(gnearest, is3d);
364  GEOSGeom_destroy(gcenter);
365  GEOSGeom_destroy(gnearest);
366  GEOSGeom_destroy(gcircle);
367  GEOSGeom_destroy(ginput);
368  }
369 
370  get_call_result_type(fcinfo, NULL, &resultTupleDesc);
371  BlessTupleDesc(resultTupleDesc);
372 
373  result_values[0] = PointerGetDatum(center);
374  result_is_null[0] = false;
375  result_values[1] = PointerGetDatum(nearest);
376  result_is_null[1] = false;
377  result_values[2] = Float8GetDatum(radius);
378  result_is_null[2] = false;
379  resultTuple = heap_form_tuple(resultTupleDesc, result_values, result_is_null);
380 
381  result = HeapTupleGetDatum(resultTuple);
382 
383  PG_RETURN_DATUM(result);
384 
385 #endif /* POSTGIS_GEOS_VERSION >= 30900 */
386 }
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:155
int gserialized_get_gbox_p(const GSERIALIZED *g, GBOX *gbox)
Read the box from the GSERIALIZED or calculate it if necessary.
Definition: gserialized.c:94
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:268
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:181
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:203
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:118
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:1218
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
Definition: lwgeom.c:2807
#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: