PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ lwgeom_intersection()

LWGEOM* lwgeom_intersection ( const LWGEOM geom1,
const LWGEOM geom2 
)

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

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