PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ polygon_to_geometry()

Datum polygon_to_geometry ( PG_FUNCTION_ARGS  )

Definition at line 236 of file geometry_inout.c.

237 {
238  POLYGON *polygon;
239  LWPOLY *lwpoly;
240  POINTARRAY *pa;
241  POINTARRAY **ppa;
242  GSERIALIZED *geom;
243  Point p;
244  int i = 0, unclosed = 0;
245 
246  POSTGIS_DEBUG(2, "polygon_to_geometry called");
247 
248  if ( PG_ARGISNULL(0) )
249  PG_RETURN_NULL();
250 
251  polygon = PG_GETARG_POLYGON_P(0);
252 
253  if ( ! polygon )
254  PG_RETURN_NULL();
255 
256  /* Are first and last points different? If so we need to close this ring */
257  if ( memcmp( polygon->p, polygon->p + polygon->npts - 1, sizeof(Point) ) )
258  {
259  unclosed = 1;
260  }
261 
262  pa = ptarray_construct_empty(0, 0, polygon->npts + unclosed);
263 
264  for ( i = 0; i < (polygon->npts+unclosed); i++ )
265  {
266  POINT4D pt;
267  p = polygon->p[i % polygon->npts];
268  pt.x = p.x;
269  pt.y = p.y;
270  ptarray_append_point(pa, &pt, LW_FALSE);
271  }
272 
273  ppa = palloc(sizeof(POINTARRAY*));
274  ppa[0] = pa;
275  lwpoly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, ppa);
276  geom = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
277  lwpoly_free(lwpoly);
278 
279  PG_RETURN_POINTER(geom);
280 }
#define LW_FALSE
Definition: liblwgeom.h:94
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:329
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE,...
Definition: ptarray.c:147
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:175
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
double x
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414

References LW_FALSE, lwpoly_as_lwgeom(), lwpoly_construct(), lwpoly_free(), ptarray_append_point(), ptarray_construct_empty(), SRID_UNKNOWN, POINT4D::x, struct_point::x, POINT4D::y, and struct_point::y.

Here is the call graph for this function: