PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 2819 of file lwgeom_functions_basic.c.

References geometry_serialize(), gserialized_has_m(), gserialized_has_z(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_swap_ordinates(), LWORD_M, LWORD_Z, ordname2ordval(), PG_FUNCTION_INFO_V1(), and ST_BoundingDiagonal().

Referenced by ordname2ordval().

2820 {
2821  GSERIALIZED *in;
2822  GSERIALIZED *out;
2823  LWGEOM *lwgeom;
2824  const char *ospec;
2825  LWORD o1, o2;
2826 
2827  ospec = PG_GETARG_CSTRING(1);
2828  if ( strlen(ospec) != 2 )
2829  {
2830  lwpgerror("Invalid ordinate specification. "
2831  "Need two letters from the set (x,y,z,m). "
2832  "Got '%s'", ospec);
2833  PG_RETURN_NULL();
2834  }
2835  o1 = ordname2ordval( ospec[0] );
2836  o2 = ordname2ordval( ospec[1] );
2837 
2838  in = PG_GETARG_GSERIALIZED_P_COPY(0);
2839 
2840  /* Check presence of given ordinates */
2841  if ( ( o1 == LWORD_M || o2 == LWORD_M ) && ! gserialized_has_m(in) )
2842  {
2843  lwpgerror("Geometry does not have an M ordinate");
2844  PG_RETURN_NULL();
2845  }
2846  if ( ( o1 == LWORD_Z || o2 == LWORD_Z ) && ! gserialized_has_z(in) )
2847  {
2848  lwpgerror("Geometry does not have a Z ordinate");
2849  PG_RETURN_NULL();
2850  }
2851 
2852  /* Nothing to do if swapping the same ordinate, pity for the copy... */
2853  if ( o1 == o2 ) PG_RETURN_POINTER(in);
2854 
2855  lwgeom = lwgeom_from_gserialized(in);
2856  lwgeom_swap_ordinates(lwgeom, o1, o2);
2857  out = geometry_serialize(lwgeom);
2858  lwgeom_free(lwgeom);
2859  PG_FREE_IF_COPY(in, 0);
2860  PG_RETURN_POINTER(out);
2861 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int gserialized_has_m(const GSERIALIZED *gser)
Check if a GSERIALIZED has an M ordinate.
Definition: g_serialized.c:50
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1510
int gserialized_has_z(const GSERIALIZED *gser)
Check if a GSERIALIZED has a Z ordinate.
Definition: g_serialized.c:45
static LWORD ordname2ordval(char n)
enum LWORD_T LWORD
Ordinate names.
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
Here is the call graph for this function:
Here is the caller graph for this function: