PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ lwgeom_intersection()

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

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

References AUTOFIX, LWGEOM::flags, FLAGS_GET_Z, GEOS2LWGEOM(), GEOS_FAIL, GEOS_FREE, GEOS_FREE_AND_FAIL, LWGEOM2GEOS(), lwgeom_clone_deep(), lwgeom_geos_errmsg, lwgeom_geos_error(), LWGEOM_GEOS_makeValid(), lwgeom_is_empty(), lwnotice(), RESULT_SRID, and SRID_INVALID.

Referenced by _lwt_AddLine(), geos_intersection(), lwgeom_clip_by_rect(), and RASTER_clip().

598 {
599  LWGEOM* result;
600  int32_t srid = RESULT_SRID(geom1, geom2);
601  uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
602  GEOSGeometry* g1;
603  GEOSGeometry* g2;
604  GEOSGeometry* g3;
605 
606  if (srid == SRID_INVALID) return NULL;
607 
608  /* A.Intersection(Empty) == Empty */
609  if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom2); /* match empty type? */
610 
611  /* Empty.Intersection(A) == Empty */
612  if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom1); /* match empty type? */
613 
614  initGEOS(lwnotice, lwgeom_geos_error);
615 
616  if (!(g1 = LWGEOM2GEOS(geom1, AUTOFIX))) GEOS_FAIL();
617  if (!(g2 = LWGEOM2GEOS(geom2, AUTOFIX))) GEOS_FREE_AND_FAIL(g1);
618 
619  g3 = GEOSIntersection(g1, g2);
620 
621  if (!g3)
622  {
623  GEOSGeometry *g1v, *g2v;
624  lwnotice("%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg);
625 
626  if (!GEOSisValid(g1) || !GEOSisValid(g2))
627  {
628  lwnotice(
629  "Your geometry dataset is not valid per OGC Specification. "
630  "Please fix it with manual review of entries that are not ST_IsValid(geom). "
631  "Retrying GEOS operation with ST_MakeValid of your input.");
632  g1v = LWGEOM_GEOS_makeValid(g1);
633  g2v = LWGEOM_GEOS_makeValid(g2);
634  g3 = GEOSIntersection(g1v, g2v);
635  GEOS_FREE(g1v, g2v);
636  }
637  }
638 
639  if (!g3) GEOS_FREE_AND_FAIL(g1);
640  GEOSSetSRID(g3, srid);
641 
642  if (!(result = GEOS2LWGEOM(g3, is3d))) GEOS_FREE_AND_FAIL(g1, g2, g3);
643 
644  GEOS_FREE(g1, g2, g3);
645  return result;
646 }
#define GEOS_FAIL()
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, uint8_t want3d)
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
uint8_t flags
Definition: liblwgeom.h:399
GEOSGeometry * LWGEOM_GEOS_makeValid(const GEOSGeometry *)
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
#define GEOS_FREE_AND_FAIL(...)
#define GEOS_FREE(...)
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:520
#define RESULT_SRID(...)
#define AUTOFIX
void lwgeom_geos_error(const char *fmt,...)
#define SRID_INVALID
Definition: liblwgeom.h:191
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
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:1393
unsigned char uint8_t
Definition: uthash.h:79
Here is the call graph for this function:
Here is the caller graph for this function: