PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ST_ClipByBox2d()

Datum ST_ClipByBox2d ( PG_FUNCTION_ARGS  )

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

1473 {
1474  static const uint32_t geom_idx = 0;
1475  static const uint32_t box2d_idx = 1;
1477  LWGEOM *lwgeom1, *lwresult ;
1478  GBOX bbox1 = {0};
1479  GBOX *bbox2;
1480  uint8_t type;
1481  int32_t srid;
1482  lwflags_t flags;
1483 
1484  if (!gserialized_datum_get_internals_p(PG_GETARG_DATUM(geom_idx), &bbox1, &flags, &type, &srid))
1485  {
1486  /* empty clips to empty, no matter rect */
1487  PG_RETURN_DATUM(PG_GETARG_DATUM(geom_idx));
1488  }
1489 
1490  /* WARNING: this is really a BOX2DF, use only xmin and ymin fields */
1491  bbox2 = (GBOX *)PG_GETARG_POINTER(box2d_idx);
1492  bbox2->flags = 0;
1493 
1494  /* If bbox1 is strictly contained by bbox2, return input geometry */
1495  if (bbox2->xmin < bbox1.xmin && bbox2->xmax > bbox1.xmax &&
1496  bbox2->ymin < bbox1.ymin && bbox2->ymax > bbox1.ymax)
1497  {
1498  PG_RETURN_DATUM(PG_GETARG_DATUM(geom_idx));
1499  }
1500 
1501  /* If bbox1 outside of bbox2, return empty */
1502  if (!gbox_overlaps_2d(&bbox1, bbox2))
1503  {
1504  /* Get type and srid from datum */
1505  lwresult = lwgeom_construct_empty(type, srid, 0, 0);
1506  result = geometry_serialize(lwresult) ;
1507  lwgeom_free(lwresult) ;
1508  PG_RETURN_POINTER(result);
1509  }
1510 
1511  lwgeom1 = lwgeom_from_gserialized(PG_GETARG_GSERIALIZED_P(geom_idx));
1512  lwresult = lwgeom_clip_by_rect(lwgeom1, bbox2->xmin, bbox2->ymin,
1513  bbox2->xmax, bbox2->ymax);
1514 
1515  lwgeom_free(lwgeom1);
1516 
1517  if (!lwresult)
1518  PG_RETURN_NULL();
1519 
1520  result = geometry_serialize(lwresult) ;
1521  PG_RETURN_POINTER(result);
1522 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
int gbox_overlaps_2d(const GBOX *g1, const GBOX *g2)
Return LW_TRUE if the GBOX overlaps on the 2d plane, LW_FALSE otherwise.
Definition: gbox.c:323
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_datum_get_internals_p(Datum gsdatum, GBOX *gbox, lwflags_t *flags, uint8_t *type, int32_t *srid)
Peak into a GSERIALIZED datum to find its bounding box and some other metadata.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
uint16_t lwflags_t
Definition: liblwgeom.h:314
LWGEOM * lwgeom_clip_by_rect(const LWGEOM *geom1, double x0, double y0, double x1, double y1)
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwgeom.c:2128
type
Definition: ovdump.py:42
double ymax
Definition: liblwgeom.h:372
double xmax
Definition: liblwgeom.h:370
double ymin
Definition: liblwgeom.h:371
double xmin
Definition: liblwgeom.h:369
lwflags_t flags
Definition: liblwgeom.h:368

References GBOX::flags, gbox_overlaps_2d(), gserialized_datum_get_internals_p(), lwgeom_clip_by_rect(), lwgeom_construct_empty(), lwgeom_free(), lwgeom_from_gserialized(), result, ovdump::type, GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Here is the call graph for this function: