PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ ST_ClipByBox2d()

Datum ST_ClipByBox2d ( PG_FUNCTION_ARGS  )

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

1399 {
1400  GSERIALIZED *geom1;
1401  GSERIALIZED *result;
1402  LWGEOM *lwgeom1, *lwresult ;
1403  const GBOX *bbox1;
1404  GBOX *bbox2;
1405 
1406  geom1 = PG_GETARG_GSERIALIZED_P(0);
1407  lwgeom1 = lwgeom_from_gserialized(geom1) ;
1408 
1409  bbox1 = lwgeom_get_bbox(lwgeom1);
1410  if ( ! bbox1 )
1411  {
1412  /* empty clips to empty, no matter rect */
1413  lwgeom_free(lwgeom1);
1414  PG_RETURN_POINTER(geom1);
1415  }
1416 
1417  /* WARNING: this is really a BOX2DF, use only xmin and ymin fields */
1418  bbox2 = (GBOX *)PG_GETARG_POINTER(1);
1419  bbox2->flags = 0;
1420 
1421  /* If bbox1 outside of bbox2, return empty */
1422  if ( ! gbox_overlaps_2d(bbox1, bbox2) )
1423  {
1424  lwresult = lwgeom_construct_empty(lwgeom1->type, lwgeom1->srid, 0, 0);
1425  lwgeom_free(lwgeom1);
1426  PG_FREE_IF_COPY(geom1, 0);
1427  result = geometry_serialize(lwresult) ;
1428  lwgeom_free(lwresult) ;
1429  PG_RETURN_POINTER(result);
1430  }
1431 
1432  /* if bbox1 is covered by bbox2, return lwgeom1 */
1433  if ( gbox_contains_2d(bbox2, bbox1) )
1434  {
1435  lwgeom_free(lwgeom1);
1436  PG_RETURN_POINTER(geom1);
1437  }
1438 
1439  lwresult = lwgeom_clip_by_rect(lwgeom1, bbox2->xmin, bbox2->ymin,
1440  bbox2->xmax, bbox2->ymax);
1441 
1442  lwgeom_free(lwgeom1);
1443  PG_FREE_IF_COPY(geom1, 0);
1444 
1445  if (!lwresult) PG_RETURN_NULL();
1446 
1447  result = geometry_serialize(lwresult) ;
1448  lwgeom_free(lwresult) ;
1449  PG_RETURN_POINTER(result);
1450 }
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
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
const GBOX * lwgeom_get_bbox(const LWGEOM *lwgeom)
Get a non-empty geometry bounding box, computing and caching it if not already there.
Definition: lwgeom.c:725
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:2083
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
double ymax
Definition: liblwgeom.h:343
double xmax
Definition: liblwgeom.h:341
double ymin
Definition: liblwgeom.h:342
double xmin
Definition: liblwgeom.h:340
lwflags_t flags
Definition: liblwgeom.h:339
uint8_t type
Definition: liblwgeom.h:448
int32_t srid
Definition: liblwgeom.h:446

References GBOX::flags, gbox_contains_2d(), gbox_overlaps_2d(), geometry_serialize(), lwgeom_clip_by_rect(), lwgeom_construct_empty(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_get_bbox(), LWGEOM::srid, LWGEOM::type, GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

Here is the call graph for this function: