PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 3078 of file lwgeom_functions_basic.c.

3079 {
3080  GSERIALIZED *in;
3081  GSERIALIZED *out;
3082  LWGEOM *lwgeom;
3083  const char *ospec;
3084  LWORD o1, o2;
3085 
3086  ospec = PG_GETARG_CSTRING(1);
3087  if (strlen(ospec) != 2)
3088  {
3089  lwpgerror(
3090  "Invalid ordinate specification. "
3091  "Need two letters from the set (x,y,z,m). "
3092  "Got '%s'",
3093  ospec);
3094  PG_RETURN_NULL();
3095  }
3096  o1 = ordname2ordval(ospec[0]);
3097  o2 = ordname2ordval(ospec[1]);
3098 
3099  in = PG_GETARG_GSERIALIZED_P_COPY(0);
3100 
3101  /* Check presence of given ordinates */
3102  if ((o1 == LWORD_M || o2 == LWORD_M) && !gserialized_has_m(in))
3103  {
3104  lwpgerror("Geometry does not have an M ordinate");
3105  PG_RETURN_NULL();
3106  }
3107  if ((o1 == LWORD_Z || o2 == LWORD_Z) && !gserialized_has_z(in))
3108  {
3109  lwpgerror("Geometry does not have a Z ordinate");
3110  PG_RETURN_NULL();
3111  }
3112 
3113  /* Nothing to do if swapping the same ordinate, pity for the copy... */
3114  if (o1 == o2)
3115  PG_RETURN_POINTER(in);
3116 
3117  lwgeom = lwgeom_from_gserialized(in);
3118  lwgeom_swap_ordinates(lwgeom, o1, o2);
3119  out = geometry_serialize(lwgeom);
3120  lwgeom_free(lwgeom);
3121  PG_FREE_IF_COPY(in, 0);
3122  PG_RETURN_POINTER(out);
3123 }
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:1246
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1569
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: