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

Definition at line 2816 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().

2817 {
2818  GSERIALIZED *in;
2819  GSERIALIZED *out;
2820  LWGEOM *lwgeom;
2821  const char *ospec;
2822  LWORD o1, o2;
2823 
2824  ospec = PG_GETARG_CSTRING(1);
2825  if ( strlen(ospec) != 2 )
2826  {
2827  lwpgerror("Invalid ordinate specification. "
2828  "Need two letters from the set (x,y,z,m). "
2829  "Got '%s'", ospec);
2830  PG_RETURN_NULL();
2831  }
2832  o1 = ordname2ordval( ospec[0] );
2833  o2 = ordname2ordval( ospec[1] );
2834 
2835  in = PG_GETARG_GSERIALIZED_P_COPY(0);
2836 
2837  /* Check presence of given ordinates */
2838  if ( ( o1 == LWORD_M || o2 == LWORD_M ) && ! gserialized_has_m(in) )
2839  {
2840  lwpgerror("Geometry does not have an M ordinate");
2841  PG_RETURN_NULL();
2842  }
2843  if ( ( o1 == LWORD_Z || o2 == LWORD_Z ) && ! gserialized_has_z(in) )
2844  {
2845  lwpgerror("Geometry does not have a Z ordinate");
2846  PG_RETURN_NULL();
2847  }
2848 
2849  /* Nothing to do if swapping the same ordinate, pity for the copy... */
2850  if ( o1 == o2 ) PG_RETURN_POINTER(in);
2851 
2852  lwgeom = lwgeom_from_gserialized(in);
2853  lwgeom_swap_ordinates(lwgeom, o1, o2);
2854  out = geometry_serialize(lwgeom);
2855  lwgeom_free(lwgeom);
2856  PG_FREE_IF_COPY(in, 0);
2857  PG_RETURN_POINTER(out);
2858 }
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:50
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1503
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:45
static LWORD ordname2ordval(char n)
enum LWORD_T LWORD
Ordinate names.
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)

Here is the call graph for this function: