PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ polygon_to_geometry()

Datum polygon_to_geometry ( PG_FUNCTION_ARGS  )

Definition at line 247 of file geometry_inout.c.

248 {
249  POLYGON *polygon;
250  LWPOLY *lwpoly;
251  POINTARRAY *pa;
252  POINTARRAY **ppa;
253  GSERIALIZED *geom;
254  Point p;
255  int i = 0, unclosed = 0;
256 
257  POSTGIS_DEBUG(2, "polygon_to_geometry called");
258 
259  if ( PG_ARGISNULL(0) )
260  PG_RETURN_NULL();
261 
262  polygon = PG_GETARG_POLYGON_P(0);
263 
264  if ( ! polygon )
265  PG_RETURN_NULL();
266 
267  /* Are first and last points different? If so we need to close this ring */
268  if ( memcmp( polygon->p, polygon->p + polygon->npts - 1, sizeof(Point) ) )
269  {
270  unclosed = 1;
271  }
272 
273  pa = ptarray_construct_empty(0, 0, polygon->npts + unclosed);
274 
275  for ( i = 0; i < (polygon->npts+unclosed); i++ )
276  {
277  POINT4D pt;
278  p = polygon->p[i % polygon->npts];
279  pt.x = p.x;
280  pt.y = p.y;
281  ptarray_append_point(pa, &pt, LW_FALSE);
282  }
283 
284  ppa = palloc(sizeof(POINTARRAY*));
285  ppa[0] = pa;
286  lwpoly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, ppa);
287  geom = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
288  lwpoly_free(lwpoly);
289 
290  PG_RETURN_POINTER(geom);
291 }
#define LW_FALSE
Definition: liblwgeom.h:77
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:320
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
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:156
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:175
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
double x
Definition: liblwgeom.h:355
double y
Definition: liblwgeom.h:355

References geometry_serialize(), 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: