PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 2835 of file lwgeom_functions_basic.c.

2836 {
2837  GSERIALIZED *in;
2838  GSERIALIZED *out;
2839  LWGEOM *lwgeom;
2840  const char *ospec;
2841  LWORD o1, o2;
2842 
2843  ospec = PG_GETARG_CSTRING(1);
2844  if ( strlen(ospec) != 2 )
2845  {
2846  lwpgerror("Invalid ordinate specification. "
2847  "Need two letters from the set (x,y,z,m). "
2848  "Got '%s'", ospec);
2849  PG_RETURN_NULL();
2850  }
2851  o1 = ordname2ordval( ospec[0] );
2852  o2 = ordname2ordval( ospec[1] );
2853 
2854  in = PG_GETARG_GSERIALIZED_P_COPY(0);
2855 
2856  /* Check presence of given ordinates */
2857  if ( ( o1 == LWORD_M || o2 == LWORD_M ) && ! gserialized_has_m(in) )
2858  {
2859  lwpgerror("Geometry does not have an M ordinate");
2860  PG_RETURN_NULL();
2861  }
2862  if ( ( o1 == LWORD_Z || o2 == LWORD_Z ) && ! gserialized_has_z(in) )
2863  {
2864  lwpgerror("Geometry does not have a Z ordinate");
2865  PG_RETURN_NULL();
2866  }
2867 
2868  /* Nothing to do if swapping the same ordinate, pity for the copy... */
2869  if ( o1 == o2 ) PG_RETURN_POINTER(in);
2870 
2871  lwgeom = lwgeom_from_gserialized(in);
2872  lwgeom_swap_ordinates(lwgeom, o1, o2);
2873  out = geometry_serialize(lwgeom);
2874  lwgeom_free(lwgeom);
2875  PG_FREE_IF_COPY(in, 0);
2876  PG_RETURN_POINTER(out);
2877 }
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:45
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
@ LWORD_Z
Definition: liblwgeom.h:116
@ LWORD_M
Definition: liblwgeom.h:117
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1510
enum LWORD_T LWORD
Ordinate names.
static LWORD ordname2ordval(char n)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)

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

Here is the call graph for this function: