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

Definition at line 545 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(), and LWGEOM::srid.

Referenced by geos_intersection(), lwt_AddLine(), and RASTER_clip().

546 {
547  LWGEOM *result ;
548  GEOSGeometry *g1, *g2, *g3 ;
549  int is3d ;
550  int srid ;
551 
552  /* A.Intersection(Empty) == Empty */
553  if ( lwgeom_is_empty(geom2) )
554  return lwgeom_clone_deep(geom2);
555 
556  /* Empty.Intersection(A) == Empty */
557  if ( lwgeom_is_empty(geom1) )
558  return lwgeom_clone_deep(geom1);
559 
560  /* ensure srids are identical */
561  srid = (int)(geom1->srid);
562  error_if_srid_mismatch(srid, (int)(geom2->srid));
563 
564  is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ;
565 
566  initGEOS(lwnotice, lwgeom_geos_error);
567 
568  LWDEBUG(3, "intersection() START");
569 
570  g1 = LWGEOM2GEOS(geom1, 0);
571  if ( 0 == g1 ) /* exception thrown at construction */
572  {
573  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
574  return NULL ;
575  }
576 
577  g2 = LWGEOM2GEOS(geom2, 0);
578  if ( 0 == g2 ) /* exception thrown at construction */
579  {
580  lwerror("Second argument geometry could not be converted to GEOS.");
581  GEOSGeom_destroy(g1);
582  return NULL ;
583  }
584 
585  LWDEBUG(3, " constructed geometrys - calling geos");
586  LWDEBUGF(3, " g1 = %s", GEOSGeomToWKT(g1));
587  LWDEBUGF(3, " g2 = %s", GEOSGeomToWKT(g2));
588  /*LWDEBUGF(3, "g2 is valid = %i",GEOSisvalid(g2)); */
589  /*LWDEBUGF(3, "g1 is valid = %i",GEOSisvalid(g1)); */
590 
591  g3 = GEOSIntersection(g1,g2);
592 
593  LWDEBUG(3, " intersection finished");
594 
595  if (g3 == NULL)
596  {
597  GEOSGeom_destroy(g1);
598  GEOSGeom_destroy(g2);
599  lwerror("Error performing intersection: %s",
601  return NULL; /* never get here */
602  }
603 
604  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ;
605 
606  GEOSSetSRID(g3, srid);
607 
608  result = GEOS2LWGEOM(g3, is3d);
609 
610  if (result == NULL)
611  {
612  GEOSGeom_destroy(g1);
613  GEOSGeom_destroy(g2);
614  GEOSGeom_destroy(g3);
615  lwerror("Error performing intersection: GEOS2LWGEOM: %s",
617  return NULL ; /* never get here */
618  }
619 
620  GEOSGeom_destroy(g1);
621  GEOSGeom_destroy(g2);
622  GEOSGeom_destroy(g3);
623 
624  return result ;
625 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:61
uint8_t flags
Definition: liblwgeom.h:381
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:433
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:341
int32_t srid
Definition: liblwgeom.h:383
void lwgeom_geos_error(const char *fmt,...)
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:124
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
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:1297
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74

Here is the call graph for this function:

Here is the caller graph for this function: