PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 3051 of file lwgeom_functions_basic.c.

3052 {
3053  GSERIALIZED *in;
3054  GSERIALIZED *out;
3055  LWGEOM *lwgeom;
3056  const char *ospec;
3057  LWORD o1, o2;
3058 
3059  ospec = PG_GETARG_CSTRING(1);
3060  if (strlen(ospec) != 2)
3061  {
3062  lwpgerror(
3063  "Invalid ordinate specification. "
3064  "Need two letters from the set (x,y,z,m). "
3065  "Got '%s'",
3066  ospec);
3067  PG_RETURN_NULL();
3068  }
3069  o1 = ordname2ordval(ospec[0]);
3070  o2 = ordname2ordval(ospec[1]);
3071 
3072  in = PG_GETARG_GSERIALIZED_P_COPY(0);
3073 
3074  /* Check presence of given ordinates */
3075  if ((o1 == LWORD_M || o2 == LWORD_M) && !gserialized_has_m(in))
3076  {
3077  lwpgerror("Geometry does not have an M ordinate");
3078  PG_RETURN_NULL();
3079  }
3080  if ((o1 == LWORD_Z || o2 == LWORD_Z) && !gserialized_has_z(in))
3081  {
3082  lwpgerror("Geometry does not have a Z ordinate");
3083  PG_RETURN_NULL();
3084  }
3085 
3086  /* Nothing to do if swapping the same ordinate, pity for the copy... */
3087  if (o1 == o2)
3088  PG_RETURN_POINTER(in);
3089 
3090  lwgeom = lwgeom_from_gserialized(in);
3091  lwgeom_swap_ordinates(lwgeom, o1, o2);
3092  out = geometry_serialize(lwgeom);
3093  lwgeom_free(lwgeom);
3094  PG_FREE_IF_COPY(in, 0);
3095  PG_RETURN_POINTER(out);
3096 }
int gserialized_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized.c:214
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:268
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:203
@ LWORD_Z
Definition: liblwgeom.h:133
@ LWORD_M
Definition: liblwgeom.h:134
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1218
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1541
enum LWORD_T LWORD
Ordinate names.
static LWORD ordname2ordval(char n)

References 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: