PostGIS  2.3.7dev-r@@SVN_REVISION@@
Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 2716 of file lwgeom_functions_basic.c.

References geometry_serialize(), gserialized_has_m(), gserialized_has_z(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_swap_ordinates(), LWORD_M, LWORD_Z, and ordname2ordval().

2717 {
2718  GSERIALIZED *in;
2719  GSERIALIZED *out;
2720  LWGEOM *lwgeom;
2721  const char *ospec;
2722  LWORD o1, o2;
2723 
2724  ospec = PG_GETARG_CSTRING(1);
2725  if ( strlen(ospec) != 2 )
2726  {
2727  lwpgerror("Invalid ordinate specification. "
2728  "Need two letters from the set (x,y,z,m). "
2729  "Got '%s'", ospec);
2730  PG_RETURN_NULL();
2731  }
2732  o1 = ordname2ordval( ospec[0] );
2733  o2 = ordname2ordval( ospec[1] );
2734 
2735  in = PG_GETARG_GSERIALIZED_P_COPY(0);
2736 
2737  /* Check presence of given ordinates */
2738  if ( ( o1 == LWORD_M || o2 == LWORD_M ) && ! gserialized_has_m(in) )
2739  {
2740  lwpgerror("Geometry does not have an M ordinate");
2741  PG_RETURN_NULL();
2742  }
2743  if ( ( o1 == LWORD_Z || o2 == LWORD_Z ) && ! gserialized_has_z(in) )
2744  {
2745  lwpgerror("Geometry does not have a Z ordinate");
2746  PG_RETURN_NULL();
2747  }
2748 
2749  /* Nothing to do if swapping the same ordinate, pity for the copy... */
2750  if ( o1 == o2 ) PG_RETURN_POINTER(in);
2751 
2752  lwgeom = lwgeom_from_gserialized(in);
2753  lwgeom_swap_ordinates(lwgeom, o1, o2);
2754  out = geometry_serialize(lwgeom);
2755  lwgeom_free(lwgeom);
2756  PG_FREE_IF_COPY(in, 0);
2757  PG_RETURN_POINTER(out);
2758 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
Definition: g_serialized.c:43
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1477
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:38
static LWORD ordname2ordval(char n)
enum LWORD_T LWORD
Ordinate names.
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)

Here is the call graph for this function: