PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ST_ClipByBox2d()

Datum ST_ClipByBox2d ( PG_FUNCTION_ARGS  )

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

1590 {
1591  static const uint32_t geom_idx = 0;
1592  static const uint32_t box2d_idx = 1;
1594  LWGEOM *lwgeom1, *lwresult ;
1595  GBOX bbox1 = {0};
1596  GBOX *bbox2;
1597  uint8_t type;
1598  int32_t srid;
1599  lwflags_t flags;
1600 
1601  if (!gserialized_datum_get_internals_p(PG_GETARG_DATUM(geom_idx), &bbox1, &flags, &type, &srid))
1602  {
1603  /* empty clips to empty, no matter rect */
1604  PG_RETURN_DATUM(PG_GETARG_DATUM(geom_idx));
1605  }
1606 
1607  /* WARNING: this is really a BOX2DF, use only xmin and ymin fields */
1608  bbox2 = (GBOX *)PG_GETARG_POINTER(box2d_idx);
1609  bbox2->flags = 0;
1610 
1611  /* if bbox1 is covered by bbox2, return lwgeom1 */
1612  if (gbox_contains_2d(bbox2, &bbox1))
1613  {
1614  PG_RETURN_DATUM(PG_GETARG_DATUM(geom_idx));
1615  }
1616 
1617  /* If bbox1 outside of bbox2, return empty */
1618  if (!gbox_overlaps_2d(&bbox1, bbox2))
1619  {
1620  /* Get type and srid from datum */
1621  lwresult = lwgeom_construct_empty(type, srid, 0, 0);
1622  result = geometry_serialize(lwresult) ;
1623  lwgeom_free(lwresult) ;
1624  PG_RETURN_POINTER(result);
1625  }
1626 
1627  lwgeom1 = lwgeom_from_gserialized(PG_GETARG_GSERIALIZED_P(geom_idx));
1628  lwresult = lwgeom_clip_by_rect(lwgeom1, bbox2->xmin, bbox2->ymin,
1629  bbox2->xmax, bbox2->ymax);
1630 
1631  lwgeom_free(lwgeom1);
1632 
1633  if (!lwresult)
1634  PG_RETURN_NULL();
1635 
1636  result = geometry_serialize(lwresult) ;
1637  PG_RETURN_POINTER(result);
1638 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
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
int gbox_contains_2d(const GBOX *g1, const GBOX *g2)
Return LW_TRUE if the first GBOX contains the second on the 2d plane, LW_FALSE otherwise.
Definition: gbox.c:339
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:299
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:2105
type
Definition: ovdump.py:42
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
lwflags_t flags
Definition: liblwgeom.h:353

References GBOX::flags, gbox_contains_2d(), 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: