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

Definition at line 1372 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.

1373 {
1374 #if POSTGIS_GEOS_VERSION < 35
1375 
1376  lwpgerror("The GEOS version this PostGIS binary "
1377  "was compiled against (%d) doesn't support "
1378  "'GEOSClipByRect' function (3.5.0+ required)",
1380  PG_RETURN_NULL();
1381 
1382 #else /* POSTGIS_GEOS_VERSION >= 35 */
1383 
1384  GSERIALIZED *geom1;
1385  GSERIALIZED *result;
1386  LWGEOM *lwgeom1, *lwresult ;
1387  const GBOX *bbox1;
1388  GBOX *bbox2;
1389 
1390  geom1 = PG_GETARG_GSERIALIZED_P(0);
1391  lwgeom1 = lwgeom_from_gserialized(geom1) ;
1392 
1393  bbox1 = lwgeom_get_bbox(lwgeom1);
1394  if ( ! bbox1 )
1395  {
1396  /* empty clips to empty, no matter rect */
1397  lwgeom_free(lwgeom1);
1398  PG_RETURN_POINTER(geom1);
1399  }
1400 
1401  /* WARNING: this is really a BOX2DF, use only xmin and ymin fields */
1402  bbox2 = (GBOX *)PG_GETARG_POINTER(1);
1403  bbox2->flags = 0;
1404 
1405  /* If bbox1 outside of bbox2, return empty */
1406  if ( ! gbox_overlaps_2d(bbox1, bbox2) )
1407  {
1408  lwresult = lwgeom_construct_empty(lwgeom1->type, lwgeom1->srid, 0, 0);
1409  lwgeom_free(lwgeom1);
1410  PG_FREE_IF_COPY(geom1, 0);
1411  result = geometry_serialize(lwresult) ;
1412  lwgeom_free(lwresult) ;
1413  PG_RETURN_POINTER(result);
1414  }
1415 
1416  /* if bbox1 is covered by bbox2, return lwgeom1 */
1417  if ( gbox_contains_2d(bbox2, bbox1) )
1418  {
1419  lwgeom_free(lwgeom1);
1420  PG_RETURN_POINTER(geom1);
1421  }
1422 
1423  lwresult = lwgeom_clip_by_rect(lwgeom1, bbox2->xmin, bbox2->ymin,
1424  bbox2->xmax, bbox2->ymax);
1425 
1426  lwgeom_free(lwgeom1);
1427  PG_FREE_IF_COPY(geom1, 0);
1428 
1429  if (!lwresult) PG_RETURN_NULL();
1430 
1431  result = geometry_serialize(lwresult) ;
1432  lwgeom_free(lwresult) ;
1433  PG_RETURN_POINTER(result);
1434 
1435 #endif /* POSTGIS_GEOS_VERSION >= 35 */
1436 }
#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:292
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
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:346
int32_t srid
Definition: liblwgeom.h:398
double ymin
Definition: liblwgeom.h:293
double xmin
Definition: liblwgeom.h:291
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:727
double ymax
Definition: liblwgeom.h:294
LWGEOM * lwgeom_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwgeom.c:2078
uint8_t flags
Definition: liblwgeom.h:290
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:330
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:395

Here is the call graph for this function: