PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 3086 of file lwgeom_functions_basic.c.

3087 {
3088  GSERIALIZED *in;
3089  GSERIALIZED *out;
3090  LWGEOM *lwgeom;
3091  const char *ospec;
3092  LWORD o1, o2;
3093 
3094  ospec = PG_GETARG_CSTRING(1);
3095  if (strlen(ospec) != 2)
3096  {
3097  lwpgerror(
3098  "Invalid ordinate specification. "
3099  "Need two letters from the set (x,y,z,m). "
3100  "Got '%s'",
3101  ospec);
3102  PG_RETURN_NULL();
3103  }
3104  o1 = ordname2ordval(ospec[0]);
3105  o2 = ordname2ordval(ospec[1]);
3106 
3107  in = PG_GETARG_GSERIALIZED_P_COPY(0);
3108 
3109  /* Check presence of given ordinates */
3110  if ((o1 == LWORD_M || o2 == LWORD_M) && !gserialized_has_m(in))
3111  {
3112  lwpgerror("Geometry does not have an M ordinate");
3113  PG_RETURN_NULL();
3114  }
3115  if ((o1 == LWORD_Z || o2 == LWORD_Z) && !gserialized_has_z(in))
3116  {
3117  lwpgerror("Geometry does not have a Z ordinate");
3118  PG_RETURN_NULL();
3119  }
3120 
3121  /* Nothing to do if swapping the same ordinate, pity for the copy... */
3122  if (o1 == o2)
3123  PG_RETURN_POINTER(in);
3124 
3125  lwgeom = lwgeom_from_gserialized(in);
3126  lwgeom_swap_ordinates(lwgeom, o1, o2);
3127  out = geometry_serialize(lwgeom);
3128  lwgeom_free(lwgeom);
3129  PG_FREE_IF_COPY(in, 0);
3130  PG_RETURN_POINTER(out);
3131 }
int gserialized_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized.c:185
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:174
@ LWORD_Z
Definition: liblwgeom.h:133
@ LWORD_M
Definition: liblwgeom.h:134
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1478
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: