PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum polygon_to_geometry ( PG_FUNCTION_ARGS  )

Definition at line 224 of file geometry_inout.c.

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

225 {
226  POLYGON *polygon;
227  LWPOLY *lwpoly;
228  POINTARRAY *pa;
229  POINTARRAY **ppa;
230  GSERIALIZED *geom;
231  Point p;
232  int i = 0, unclosed = 0;
233 
234  POSTGIS_DEBUG(2, "polygon_to_geometry called");
235 
236  if ( PG_ARGISNULL(0) )
237  PG_RETURN_NULL();
238 
239  polygon = PG_GETARG_POLYGON_P(0);
240 
241  if ( ! polygon )
242  PG_RETURN_NULL();
243 
244  /* Are first and last points different? If so we need to close this ring */
245  if ( memcmp( polygon->p, polygon->p + polygon->npts - 1, sizeof(Point) ) )
246  {
247  unclosed = 1;
248  }
249 
250  pa = ptarray_construct_empty(0, 0, polygon->npts + unclosed);
251 
252  for ( i = 0; i < (polygon->npts+unclosed); i++ )
253  {
254  POINT4D pt;
255  p = polygon->p[i % polygon->npts];
256  pt.x = p.x;
257  pt.y = p.y;
258  ptarray_append_point(pa, &pt, LW_FALSE);
259  }
260 
261  ppa = palloc(sizeof(POINTARRAY*));
262  ppa[0] = pa;
263  lwpoly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, ppa);
264  geom = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
265  lwpoly_free(lwpoly);
266 
267  PG_RETURN_POINTER(geom);
268 }
double x
Definition: liblwgeom.h:308
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:57
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
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_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:141
#define LW_FALSE
Definition: liblwgeom.h:52
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:79
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
double y
Definition: liblwgeom.h:308

Here is the call graph for this function: