PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ ST_SwapOrdinates()

Datum ST_SwapOrdinates ( PG_FUNCTION_ARGS  )

Definition at line 2699 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().

2700 {
2701  GSERIALIZED *in;
2702  GSERIALIZED *out;
2703  LWGEOM *lwgeom;
2704  const char *ospec;
2705  LWORD o1, o2;
2706 
2707  ospec = PG_GETARG_CSTRING(1);
2708  if ( strlen(ospec) != 2 )
2709  {
2710  lwpgerror("Invalid ordinate specification. "
2711  "Need two letters from the set (x,y,z,m). "
2712  "Got '%s'", ospec);
2713  PG_RETURN_NULL();
2714  }
2715  o1 = ordname2ordval( ospec[0] );
2716  o2 = ordname2ordval( ospec[1] );
2717 
2718  in = PG_GETARG_GSERIALIZED_P_COPY(0);
2719 
2720  /* Check presence of given ordinates */
2721  if ( ( o1 == LWORD_M || o2 == LWORD_M ) && ! gserialized_has_m(in) )
2722  {
2723  lwpgerror("Geometry does not have an M ordinate");
2724  PG_RETURN_NULL();
2725  }
2726  if ( ( o1 == LWORD_Z || o2 == LWORD_Z ) && ! gserialized_has_z(in) )
2727  {
2728  lwpgerror("Geometry does not have a Z ordinate");
2729  PG_RETURN_NULL();
2730  }
2731 
2732  /* Nothing to do if swapping the same ordinate, pity for the copy... */
2733  if ( o1 == o2 ) PG_RETURN_POINTER(in);
2734 
2735  lwgeom = lwgeom_from_gserialized(in);
2736  lwgeom_swap_ordinates(lwgeom, o1, o2);
2737  out = geometry_serialize(lwgeom);
2738  lwgeom_free(lwgeom);
2739  PG_FREE_IF_COPY(in, 0);
2740  PG_RETURN_POINTER(out);
2741 }
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:1099
void lwgeom_swap_ordinates(LWGEOM *in, LWORD o1, LWORD o2)
Swap ordinate values in every vertex of the geometry.
Definition: lwgeom.c:1513
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: