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

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

2655 {
2656  GSERIALIZED *in;
2657  GSERIALIZED *out;
2658  LWGEOM *lwgeom;
2659  const char *ospec;
2660  LWORD o1, o2;
2661 
2662  ospec = PG_GETARG_CSTRING(1);
2663  if ( strlen(ospec) != 2 )
2664  {
2665  lwpgerror("Invalid ordinate specification. "
2666  "Need two letters from the set (x,y,z,m). "
2667  "Got '%s'", ospec);
2668  PG_RETURN_NULL();
2669  }
2670  o1 = ordname2ordval( ospec[0] );
2671  o2 = ordname2ordval( ospec[1] );
2672 
2673  in = PG_GETARG_GSERIALIZED_P_COPY(0);
2674 
2675  /* Check presence of given ordinates */
2676  if ( ( o1 == LWORD_M || o2 == LWORD_M ) && ! gserialized_has_m(in) )
2677  {
2678  lwpgerror("Geometry does not have an M ordinate");
2679  PG_RETURN_NULL();
2680  }
2681  if ( ( o1 == LWORD_Z || o2 == LWORD_Z ) && ! gserialized_has_z(in) )
2682  {
2683  lwpgerror("Geometry does not have a Z ordinate");
2684  PG_RETURN_NULL();
2685  }
2686 
2687  /* Nothing to do if swapping the same ordinate, pity for the copy... */
2688  if ( o1 == o2 ) PG_RETURN_POINTER(in);
2689 
2690  lwgeom = lwgeom_from_gserialized(in);
2691  lwgeom_swap_ordinates(lwgeom, o1, o2);
2692  out = geometry_serialize(lwgeom);
2693  lwgeom_free(lwgeom);
2694  PG_FREE_IF_COPY(in, 0);
2695  PG_RETURN_POINTER(out);
2696 }
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:29
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1464
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:24
static LWORD ordname2ordval(char n)
enum LWORD_T LWORD
Ordinate names.
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)

Here is the call graph for this function: