PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ geography_azimuth()

Datum geography_azimuth ( PG_FUNCTION_ARGS  )

Definition at line 1002 of file geography_measurement.c.

References geography_segmentize(), gserialized_get_srid(), gserialized_get_type(), lwgeom_as_lwpoint(), lwgeom_azumith_spheroid(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), PG_FUNCTION_INFO_V1(), POINTTYPE, and s.

Referenced by geography_project().

1003 {
1004  LWGEOM *lwgeom1 = NULL;
1005  LWGEOM *lwgeom2 = NULL;
1006  GSERIALIZED *g1 = NULL;
1007  GSERIALIZED *g2 = NULL;
1008  double azimuth;
1009  SPHEROID s;
1010  uint32_t type1, type2;
1011 
1012  /* Get our geometry object loaded into memory. */
1013  g1 = PG_GETARG_GSERIALIZED_P(0);
1014  g2 = PG_GETARG_GSERIALIZED_P(1);
1015 
1016  /* Only return for points. */
1017  type1 = gserialized_get_type(g1);
1018  type2 = gserialized_get_type(g2);
1019  if ( type1 != POINTTYPE || type2 != POINTTYPE )
1020  {
1021  elog(ERROR, "ST_Azimuth(geography, geography) is only valid for point inputs");
1022  PG_RETURN_NULL();
1023  }
1024 
1025  lwgeom1 = lwgeom_from_gserialized(g1);
1026  lwgeom2 = lwgeom_from_gserialized(g2);
1027 
1028  /* EMPTY things cannot be used */
1029  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
1030  {
1031  lwgeom_free(lwgeom1);
1032  lwgeom_free(lwgeom2);
1033  elog(ERROR, "ST_Azimuth(geography, geography) cannot work with empty points");
1034  PG_RETURN_NULL();
1035  }
1036 
1037  /* Initialize spheroid */
1038  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
1039 
1040  /* Calculate the direction */
1041  azimuth = lwgeom_azumith_spheroid(lwgeom_as_lwpoint(lwgeom1), lwgeom_as_lwpoint(lwgeom2), &s);
1042 
1043  /* Clean up */
1044  lwgeom_free(lwgeom1);
1045  lwgeom_free(lwgeom2);
1046 
1047  PG_FREE_IF_COPY(g1, 0);
1048  PG_FREE_IF_COPY(g2, 1);
1049 
1050  /* Return NULL for unknown (same point) azimuth */
1051  if( isnan(azimuth) )
1052  {
1053  PG_RETURN_NULL();
1054  }
1055 
1056  PG_RETURN_FLOAT8(azimuth);
1057 }
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
double lwgeom_azumith_spheroid(const LWPOINT *r, const LWPOINT *s, const SPHEROID *spheroid)
Calculate the bearing between two points on a spheroid.
Definition: lwgeodetic.c:2069
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
unsigned int uint32_t
Definition: uthash.h:78
char * s
Definition: cu_in_wkt.c:23
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1393
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:99
Here is the call graph for this function:
Here is the caller graph for this function: