PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ lwgeom_boundary()

LWGEOM* lwgeom_boundary ( LWGEOM lwgeom)

Definition at line 2569 of file lwgeom.c.

2570 {
2571  int32_t srid = lwgeom_get_srid(lwgeom);
2572  uint8_t hasz = lwgeom_has_z(lwgeom);
2573  uint8_t hasm = lwgeom_has_m(lwgeom);
2574 
2575  switch (lwgeom->type)
2576  {
2577  case POINTTYPE:
2578  case MULTIPOINTTYPE: {
2579  return lwgeom_construct_empty(lwgeom->type, srid, hasz, hasm);
2580  }
2581  case LINETYPE:
2582  case CIRCSTRINGTYPE: {
2583  if (lwgeom_is_closed(lwgeom) || lwgeom_is_empty(lwgeom))
2584  return (LWGEOM *)lwmpoint_construct_empty(srid, hasz, hasm);
2585  else
2586  {
2587  LWLINE *lwline = (LWLINE *)lwgeom;
2588  LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
2589  POINT4D pt;
2590  getPoint4d_p(lwline->points, 0, &pt);
2591  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
2592  getPoint4d_p(lwline->points, lwline->points->npoints - 1, &pt);
2593  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
2594 
2595  return (LWGEOM *)lwmpoint;
2596  }
2597  }
2598  case MULTILINETYPE:
2599  case MULTICURVETYPE: {
2600  LWMLINE *lwmline = (LWMLINE *)lwgeom;
2601  POINT4D *out = lwalloc(sizeof(POINT4D) * lwmline->ngeoms * 2);
2602  uint32_t n = 0;
2603 
2604  for (uint32_t i = 0; i < lwmline->ngeoms; i++)
2605  {
2606  LWMPOINT *points = lwgeom_as_lwmpoint(lwgeom_boundary((LWGEOM *)lwmline->geoms[i]));
2607  if (!points)
2608  continue;
2609 
2610  for (uint32_t k = 0; k < points->ngeoms; k++)
2611  {
2612  POINT4D pt = getPoint4d(points->geoms[k]->point, 0);
2613 
2614  uint8_t seen = LW_FALSE;
2615  for (uint32_t j = 0; j < n; j++)
2616  {
2617  if (memcmp(&(out[j]), &pt, sizeof(POINT4D)) == 0)
2618  {
2619  seen = LW_TRUE;
2620  out[j] = out[--n];
2621  break;
2622  }
2623  }
2624  if (!seen)
2625  out[n++] = pt;
2626  }
2627 
2628  lwgeom_free((LWGEOM *)points);
2629  }
2630 
2631  LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
2632 
2633  for (uint32_t i = 0; i < n; i++)
2634  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &(out[i])));
2635 
2636  lwfree(out);
2637 
2638  return (LWGEOM *)lwmpoint;
2639  }
2640  case TRIANGLETYPE: {
2641  LWTRIANGLE *lwtriangle = (LWTRIANGLE *)lwgeom;
2642  POINTARRAY *points = ptarray_clone_deep(lwtriangle->points);
2643  return (LWGEOM *)lwline_construct(srid, 0, points);
2644  }
2645  case POLYGONTYPE: {
2646  LWPOLY *lwpoly = (LWPOLY *)lwgeom;
2647 
2648  LWMLINE *lwmline = lwmline_construct_empty(srid, hasz, hasm);
2649  for (uint32_t i = 0; i < lwpoly->nrings; i++)
2650  {
2651  POINTARRAY *ring = ptarray_clone_deep(lwpoly->rings[i]);
2652  lwmline_add_lwline(lwmline, lwline_construct(srid, 0, ring));
2653  }
2654 
2655  /* Homogenize the multilinestring to hopefully get a single LINESTRING */
2656  LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwmline);
2657  lwgeom_free((LWGEOM *)lwmline);
2658  return lwout;
2659  }
2660  case CURVEPOLYTYPE: {
2661  LWCURVEPOLY *lwcurvepoly = (LWCURVEPOLY *)lwgeom;
2662  LWCOLLECTION *lwcol = lwcollection_construct_empty(MULTICURVETYPE, srid, hasz, hasm);
2663 
2664  for (uint32_t i = 0; i < lwcurvepoly->nrings; i++)
2665  lwcol = lwcollection_add_lwgeom(lwcol, lwgeom_clone_deep(lwcurvepoly->rings[i]));
2666 
2667  return (LWGEOM *)lwcol;
2668  }
2669  case MULTIPOLYGONTYPE:
2670  case COLLECTIONTYPE:
2671  case TINTYPE: {
2672  LWCOLLECTION *lwcol = (LWCOLLECTION *)lwgeom;
2673  LWCOLLECTION *lwcol_boundary = lwcollection_construct_empty(COLLECTIONTYPE, srid, hasz, hasm);
2674 
2675  for (uint32_t i = 0; i < lwcol->ngeoms; i++)
2676  lwcollection_add_lwgeom(lwcol_boundary, lwgeom_boundary(lwcol->geoms[i]));
2677 
2678  LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwcol_boundary);
2679  lwgeom_free((LWGEOM *)lwcol_boundary);
2680 
2681  return lwout;
2682  }
2683  default:
2684  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(lwgeom->type));
2685  return NULL;
2686  }
2687 }
POINT4D getPoint4d(const POINTARRAY *pa, uint32_t n)
Definition: lwgeom_api.c:108
#define LW_FALSE
Definition: liblwgeom.h:108
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
#define CURVEPOLYTYPE
Definition: liblwgeom.h:125
#define MULTILINETYPE
Definition: liblwgeom.h:120
#define LINETYPE
Definition: liblwgeom.h:117
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:634
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:116
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:130
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
Definition: lwhomogenize.c:208
void lwfree(void *mem)
Definition: lwutil.c:242
#define POLYGONTYPE
Definition: liblwgeom.h:118
LWMPOINT * lwmpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwmpoint.c:39
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:123
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:126
#define MULTICURVETYPE
Definition: liblwgeom.h:126
#define TRIANGLETYPE
Definition: liblwgeom.h:129
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:188
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
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:1036
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:910
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:225
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep-clone an LWGEOM object.
Definition: lwgeom.c:512
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:917
LWGEOM * lwgeom_boundary(LWGEOM *lwgeom)
Definition: lwgeom.c:2569
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1138
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:924
LWGEOM * lwgeom_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
Definition: lwgeom.c:2111
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
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:203
uint32_t ngeoms
Definition: liblwgeom.h:594
LWGEOM ** geoms
Definition: liblwgeom.h:589
LWGEOM ** rings
Definition: liblwgeom.h:617
uint32_t nrings
Definition: liblwgeom.h:622
uint8_t type
Definition: liblwgeom.h:476
POINTARRAY * points
Definition: liblwgeom.h:497
LWLINE ** geoms
Definition: liblwgeom.h:561
uint32_t ngeoms
Definition: liblwgeom.h:566
uint32_t ngeoms
Definition: liblwgeom.h:552
LWPOINT ** geoms
Definition: liblwgeom.h:547
POINTARRAY * point
Definition: liblwgeom.h:485
POINTARRAY ** rings
Definition: liblwgeom.h:533
uint32_t nrings
Definition: liblwgeom.h:538
POINTARRAY * points
Definition: liblwgeom.h:509
uint32_t npoints
Definition: liblwgeom.h:441

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: