PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 2969 of file lwgeom_functions_basic.c.

2970 {
2971  GSERIALIZED *in;
2972  GSERIALIZED *out;
2973  LWGEOM *lwgeom;
2974  const char *ospec;
2975  LWORD o1, o2;
2976 
2977  ospec = PG_GETARG_CSTRING(1);
2978  if (strlen(ospec) != 2)
2979  {
2980  lwpgerror(
2981  "Invalid ordinate specification. "
2982  "Need two letters from the set (x,y,z,m). "
2983  "Got '%s'",
2984  ospec);
2985  PG_RETURN_NULL();
2986  }
2987  o1 = ordname2ordval(ospec[0]);
2988  o2 = ordname2ordval(ospec[1]);
2989 
2990  in = PG_GETARG_GSERIALIZED_P_COPY(0);
2991 
2992  /* Check presence of given ordinates */
2993  if ((o1 == LWORD_M || o2 == LWORD_M) && !gserialized_has_m(in))
2994  {
2995  lwpgerror("Geometry does not have an M ordinate");
2996  PG_RETURN_NULL();
2997  }
2998  if ((o1 == LWORD_Z || o2 == LWORD_Z) && !gserialized_has_z(in))
2999  {
3000  lwpgerror("Geometry does not have a Z ordinate");
3001  PG_RETURN_NULL();
3002  }
3003 
3004  /* Nothing to do if swapping the same ordinate, pity for the copy... */
3005  if (o1 == o2)
3006  PG_RETURN_POINTER(in);
3007 
3008  lwgeom = lwgeom_from_gserialized(in);
3009  lwgeom_swap_ordinates(lwgeom, o1, o2);
3010  out = geometry_serialize(lwgeom);
3011  lwgeom_free(lwgeom);
3012  PG_FREE_IF_COPY(in, 0);
3013  PG_RETURN_POINTER(out);
3014 }
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:148
@ LWORD_M
Definition: liblwgeom.h:149
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: