PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 2850 of file lwgeom_functions_basic.c.

2851{
2852 GSERIALIZED *in;
2853 GSERIALIZED *out;
2854 LWGEOM *lwgeom;
2855 const char *ospec;
2856 LWORD o1, o2;
2857
2858 ospec = PG_GETARG_CSTRING(1);
2859 if (strlen(ospec) != 2)
2860 {
2861 lwpgerror(
2862 "Invalid ordinate specification. "
2863 "Need two letters from the set (x,y,z,m). "
2864 "Got '%s'",
2865 ospec);
2866 PG_RETURN_NULL();
2867 }
2868 o1 = ordname2ordval(ospec[0]);
2869 o2 = ordname2ordval(ospec[1]);
2870
2871 in = PG_GETARG_GSERIALIZED_P_COPY(0);
2872
2873 /* Check presence of given ordinates */
2874 if ((o1 == LWORD_M || o2 == LWORD_M) && !gserialized_has_m(in))
2875 {
2876 lwpgerror("Geometry does not have an M ordinate");
2877 PG_RETURN_NULL();
2878 }
2879 if ((o1 == LWORD_Z || o2 == LWORD_Z) && !gserialized_has_z(in))
2880 {
2881 lwpgerror("Geometry does not have a Z ordinate");
2882 PG_RETURN_NULL();
2883 }
2884
2885 /* Nothing to do if swapping the same ordinate, pity for the copy... */
2886 if (o1 == o2)
2887 PG_RETURN_POINTER(in);
2888
2889 lwgeom = lwgeom_from_gserialized(in);
2890 lwgeom_swap_ordinates(lwgeom, o1, o2);
2891 out = geometry_serialize(lwgeom);
2892 lwgeom_free(lwgeom);
2893 PG_FREE_IF_COPY(in, 0);
2894 PG_RETURN_POINTER(out);
2895}
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
@ 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)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)

References geometry_serialize(), 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: