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

Definition at line 571 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().

572 {
573  LWGEOM *result ;
574  GEOSGeometry *g1, *g2, *g3 ;
575  int is3d ;
576  int srid ;
577 
578  /* A.Intersection(Empty) == Empty */
579  if ( lwgeom_is_empty(geom2) )
580  return lwgeom_clone_deep(geom2);
581 
582  /* Empty.Intersection(A) == Empty */
583  if ( lwgeom_is_empty(geom1) )
584  return lwgeom_clone_deep(geom1);
585 
586  /* ensure srids are identical */
587  srid = (int)(geom1->srid);
588  error_if_srid_mismatch(srid, (int)(geom2->srid));
589 
590  is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ;
591 
592  initGEOS(lwnotice, lwgeom_geos_error);
593 
594  LWDEBUG(3, "intersection() START");
595 
596  g1 = LWGEOM2GEOS(geom1, 0);
597  if ( 0 == g1 ) /* exception thrown at construction */
598  {
599  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
600  return NULL ;
601  }
602 
603  g2 = LWGEOM2GEOS(geom2, 0);
604  if ( 0 == g2 ) /* exception thrown at construction */
605  {
606  lwerror("Second argument geometry could not be converted to GEOS.");
607  GEOSGeom_destroy(g1);
608  return NULL ;
609  }
610 
611  LWDEBUG(3, " constructed geometrys - calling geos");
612  LWDEBUGF(3, " g1 = %s", GEOSGeomToWKT(g1));
613  LWDEBUGF(3, " g2 = %s", GEOSGeomToWKT(g2));
614  /*LWDEBUGF(3, "g2 is valid = %i",GEOSisvalid(g2)); */
615  /*LWDEBUGF(3, "g1 is valid = %i",GEOSisvalid(g1)); */
616 
617  g3 = GEOSIntersection(g1,g2);
618 
619  LWDEBUG(3, " intersection finished");
620 
621  if (g3 == NULL)
622  {
623  GEOSGeom_destroy(g1);
624  GEOSGeom_destroy(g2);
625  lwerror("Error performing intersection: %s",
627  return NULL; /* never get here */
628  }
629 
630  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ;
631 
632  GEOSSetSRID(g3, srid);
633 
634  result = GEOS2LWGEOM(g3, is3d);
635 
636  if (result == NULL)
637  {
638  GEOSGeom_destroy(g1);
639  GEOSGeom_destroy(g2);
640  GEOSGeom_destroy(g3);
641  lwerror("Error performing intersection: GEOS2LWGEOM: %s",
643  return NULL ; /* never get here */
644  }
645 
646  GEOSGeom_destroy(g1);
647  GEOSGeom_destroy(g2);
648  GEOSGeom_destroy(g3);
649 
650  return result ;
651 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:89
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:446
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:369
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:1310
#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:102

Here is the call graph for this function:

Here is the caller graph for this function: