PostGIS  2.1.10dev-r@@SVN_REVISION@@
LWGEOM* lwgeom_difference ( const LWGEOM geom1,
const LWGEOM geom2 
)

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

References error_if_srid_mismatch(), LWGEOM::flags, FLAGS_GET_Z, GEOS2LWGEOM(), LWDEBUGF, lwerror(), LWGEOM2GEOS(), lwgeom_clone_deep(), lwgeom_geos_errmsg, lwgeom_geos_error(), lwgeom_is_empty(), lwnotice(), result, and LWGEOM::srid.

Referenced by difference().

561 {
562  GEOSGeometry *g1, *g2, *g3;
563  LWGEOM *result;
564  int is3d;
565  int srid;
566 
567  /* A.Difference(Empty) == A */
568  if ( lwgeom_is_empty(geom2) )
569  return lwgeom_clone_deep(geom1);
570 
571  /* Empty.Intersection(A) == Empty */
572  if ( lwgeom_is_empty(geom1) )
573  return lwgeom_clone_deep(geom1);
574 
575  /* ensure srids are identical */
576  srid = (int)(geom1->srid);
577  error_if_srid_mismatch(srid, (int)(geom2->srid));
578 
579  is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ;
580 
581  initGEOS(lwnotice, lwgeom_geos_error);
582 
583  g1 = LWGEOM2GEOS(geom1);
584  if ( 0 == g1 ) /* exception thrown at construction */
585  {
586  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
587  return NULL;
588  }
589 
590  g2 = LWGEOM2GEOS(geom2);
591  if ( 0 == g2 ) /* exception thrown at construction */
592  {
593  GEOSGeom_destroy(g1);
594  lwerror("Second argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
595  return NULL;
596  }
597 
598  g3 = GEOSDifference(g1,g2);
599 
600  if (g3 == NULL)
601  {
602  GEOSGeom_destroy(g1);
603  GEOSGeom_destroy(g2);
604  lwerror("GEOSDifference: %s", lwgeom_geos_errmsg);
605  return NULL ; /* never get here */
606  }
607 
608  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ;
609 
610  GEOSSetSRID(g3, srid);
611 
612  result = GEOS2LWGEOM(g3, is3d);
613 
614  if (result == NULL)
615  {
616  GEOSGeom_destroy(g1);
617  GEOSGeom_destroy(g2);
618  GEOSGeom_destroy(g3);
619  lwerror("Error performing difference: GEOS2LWGEOM: %s",
621  return NULL; /* never get here */
622  }
623 
624  GEOSGeom_destroy(g1);
625  GEOSGeom_destroy(g2);
626  GEOSGeom_destroy(g3);
627 
628  /* compressType(result); */
629 
630  return result;
631 }
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:317
uint8_t flags
Definition: liblwgeom.h:353
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:389
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom)
char ** result
Definition: liblwgeom.h:218
int32_t srid
Definition: liblwgeom.h:355
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:54
void lwgeom_geos_error(const char *fmt,...)
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:106
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:1229
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55

Here is the call graph for this function:

Here is the caller graph for this function: