PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ polygon_to_geometry()

Datum polygon_to_geometry ( PG_FUNCTION_ARGS  )

Definition at line 223 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.

Referenced by geometry_to_polygon().

224 {
225  POLYGON *polygon;
226  LWPOLY *lwpoly;
227  POINTARRAY *pa;
228  POINTARRAY **ppa;
229  GSERIALIZED *geom;
230  Point p;
231  int i = 0, unclosed = 0;
232 
233  POSTGIS_DEBUG(2, "polygon_to_geometry called");
234 
235  if ( PG_ARGISNULL(0) )
236  PG_RETURN_NULL();
237 
238  polygon = PG_GETARG_POLYGON_P(0);
239 
240  if ( ! polygon )
241  PG_RETURN_NULL();
242 
243  /* Are first and last points different? If so we need to close this ring */
244  if ( memcmp( polygon->p, polygon->p + polygon->npts - 1, sizeof(Point) ) )
245  {
246  unclosed = 1;
247  }
248 
249  pa = ptarray_construct_empty(0, 0, polygon->npts + unclosed);
250 
251  for ( i = 0; i < (polygon->npts+unclosed); i++ )
252  {
253  POINT4D pt;
254  p = polygon->p[i % polygon->npts];
255  pt.x = p.x;
256  pt.y = p.y;
257  ptarray_append_point(pa, &pt, LW_FALSE);
258  }
259 
260  ppa = palloc(sizeof(POINTARRAY*));
261  ppa[0] = pa;
262  lwpoly = lwpoly_construct(SRID_UNKNOWN, NULL, 1, ppa);
263  geom = geometry_serialize(lwpoly_as_lwgeom(lwpoly));
264  lwpoly_free(lwpoly);
265 
266  PG_RETURN_POINTER(geom);
267 }
double x
Definition: liblwgeom.h:336
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
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:156
#define LW_FALSE
Definition: liblwgeom.h:62
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:172
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
double y
Definition: liblwgeom.h:336
Here is the call graph for this function:
Here is the caller graph for this function: