PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ lwgeom_boundary()

LWGEOM* lwgeom_boundary ( LWGEOM lwgeom)

Definition at line 2714 of file lwgeom.c.

2715 {
2716  int32_t srid = lwgeom_get_srid(lwgeom);
2717  uint8_t hasz = lwgeom_has_z(lwgeom);
2718  uint8_t hasm = lwgeom_has_m(lwgeom);
2719 
2720  switch (lwgeom->type)
2721  {
2722  case POINTTYPE:
2723  case MULTIPOINTTYPE: {
2724  return lwgeom_construct_empty(lwgeom->type, srid, hasz, hasm);
2725  }
2726  case LINETYPE:
2727  case CIRCSTRINGTYPE: {
2728  if (lwgeom_is_closed(lwgeom) || lwgeom_is_empty(lwgeom))
2729  return (LWGEOM *)lwmpoint_construct_empty(srid, hasz, hasm);
2730  else
2731  {
2732  LWLINE *lwline = (LWLINE *)lwgeom;
2733  LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
2734  POINT4D pt;
2735  getPoint4d_p(lwline->points, 0, &pt);
2736  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
2737  getPoint4d_p(lwline->points, lwline->points->npoints - 1, &pt);
2738  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
2739 
2740  return (LWGEOM *)lwmpoint;
2741  }
2742  }
2743  case MULTILINETYPE:
2744  case MULTICURVETYPE: {
2745  LWMLINE *lwmline = (LWMLINE *)lwgeom;
2746  POINT4D *out = lwalloc(sizeof(POINT4D) * lwmline->ngeoms * 2);
2747  uint32_t n = 0;
2748 
2749  for (uint32_t i = 0; i < lwmline->ngeoms; i++)
2750  {
2751  LWMPOINT *points = lwgeom_as_lwmpoint(lwgeom_boundary((LWGEOM *)lwmline->geoms[i]));
2752  if (!points)
2753  continue;
2754 
2755  for (uint32_t k = 0; k < points->ngeoms; k++)
2756  {
2757  POINT4D pt = getPoint4d(points->geoms[k]->point, 0);
2758 
2759  uint8_t seen = LW_FALSE;
2760  for (uint32_t j = 0; j < n; j++)
2761  {
2762  if (memcmp(&(out[j]), &pt, sizeof(POINT4D)) == 0)
2763  {
2764  seen = LW_TRUE;
2765  out[j] = out[--n];
2766  break;
2767  }
2768  }
2769  if (!seen)
2770  out[n++] = pt;
2771  }
2772 
2773  lwgeom_free((LWGEOM *)points);
2774  }
2775 
2776  LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
2777 
2778  for (uint32_t i = 0; i < n; i++)
2779  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &(out[i])));
2780 
2781  lwfree(out);
2782 
2783  return (LWGEOM *)lwmpoint;
2784  }
2785  case TRIANGLETYPE: {
2786  LWTRIANGLE *lwtriangle = (LWTRIANGLE *)lwgeom;
2787  POINTARRAY *points = ptarray_clone_deep(lwtriangle->points);
2788  return (LWGEOM *)lwline_construct(srid, 0, points);
2789  }
2790  case POLYGONTYPE: {
2791  LWPOLY *lwpoly = (LWPOLY *)lwgeom;
2792 
2793  LWMLINE *lwmline = lwmline_construct_empty(srid, hasz, hasm);
2794  for (uint32_t i = 0; i < lwpoly->nrings; i++)
2795  {
2796  POINTARRAY *ring = ptarray_clone_deep(lwpoly->rings[i]);
2797  lwmline_add_lwline(lwmline, lwline_construct(srid, 0, ring));
2798  }
2799 
2800  /* Homogenize the multilinestring to hopefully get a single LINESTRING */
2801  LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwmline);
2802  lwgeom_free((LWGEOM *)lwmline);
2803  return lwout;
2804  }
2805  case CURVEPOLYTYPE: {
2806  LWCURVEPOLY *lwcurvepoly = (LWCURVEPOLY *)lwgeom;
2807  LWCOLLECTION *lwcol = lwcollection_construct_empty(MULTICURVETYPE, srid, hasz, hasm);
2808 
2809  for (uint32_t i = 0; i < lwcurvepoly->nrings; i++)
2810  lwcol = lwcollection_add_lwgeom(lwcol, lwgeom_clone_deep(lwcurvepoly->rings[i]));
2811 
2812  return (LWGEOM *)lwcol;
2813  }
2814  case MULTIPOLYGONTYPE:
2815  case COLLECTIONTYPE:
2816  case TINTYPE: {
2817  LWCOLLECTION *lwcol = (LWCOLLECTION *)lwgeom;
2818  LWCOLLECTION *lwcol_boundary = lwcollection_construct_empty(COLLECTIONTYPE, srid, hasz, hasm);
2819 
2820  for (uint32_t i = 0; i < lwcol->ngeoms; i++)
2821  lwcollection_add_lwgeom(lwcol_boundary, lwgeom_boundary(lwcol->geoms[i]));
2822 
2823  LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwcol_boundary);
2824  lwgeom_free((LWGEOM *)lwcol_boundary);
2825 
2826  return lwout;
2827  }
2828  default:
2829  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(lwgeom->type));
2830  return NULL;
2831  }
2832 }
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:1081
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:955
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:270
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep-clone an LWGEOM object.
Definition: lwgeom.c:557
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:962
LWGEOM * lwgeom_boundary(LWGEOM *lwgeom)
Definition: lwgeom.c:2714
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1246
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:969
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwgeom.c:2219
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: