PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ lwgeom_boundary()

LWGEOM* lwgeom_boundary ( LWGEOM lwgeom)

Definition at line 2686 of file lwgeom.c.

2687 {
2688  int32_t srid = lwgeom_get_srid(lwgeom);
2689  uint8_t hasz = lwgeom_has_z(lwgeom);
2690  uint8_t hasm = lwgeom_has_m(lwgeom);
2691 
2692  switch (lwgeom->type)
2693  {
2694  case POINTTYPE:
2695  case MULTIPOINTTYPE: {
2696  return lwgeom_construct_empty(lwgeom->type, srid, hasz, hasm);
2697  }
2698  case LINETYPE:
2699  case CIRCSTRINGTYPE: {
2700  if (lwgeom_is_closed(lwgeom) || lwgeom_is_empty(lwgeom))
2701  return (LWGEOM *)lwmpoint_construct_empty(srid, hasz, hasm);
2702  else
2703  {
2704  LWLINE *lwline = (LWLINE *)lwgeom;
2705  LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
2706  POINT4D pt;
2707  getPoint4d_p(lwline->points, 0, &pt);
2708  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
2709  getPoint4d_p(lwline->points, lwline->points->npoints - 1, &pt);
2710  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
2711 
2712  return (LWGEOM *)lwmpoint;
2713  }
2714  }
2715  case MULTILINETYPE:
2716  case MULTICURVETYPE: {
2717  LWMLINE *lwmline = (LWMLINE *)lwgeom;
2718  POINT4D *out = lwalloc(sizeof(POINT4D) * lwmline->ngeoms * 2);
2719  uint32_t n = 0;
2720 
2721  for (uint32_t i = 0; i < lwmline->ngeoms; i++)
2722  {
2723  LWMPOINT *points = lwgeom_as_lwmpoint(lwgeom_boundary((LWGEOM *)lwmline->geoms[i]));
2724  if (!points)
2725  continue;
2726 
2727  for (uint32_t k = 0; k < points->ngeoms; k++)
2728  {
2729  POINT4D pt = getPoint4d(points->geoms[k]->point, 0);
2730 
2731  uint8_t seen = LW_FALSE;
2732  for (uint32_t j = 0; j < n; j++)
2733  {
2734  if (memcmp(&(out[j]), &pt, sizeof(POINT4D)) == 0)
2735  {
2736  seen = LW_TRUE;
2737  out[j] = out[--n];
2738  break;
2739  }
2740  }
2741  if (!seen)
2742  out[n++] = pt;
2743  }
2744 
2745  lwgeom_free((LWGEOM *)points);
2746  }
2747 
2748  LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
2749 
2750  for (uint32_t i = 0; i < n; i++)
2751  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &(out[i])));
2752 
2753  lwfree(out);
2754 
2755  return (LWGEOM *)lwmpoint;
2756  }
2757  case TRIANGLETYPE: {
2758  LWTRIANGLE *lwtriangle = (LWTRIANGLE *)lwgeom;
2759  POINTARRAY *points = ptarray_clone_deep(lwtriangle->points);
2760  return (LWGEOM *)lwline_construct(srid, 0, points);
2761  }
2762  case POLYGONTYPE: {
2763  LWPOLY *lwpoly = (LWPOLY *)lwgeom;
2764 
2765  LWMLINE *lwmline = lwmline_construct_empty(srid, hasz, hasm);
2766  for (uint32_t i = 0; i < lwpoly->nrings; i++)
2767  {
2768  POINTARRAY *ring = ptarray_clone_deep(lwpoly->rings[i]);
2769  lwmline_add_lwline(lwmline, lwline_construct(srid, 0, ring));
2770  }
2771 
2772  /* Homogenize the multilinestring to hopefully get a single LINESTRING */
2773  LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwmline);
2774  lwgeom_free((LWGEOM *)lwmline);
2775  return lwout;
2776  }
2777  case CURVEPOLYTYPE: {
2778  LWCURVEPOLY *lwcurvepoly = (LWCURVEPOLY *)lwgeom;
2779  LWCOLLECTION *lwcol = lwcollection_construct_empty(MULTICURVETYPE, srid, hasz, hasm);
2780 
2781  for (uint32_t i = 0; i < lwcurvepoly->nrings; i++)
2782  lwcol = lwcollection_add_lwgeom(lwcol, lwgeom_clone_deep(lwcurvepoly->rings[i]));
2783 
2784  return (LWGEOM *)lwcol;
2785  }
2786  case MULTIPOLYGONTYPE:
2787  case COLLECTIONTYPE:
2788  case TINTYPE: {
2789  LWCOLLECTION *lwcol = (LWCOLLECTION *)lwgeom;
2790  LWCOLLECTION *lwcol_boundary = lwcollection_construct_empty(COLLECTIONTYPE, srid, hasz, hasm);
2791 
2792  for (uint32_t i = 0; i < lwcol->ngeoms; i++)
2793  lwcollection_add_lwgeom(lwcol_boundary, lwgeom_boundary(lwcol->geoms[i]));
2794 
2795  LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwcol_boundary);
2796  lwgeom_free((LWGEOM *)lwcol_boundary);
2797 
2798  return lwout;
2799  }
2800  default:
2801  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(lwgeom->type));
2802  return NULL;
2803  }
2804 }
POINT4D getPoint4d(const POINTARRAY *pa, uint32_t n)
Definition: lwgeom_api.c:107
#define LW_FALSE
Definition: liblwgeom.h:94
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
#define CURVEPOLYTYPE
Definition: liblwgeom.h:111
#define MULTILINETYPE
Definition: liblwgeom.h:106
#define LINETYPE
Definition: liblwgeom.h:103
#define MULTIPOINTTYPE
Definition: liblwgeom.h:105
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:643
LWMLINE * lwmline_add_lwline(LWMLINE *mobj, const LWLINE *obj)
Definition: lwmline.c:46
LWMLINE * lwmline_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwmline.c:38
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
Definition: lwmpoint.c:45
#define TINTYPE
Definition: liblwgeom.h:116
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:107
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
Definition: lwhomogenize.c:208
void lwfree(void *mem)
Definition: lwutil.c:248
#define POLYGONTYPE
Definition: liblwgeom.h:104
LWMPOINT * lwmpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwmpoint.c:39
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:109
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwcollection.c:92
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
#define MULTICURVETYPE
Definition: liblwgeom.h:112
#define TRIANGLETYPE
Definition: liblwgeom.h:115
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:189
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
LWPOINT * lwpoint_make(int32_t srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:206
int lwgeom_is_closed(const LWGEOM *geom)
Return true or false depending on whether a geometry is a linear feature that closes on itself.
Definition: lwgeom.c:1053
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:927
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:242
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep-clone an LWGEOM object.
Definition: lwgeom.c:529
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
LWGEOM * lwgeom_boundary(LWGEOM *lwgeom)
Definition: lwgeom.c:2686
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1218
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:941
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwgeom.c:2191
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:199
uint32_t ngeoms
Definition: liblwgeom.h:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
LWGEOM ** rings
Definition: liblwgeom.h:603
uint32_t nrings
Definition: liblwgeom.h:608
uint8_t type
Definition: liblwgeom.h:462
POINTARRAY * points
Definition: liblwgeom.h:483
LWLINE ** geoms
Definition: liblwgeom.h:547
uint32_t ngeoms
Definition: liblwgeom.h:552
uint32_t ngeoms
Definition: liblwgeom.h:538
LWPOINT ** geoms
Definition: liblwgeom.h:533
POINTARRAY * point
Definition: liblwgeom.h:471
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
POINTARRAY * points
Definition: liblwgeom.h:495
uint32_t npoints
Definition: liblwgeom.h:427

References CIRCSTRINGTYPE, COLLECTIONTYPE, CURVEPOLYTYPE, LWMPOINT::geoms, LWMLINE::geoms, LWCOLLECTION::geoms, getPoint4d(), getPoint4d_p(), LINETYPE, LW_FALSE, LW_TRUE, lwalloc(), lwcollection_add_lwgeom(), lwcollection_construct_empty(), lwerror(), lwfree(), lwgeom_as_lwmpoint(), lwgeom_boundary(), lwgeom_clone_deep(), lwgeom_construct_empty(), lwgeom_free(), lwgeom_get_srid(), lwgeom_has_m(), lwgeom_has_z(), lwgeom_homogenize(), lwgeom_is_closed(), lwgeom_is_empty(), lwline_construct(), lwmline_add_lwline(), lwmline_construct_empty(), lwmpoint_add_lwpoint(), lwmpoint_construct_empty(), lwpoint_make(), lwtype_name(), MULTICURVETYPE, MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, LWMPOINT::ngeoms, LWMLINE::ngeoms, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, LWCURVEPOLY::nrings, LWPOINT::point, LWLINE::points, LWTRIANGLE::points, POINTTYPE, POLYGONTYPE, ptarray_clone_deep(), LWPOLY::rings, LWCURVEPOLY::rings, TINTYPE, TRIANGLETYPE, and LWGEOM::type.

Referenced by boundary(), and lwgeom_boundary().

Here is the call graph for this function:
Here is the caller graph for this function: