PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 2857 of file lwgeom_functions_basic.c.

2858 {
2859  GSERIALIZED *in;
2860  GSERIALIZED *out;
2861  LWGEOM *lwgeom;
2862  const char *ospec;
2863  LWORD o1, o2;
2864 
2865  ospec = PG_GETARG_CSTRING(1);
2866  if (strlen(ospec) != 2)
2867  {
2868  lwpgerror(
2869  "Invalid ordinate specification. "
2870  "Need two letters from the set (x,y,z,m). "
2871  "Got '%s'",
2872  ospec);
2873  PG_RETURN_NULL();
2874  }
2875  o1 = ordname2ordval(ospec[0]);
2876  o2 = ordname2ordval(ospec[1]);
2877 
2878  in = PG_GETARG_GSERIALIZED_P_COPY(0);
2879 
2880  /* Check presence of given ordinates */
2881  if ((o1 == LWORD_M || o2 == LWORD_M) && !gserialized_has_m(in))
2882  {
2883  lwpgerror("Geometry does not have an M ordinate");
2884  PG_RETURN_NULL();
2885  }
2886  if ((o1 == LWORD_Z || o2 == LWORD_Z) && !gserialized_has_z(in))
2887  {
2888  lwpgerror("Geometry does not have a Z ordinate");
2889  PG_RETURN_NULL();
2890  }
2891 
2892  /* Nothing to do if swapping the same ordinate, pity for the copy... */
2893  if (o1 == o2)
2894  PG_RETURN_POINTER(in);
2895 
2896  lwgeom = lwgeom_from_gserialized(in);
2897  lwgeom_swap_ordinates(lwgeom, o1, o2);
2898  out = geometry_serialize(lwgeom);
2899  lwgeom_free(lwgeom);
2900  PG_FREE_IF_COPY(in, 0);
2901  PG_RETURN_POINTER(out);
2902 }
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:147
@ LWORD_M
Definition: liblwgeom.h:148
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1461
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: