PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ lwgeom_difference()

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

Definition at line 796 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().

797 {
798  GEOSGeometry *g1, *g2, *g3;
799  LWGEOM *result;
800  int is3d;
801  int srid;
802 
803  /* A.Difference(Empty) == A */
804  if ( lwgeom_is_empty(geom2) )
805  return lwgeom_clone_deep(geom1);
806 
807  /* Empty.Intersection(A) == Empty */
808  if ( lwgeom_is_empty(geom1) )
809  return lwgeom_clone_deep(geom1);
810 
811  /* ensure srids are identical */
812  srid = (int)(geom1->srid);
813  error_if_srid_mismatch(srid, (int)(geom2->srid));
814 
815  is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ;
816 
817  initGEOS(lwnotice, lwgeom_geos_error);
818 
819  g1 = LWGEOM2GEOS(geom1, 0);
820  if ( 0 == g1 ) /* exception thrown at construction */
821  {
822  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
823  return NULL;
824  }
825 
826  g2 = LWGEOM2GEOS(geom2, 0);
827  if ( 0 == g2 ) /* exception thrown at construction */
828  {
829  GEOSGeom_destroy(g1);
830  lwerror("Second argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
831  return NULL;
832  }
833 
834  g3 = GEOSDifference(g1,g2);
835 
836  if (g3 == NULL)
837  {
838  GEOSGeom_destroy(g1);
839  GEOSGeom_destroy(g2);
840  lwerror("GEOSDifference: %s", lwgeom_geos_errmsg);
841  return NULL ; /* never get here */
842  }
843 
844  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ;
845 
846  GEOSSetSRID(g3, srid);
847 
848  result = GEOS2LWGEOM(g3, is3d);
849 
850  if (result == NULL)
851  {
852  GEOSGeom_destroy(g1);
853  GEOSGeom_destroy(g2);
854  GEOSGeom_destroy(g3);
855  lwerror("Error performing difference: GEOS2LWGEOM: %s",
857  return NULL; /* never get here */
858  }
859 
860  GEOSGeom_destroy(g1);
861  GEOSGeom_destroy(g2);
862  GEOSGeom_destroy(g3);
863 
864  /* compressType(result); */
865 
866  return result;
867 }
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]
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: