PostGIS  2.1.10dev-r@@SVN_REVISION@@
LWGEOM* lwgeom_intersection ( const LWGEOM geom1,
const LWGEOM geom2 
)

Definition at line 477 of file liblwgeom/lwgeom_geos.c.

References error_if_srid_mismatch(), LWGEOM::flags, FLAGS_GET_Z, GEOS2LWGEOM(), LWDEBUG, LWDEBUGF, lwerror(), LWGEOM2GEOS(), lwgeom_clone_deep(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwgeom_is_empty(), lwnotice(), result, and LWGEOM::srid.

Referenced by geos_intersection(), and RASTER_clip().

478 {
479  LWGEOM *result ;
480  GEOSGeometry *g1, *g2, *g3 ;
481  int is3d ;
482  int srid ;
483 
484  /* A.Intersection(Empty) == Empty */
485  if ( lwgeom_is_empty(geom2) )
486  return lwgeom_clone_deep(geom2);
487 
488  /* Empty.Intersection(A) == Empty */
489  if ( lwgeom_is_empty(geom1) )
490  return lwgeom_clone_deep(geom1);
491 
492  /* ensure srids are identical */
493  srid = (int)(geom1->srid);
494  error_if_srid_mismatch(srid, (int)(geom2->srid));
495 
496  is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ;
497 
498  initGEOS(lwnotice, lwgeom_geos_error);
499 
500  LWDEBUG(3, "intersection() START");
501 
502  g1 = LWGEOM2GEOS(geom1);
503  if ( 0 == g1 ) /* exception thrown at construction */
504  {
505  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
506  return NULL ;
507  }
508 
509  g2 = LWGEOM2GEOS(geom2);
510  if ( 0 == g2 ) /* exception thrown at construction */
511  {
512  lwerror("Second argument geometry could not be converted to GEOS.");
513  GEOSGeom_destroy(g1);
514  return NULL ;
515  }
516 
517  LWDEBUG(3, " constructed geometrys - calling geos");
518  LWDEBUGF(3, " g1 = %s", GEOSGeomToWKT(g1));
519  LWDEBUGF(3, " g2 = %s", GEOSGeomToWKT(g2));
520  /*LWDEBUGF(3, "g2 is valid = %i",GEOSisvalid(g2)); */
521  /*LWDEBUGF(3, "g1 is valid = %i",GEOSisvalid(g1)); */
522 
523  g3 = GEOSIntersection(g1,g2);
524 
525  LWDEBUG(3, " intersection finished");
526 
527  if (g3 == NULL)
528  {
529  GEOSGeom_destroy(g1);
530  GEOSGeom_destroy(g2);
531  lwerror("Error performing intersection: %s",
533  return NULL; /* never get here */
534  }
535 
536  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ;
537 
538  GEOSSetSRID(g3, srid);
539 
540  result = GEOS2LWGEOM(g3, is3d);
541 
542  if (result == NULL)
543  {
544  GEOSGeom_destroy(g1);
545  GEOSGeom_destroy(g2);
546  GEOSGeom_destroy(g3);
547  lwerror("Error performing intersection: GEOS2LWGEOM: %s",
549  return NULL ; /* never get here */
550  }
551 
552  GEOSGeom_destroy(g1);
553  GEOSGeom_destroy(g2);
554  GEOSGeom_destroy(g3);
555 
556  return result ;
557 }
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:317
uint8_t flags
Definition: liblwgeom.h:353
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:389
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom)
char ** result
Definition: liblwgeom.h:218
int32_t srid
Definition: liblwgeom.h:355
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:54
void lwgeom_geos_error(const char *fmt,...)
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:106
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, char want3d)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1229
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55

Here is the call graph for this function:

Here is the caller graph for this function: