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

Definition at line 595 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 _lwt_AddLine(), geos_intersection(), mvt_geom(), and RASTER_clip().

596 {
597  LWGEOM *result ;
598  GEOSGeometry *g1, *g2, *g3 ;
599  int is3d ;
600  int srid ;
601 
602  /* A.Intersection(Empty) == Empty */
603  if ( lwgeom_is_empty(geom2) )
604  return lwgeom_clone_deep(geom2);
605 
606  /* Empty.Intersection(A) == Empty */
607  if ( lwgeom_is_empty(geom1) )
608  return lwgeom_clone_deep(geom1);
609 
610  /* ensure srids are identical */
611  srid = (int)(geom1->srid);
612  error_if_srid_mismatch(srid, (int)(geom2->srid));
613 
614  is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ;
615 
616  initGEOS(lwnotice, lwgeom_geos_error);
617 
618  LWDEBUG(3, "intersection() START");
619 
620  g1 = LWGEOM2GEOS(geom1, 0);
621  if ( 0 == g1 ) /* exception thrown at construction */
622  {
623  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
624  return NULL ;
625  }
626 
627  g2 = LWGEOM2GEOS(geom2, 0);
628  if ( 0 == g2 ) /* exception thrown at construction */
629  {
630  lwerror("Second argument geometry could not be converted to GEOS.");
631  GEOSGeom_destroy(g1);
632  return NULL ;
633  }
634 
635  LWDEBUG(3, " constructed geometrys - calling geos");
636  LWDEBUGF(3, " g1 = %s", GEOSGeomToWKT(g1));
637  LWDEBUGF(3, " g2 = %s", GEOSGeomToWKT(g2));
638  /*LWDEBUGF(3, "g2 is valid = %i",GEOSisvalid(g2)); */
639  /*LWDEBUGF(3, "g1 is valid = %i",GEOSisvalid(g1)); */
640 
641  g3 = GEOSIntersection(g1,g2);
642 
643  LWDEBUG(3, " intersection finished");
644 
645  if (g3 == NULL)
646  {
647  GEOSGeom_destroy(g1);
648  GEOSGeom_destroy(g2);
649  lwerror("Error performing intersection: %s",
651  return NULL; /* never get here */
652  }
653 
654  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ;
655 
656  GEOSSetSRID(g3, srid);
657 
658  result = GEOS2LWGEOM(g3, is3d);
659 
660  if (result == NULL)
661  {
662  GEOSGeom_destroy(g1);
663  GEOSGeom_destroy(g2);
664  GEOSGeom_destroy(g3);
665  lwerror("Error performing intersection: GEOS2LWGEOM: %s",
667  return NULL ; /* never get here */
668  }
669 
670  GEOSGeom_destroy(g1);
671  GEOSGeom_destroy(g2);
672  GEOSGeom_destroy(g3);
673 
674  return result ;
675 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
uint8_t flags
Definition: liblwgeom.h:396
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:513
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:338
int32_t srid
Definition: liblwgeom.h:398
void lwgeom_geos_error(const char *fmt,...)
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
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:1386
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190

Here is the call graph for this function:

Here is the caller graph for this function: