PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ lwgeom_difference()

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

Definition at line 741 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(), and LWGEOM::srid.

Referenced by geos_difference(), and lwt_AddLine().

742 {
743  GEOSGeometry *g1, *g2, *g3;
744  LWGEOM *result;
745  int is3d;
746  int srid;
747 
748  /* A.Difference(Empty) == A */
749  if ( lwgeom_is_empty(geom2) )
750  return lwgeom_clone_deep(geom1);
751 
752  /* Empty.Intersection(A) == Empty */
753  if ( lwgeom_is_empty(geom1) )
754  return lwgeom_clone_deep(geom1);
755 
756  /* ensure srids are identical */
757  srid = (int)(geom1->srid);
758  error_if_srid_mismatch(srid, (int)(geom2->srid));
759 
760  is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ;
761 
762  initGEOS(lwnotice, lwgeom_geos_error);
763 
764  g1 = LWGEOM2GEOS(geom1, 0);
765  if ( 0 == g1 ) /* exception thrown at construction */
766  {
767  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
768  return NULL;
769  }
770 
771  g2 = LWGEOM2GEOS(geom2, 0);
772  if ( 0 == g2 ) /* exception thrown at construction */
773  {
774  GEOSGeom_destroy(g1);
775  lwerror("Second argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
776  return NULL;
777  }
778 
779  g3 = GEOSDifference(g1,g2);
780 
781  if (g3 == NULL)
782  {
783  GEOSGeom_destroy(g1);
784  GEOSGeom_destroy(g2);
785  lwerror("GEOSDifference: %s", lwgeom_geos_errmsg);
786  return NULL ; /* never get here */
787  }
788 
789  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ;
790 
791  GEOSSetSRID(g3, srid);
792 
793  result = GEOS2LWGEOM(g3, is3d);
794 
795  if (result == NULL)
796  {
797  GEOSGeom_destroy(g1);
798  GEOSGeom_destroy(g2);
799  GEOSGeom_destroy(g3);
800  lwerror("Error performing difference: GEOS2LWGEOM: %s",
802  return NULL; /* never get here */
803  }
804 
805  GEOSGeom_destroy(g1);
806  GEOSGeom_destroy(g2);
807  GEOSGeom_destroy(g3);
808 
809  /* compressType(result); */
810 
811  return result;
812 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:61
uint8_t flags
Definition: liblwgeom.h:381
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:433
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:341
int32_t srid
Definition: liblwgeom.h:383
void lwgeom_geos_error(const char *fmt,...)
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:124
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:1297
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
Here is the call graph for this function:
Here is the caller graph for this function: