PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lwgeom_boundary()

LWGEOM* lwgeom_boundary ( LWGEOM lwgeom)

Definition at line 2560 of file lwgeom.c.

2561 {
2562  int32_t srid = lwgeom_get_srid(lwgeom);
2563  uint8_t hasz = lwgeom_has_z(lwgeom);
2564  uint8_t hasm = lwgeom_has_m(lwgeom);
2565 
2566  switch (lwgeom->type)
2567  {
2568  case POINTTYPE:
2569  case MULTIPOINTTYPE: {
2570  return lwgeom_construct_empty(lwgeom->type, srid, hasz, hasm);
2571  }
2572  case LINETYPE:
2573  case CIRCSTRINGTYPE: {
2574  if (lwgeom_is_closed(lwgeom) || lwgeom_is_empty(lwgeom))
2575  return (LWGEOM *)lwmpoint_construct_empty(srid, hasz, hasm);
2576  else
2577  {
2578  LWLINE *lwline = (LWLINE *)lwgeom;
2579  LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
2580  POINT4D pt;
2581  getPoint4d_p(lwline->points, 0, &pt);
2582  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
2583  getPoint4d_p(lwline->points, lwline->points->npoints - 1, &pt);
2584  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &pt));
2585 
2586  return (LWGEOM *)lwmpoint;
2587  }
2588  }
2589  case MULTILINETYPE:
2590  case MULTICURVETYPE: {
2591  LWMLINE *lwmline = (LWMLINE *)lwgeom;
2592  POINT4D *out = lwalloc(sizeof(POINT4D) * lwmline->ngeoms * 2);
2593  uint32_t n = 0;
2594 
2595  for (uint32_t i = 0; i < lwmline->ngeoms; i++)
2596  {
2597  LWMPOINT *points = lwgeom_as_lwmpoint(lwgeom_boundary((LWGEOM *)lwmline->geoms[i]));
2598  if (!points)
2599  continue;
2600 
2601  for (uint32_t k = 0; k < points->ngeoms; k++)
2602  {
2603  POINT4D pt = getPoint4d(points->geoms[k]->point, 0);
2604 
2605  uint8_t seen = LW_FALSE;
2606  for (uint32_t j = 0; j < n; j++)
2607  {
2608  if (memcmp(&(out[j]), &pt, sizeof(POINT4D)) == 0)
2609  {
2610  seen = LW_TRUE;
2611  out[j] = out[--n];
2612  break;
2613  }
2614  }
2615  if (!seen)
2616  out[n++] = pt;
2617  }
2618 
2619  lwgeom_free((LWGEOM *)points);
2620  }
2621 
2622  LWMPOINT *lwmpoint = lwmpoint_construct_empty(srid, hasz, hasm);
2623 
2624  for (uint32_t i = 0; i < n; i++)
2625  lwmpoint_add_lwpoint(lwmpoint, lwpoint_make(srid, hasz, hasm, &(out[i])));
2626 
2627  lwfree(out);
2628 
2629  return (LWGEOM *)lwmpoint;
2630  }
2631  case TRIANGLETYPE: {
2632  LWTRIANGLE *lwtriangle = (LWTRIANGLE *)lwgeom;
2633  POINTARRAY *points = ptarray_clone_deep(lwtriangle->points);
2634  return (LWGEOM *)lwline_construct(srid, 0, points);
2635  }
2636  case POLYGONTYPE: {
2637  LWPOLY *lwpoly = (LWPOLY *)lwgeom;
2638 
2639  LWMLINE *lwmline = lwmline_construct_empty(srid, hasz, hasm);
2640  for (uint32_t i = 0; i < lwpoly->nrings; i++)
2641  {
2642  POINTARRAY *ring = ptarray_clone_deep(lwpoly->rings[i]);
2643  lwmline_add_lwline(lwmline, lwline_construct(srid, 0, ring));
2644  }
2645 
2646  /* Homogenize the multilinestring to hopefully get a single LINESTRING */
2647  LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwmline);
2648  lwgeom_free((LWGEOM *)lwmline);
2649  return lwout;
2650  }
2651  case CURVEPOLYTYPE: {
2652  LWCURVEPOLY *lwcurvepoly = (LWCURVEPOLY *)lwgeom;
2653  LWCOLLECTION *lwcol = lwcollection_construct_empty(MULTICURVETYPE, srid, hasz, hasm);
2654 
2655  for (uint32_t i = 0; i < lwcurvepoly->nrings; i++)
2656  lwcol = lwcollection_add_lwgeom(lwcol, lwgeom_clone_deep(lwcurvepoly->rings[i]));
2657 
2658  return (LWGEOM *)lwcol;
2659  }
2660  case MULTIPOLYGONTYPE:
2661  case COLLECTIONTYPE:
2662  case TINTYPE: {
2663  LWCOLLECTION *lwcol = (LWCOLLECTION *)lwgeom;
2664  LWCOLLECTION *lwcol_boundary = lwcollection_construct_empty(COLLECTIONTYPE, srid, hasz, hasm);
2665 
2666  for (uint32_t i = 0; i < lwcol->ngeoms; i++)
2667  lwcollection_add_lwgeom(lwcol_boundary, lwgeom_boundary(lwcol->geoms[i]));
2668 
2669  LWGEOM *lwout = lwgeom_homogenize((LWGEOM *)lwcol_boundary);
2670  lwgeom_free((LWGEOM *)lwcol_boundary);
2671 
2672  return lwout;
2673  }
2674  default:
2675  lwerror("%s: unsupported geometry type: %s", __func__, lwtype_name(lwgeom->type));
2676  return NULL;
2677  }
2678 }
POINT4D getPoint4d(const POINTARRAY *pa, uint32_t n)
Definition: lwgeom_api.c:108
#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:647
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:242
#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:188
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:2560
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1155
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:2105
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: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: