PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ lwgeom_difference()

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

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

768 {
769  GEOSGeometry *g1, *g2, *g3;
770  LWGEOM *result;
771  int is3d;
772  int srid;
773 
774  /* A.Difference(Empty) == A */
775  if ( lwgeom_is_empty(geom2) )
776  return lwgeom_clone_deep(geom1);
777 
778  /* Empty.Intersection(A) == Empty */
779  if ( lwgeom_is_empty(geom1) )
780  return lwgeom_clone_deep(geom1);
781 
782  /* ensure srids are identical */
783  srid = (int)(geom1->srid);
784  error_if_srid_mismatch(srid, (int)(geom2->srid));
785 
786  is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ;
787 
788  initGEOS(lwnotice, lwgeom_geos_error);
789 
790  g1 = LWGEOM2GEOS(geom1, 0);
791  if ( 0 == g1 ) /* exception thrown at construction */
792  {
793  lwerror("First argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
794  return NULL;
795  }
796 
797  g2 = LWGEOM2GEOS(geom2, 0);
798  if ( 0 == g2 ) /* exception thrown at construction */
799  {
800  GEOSGeom_destroy(g1);
801  lwerror("Second argument geometry could not be converted to GEOS: %s", lwgeom_geos_errmsg);
802  return NULL;
803  }
804 
805  g3 = GEOSDifference(g1,g2);
806 
807  if (g3 == NULL)
808  {
809  GEOSGeom_destroy(g1);
810  GEOSGeom_destroy(g2);
811  lwerror("GEOSDifference: %s", lwgeom_geos_errmsg);
812  return NULL ; /* never get here */
813  }
814 
815  LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ;
816 
817  GEOSSetSRID(g3, srid);
818 
819  result = GEOS2LWGEOM(g3, is3d);
820 
821  if (result == NULL)
822  {
823  GEOSGeom_destroy(g1);
824  GEOSGeom_destroy(g2);
825  GEOSGeom_destroy(g3);
826  lwerror("Error performing difference: GEOS2LWGEOM: %s",
828  return NULL; /* never get here */
829  }
830 
831  GEOSGeom_destroy(g1);
832  GEOSGeom_destroy(g2);
833  GEOSGeom_destroy(g3);
834 
835  /* compressType(result); */
836 
837  return result;
838 }
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:89
uint8_t flags
Definition: liblwgeom.h:396
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:446
void error_if_srid_mismatch(int srid1, int srid2)
Definition: lwutil.c:369
int32_t srid
Definition: liblwgeom.h:398
void lwgeom_geos_error(const char *fmt,...)
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
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:1310
#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:102
Here is the call graph for this function:
Here is the caller graph for this function: