PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ ST_ClipByBox2d()

Datum ST_ClipByBox2d ( PG_FUNCTION_ARGS  )

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

Referenced by centroid().

1293 {
1294  GSERIALIZED *geom1;
1295  GSERIALIZED *result;
1296  LWGEOM *lwgeom1, *lwresult ;
1297  const GBOX *bbox1;
1298  GBOX *bbox2;
1299 
1300  geom1 = PG_GETARG_GSERIALIZED_P(0);
1301  lwgeom1 = lwgeom_from_gserialized(geom1) ;
1302 
1303  bbox1 = lwgeom_get_bbox(lwgeom1);
1304  if ( ! bbox1 )
1305  {
1306  /* empty clips to empty, no matter rect */
1307  lwgeom_free(lwgeom1);
1308  PG_RETURN_POINTER(geom1);
1309  }
1310 
1311  /* WARNING: this is really a BOX2DF, use only xmin and ymin fields */
1312  bbox2 = (GBOX *)PG_GETARG_POINTER(1);
1313  bbox2->flags = 0;
1314 
1315  /* If bbox1 outside of bbox2, return empty */
1316  if ( ! gbox_overlaps_2d(bbox1, bbox2) )
1317  {
1318  lwresult = lwgeom_construct_empty(lwgeom1->type, lwgeom1->srid, 0, 0);
1319  lwgeom_free(lwgeom1);
1320  PG_FREE_IF_COPY(geom1, 0);
1321  result = geometry_serialize(lwresult) ;
1322  lwgeom_free(lwresult) ;
1323  PG_RETURN_POINTER(result);
1324  }
1325 
1326  /* if bbox1 is covered by bbox2, return lwgeom1 */
1327  if ( gbox_contains_2d(bbox2, bbox1) )
1328  {
1329  lwgeom_free(lwgeom1);
1330  PG_RETURN_POINTER(geom1);
1331  }
1332 
1333  lwresult = lwgeom_clip_by_rect(lwgeom1, bbox2->xmin, bbox2->ymin,
1334  bbox2->xmax, bbox2->ymax);
1335 
1336  lwgeom_free(lwgeom1);
1337  PG_FREE_IF_COPY(geom1, 0);
1338 
1339  if (!lwresult) PG_RETURN_NULL();
1340 
1341  result = geometry_serialize(lwresult) ;
1342  lwgeom_free(lwresult) ;
1343  PG_RETURN_POINTER(result);
1344 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
double xmax
Definition: liblwgeom.h:295
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
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:401
double ymin
Definition: liblwgeom.h:296
double xmin
Definition: liblwgeom.h:294
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:734
double ymax
Definition: liblwgeom.h:297
LWGEOM * lwgeom_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwgeom.c:2085
uint8_t flags
Definition: liblwgeom.h:293
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:398
Here is the call graph for this function:
Here is the caller graph for this function: