PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@

◆ lwgeom_intersection()

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

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

References LWGEOM::flags, FLAGS_GET_Z, geos_clean(), geos_clean_and_fail(), get_result_srid(), input_lwgeom_to_geos(), lwgeom_clone_deep(), lwgeom_geos_errmsg, lwgeom_geos_error(), LWGEOM_GEOS_makeValid(), lwgeom_is_empty(), lwnotice(), output_geos_as_lwgeom(), and SRID_INVALID.

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

590 {
591  LWGEOM* result;
592  int32_t srid = get_result_srid(geom1, geom2, __func__);
593  uint8_t is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags));
594  GEOSGeometry* g1;
595  GEOSGeometry* g2;
596  GEOSGeometry* g3;
597 
598  if (srid == SRID_INVALID) return NULL;
599 
600  /* A.Intersection(Empty) == Empty */
601  if (lwgeom_is_empty(geom2)) return lwgeom_clone_deep(geom2); /* match empty type? */
602 
603  /* Empty.Intersection(A) == Empty */
604  if (lwgeom_is_empty(geom1)) return lwgeom_clone_deep(geom1); /* match empty type? */
605 
606  initGEOS(lwnotice, lwgeom_geos_error);
607 
608  if (!input_lwgeom_to_geos(&g1, geom1, __func__)) return NULL;
609  if (!input_lwgeom_to_geos(&g2, geom2, __func__)) return geos_clean_and_fail(g1, NULL, NULL, __func__);
610 
611  g3 = GEOSIntersection(g1, g2);
612 
613  if (!g3)
614  {
615  GEOSGeometry *g1v, *g2v;
616  lwnotice("%s: GEOS Error: %s", __func__, lwgeom_geos_errmsg);
617 
618  if (!GEOSisValid(g1) || !GEOSisValid(g2))
619  {
620  lwnotice(
621  "Your geometry dataset is not valid per OGC Specification. "
622  "Please fix it with manual review of entries that are not ST_IsValid(geom). "
623  "Retrying GEOS operation with ST_MakeValid of your input.");
624  g1v = LWGEOM_GEOS_makeValid(g1);
625  g2v = LWGEOM_GEOS_makeValid(g2);
626  g3 = GEOSIntersection(g1v, g2v);
627  geos_clean(g1v, g2v, NULL);
628  }
629  }
630 
631  if (!g3) return geos_clean_and_fail(g1, g2, NULL, __func__);
632 
633  if (!output_geos_as_lwgeom(&g3, &result, srid, is3d, __func__))
634  return geos_clean_and_fail(g1, g2, g3, __func__);
635 
636  geos_clean(g1, g2, g3);
637  return result;
638 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
uint8_t flags
Definition: liblwgeom.h:399
static LWGEOM * geos_clean_and_fail(GEOSGeometry *g1, GEOSGeometry *g2, GEOSGeometry *g3, const char *funcname)
GEOSGeometry * LWGEOM_GEOS_makeValid(const GEOSGeometry *)
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:520
static uint8_t output_geos_as_lwgeom(GEOSGeometry **g, LWGEOM **geom, const int32_t srid, const uint8_t is3d, const char *funcname)
void lwgeom_geos_error(const char *fmt,...)
static void geos_clean(GEOSGeometry *g1, GEOSGeometry *g2, GEOSGeometry *g3)
#define SRID_INVALID
Definition: liblwgeom.h:191
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
static uint8_t input_lwgeom_to_geos(GEOSGeometry **g, const LWGEOM *geom, const char *funcname)
static int32_t get_result_srid(const LWGEOM *geom1, const LWGEOM *geom2, const char *funcname)
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: