PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum ST_ClipByBox2d ( PG_FUNCTION_ARGS  )

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

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(), POSTGIS_GEOS_VERSION, LWGEOM::srid, LWGEOM::type, GBOX::xmax, GBOX::xmin, GBOX::ymax, and GBOX::ymin.

1575 {
1576 #if POSTGIS_GEOS_VERSION < 35
1577 
1578  lwpgerror("The GEOS version this PostGIS binary "
1579  "was compiled against (%d) doesn't support "
1580  "'GEOSClipByRect' function (3.5.0+ required)",
1582  PG_RETURN_NULL();
1583 
1584 #else /* POSTGIS_GEOS_VERSION >= 35 */
1585 
1586  GSERIALIZED *geom1;
1587  GSERIALIZED *result;
1588  LWGEOM *lwgeom1, *lwresult ;
1589  const GBOX *bbox1;
1590  GBOX *bbox2;
1591 
1592  geom1 = PG_GETARG_GSERIALIZED_P(0);
1593  lwgeom1 = lwgeom_from_gserialized(geom1) ;
1594 
1595  bbox1 = lwgeom_get_bbox(lwgeom1);
1596  if ( ! bbox1 )
1597  {
1598  /* empty clips to empty, no matter rect */
1599  lwgeom_free(lwgeom1);
1600  PG_RETURN_POINTER(geom1);
1601  }
1602 
1603  /* WARNING: this is really a BOX2DF, use only xmin and ymin fields */
1604  bbox2 = (GBOX *)PG_GETARG_POINTER(1);
1605  bbox2->flags = 0;
1606 
1607  /* If bbox1 outside of bbox2, return empty */
1608  if ( ! gbox_overlaps_2d(bbox1, bbox2) )
1609  {
1610  lwresult = lwgeom_construct_empty(lwgeom1->type, lwgeom1->srid, 0, 0);
1611  lwgeom_free(lwgeom1);
1612  PG_FREE_IF_COPY(geom1, 0);
1613  result = geometry_serialize(lwresult) ;
1614  lwgeom_free(lwresult) ;
1615  PG_RETURN_POINTER(result);
1616  }
1617 
1618  /* if bbox1 is covered by bbox2, return lwgeom1 */
1619  if ( gbox_contains_2d(bbox2, bbox1) )
1620  {
1621  lwgeom_free(lwgeom1);
1622  PG_RETURN_POINTER(geom1);
1623  }
1624 
1625  lwresult = lwgeom_clip_by_rect(lwgeom1, bbox2->xmin, bbox2->ymin,
1626  bbox2->xmax, bbox2->ymax);
1627 
1628  lwgeom_free(lwgeom1);
1629  PG_FREE_IF_COPY(geom1, 0);
1630 
1631  if ( lwresult == NULL )
1632  PG_RETURN_NULL();
1633 
1634  result = geometry_serialize(lwresult) ;
1635  lwgeom_free(lwresult) ;
1636  PG_RETURN_POINTER(result);
1637 
1638 #endif /* POSTGIS_GEOS_VERSION >= 35 */
1639 }
#define POSTGIS_GEOS_VERSION
Definition: sqldefines.h:10
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
double xmax
Definition: liblwgeom.h:277
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
LWGEOM * lwgeom_clip_by_rect(const LWGEOM *geom1, double x0, double y0, double x1, double y1)
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: g_box.c:316
int32_t srid
Definition: liblwgeom.h:383
double ymin
Definition: liblwgeom.h:278
double xmin
Definition: liblwgeom.h:276
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:640
double ymax
Definition: liblwgeom.h:279
LWGEOM * lwgeom_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwgeom.c:1807
uint8_t flags
Definition: liblwgeom.h:275
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: g_box.c:300
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:380

Here is the call graph for this function: