PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_azimuth()

Datum geography_azimuth ( PG_FUNCTION_ARGS  )

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

1011 {
1012  LWGEOM *lwgeom1 = NULL;
1013  LWGEOM *lwgeom2 = NULL;
1014  GSERIALIZED *g1 = NULL;
1015  GSERIALIZED *g2 = NULL;
1016  double azimuth;
1017  SPHEROID s;
1018  uint32_t type1, type2;
1019 
1020  /* Get our geometry object loaded into memory. */
1021  g1 = PG_GETARG_GSERIALIZED_P(0);
1022  g2 = PG_GETARG_GSERIALIZED_P(1);
1023 
1024  /* Only return for points. */
1025  type1 = gserialized_get_type(g1);
1026  type2 = gserialized_get_type(g2);
1027  if ( type1 != POINTTYPE || type2 != POINTTYPE )
1028  {
1029  elog(ERROR, "ST_Azimuth(geography, geography) is only valid for point inputs");
1030  PG_RETURN_NULL();
1031  }
1032 
1033  lwgeom1 = lwgeom_from_gserialized(g1);
1034  lwgeom2 = lwgeom_from_gserialized(g2);
1035 
1036  /* EMPTY things cannot be used */
1037  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
1038  {
1039  lwgeom_free(lwgeom1);
1040  lwgeom_free(lwgeom2);
1041  elog(ERROR, "ST_Azimuth(geography, geography) cannot work with empty points");
1042  PG_RETURN_NULL();
1043  }
1044 
1045  /* Initialize spheroid */
1046  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
1047 
1048  /* Calculate the direction */
1049  azimuth = lwgeom_azumith_spheroid(lwgeom_as_lwpoint(lwgeom1), lwgeom_as_lwpoint(lwgeom2), &s);
1050 
1051  /* Clean up */
1052  lwgeom_free(lwgeom1);
1053  lwgeom_free(lwgeom2);
1054 
1055  PG_FREE_IF_COPY(g1, 0);
1056  PG_FREE_IF_COPY(g2, 1);
1057 
1058  /* Return NULL for unknown (same point) azimuth */
1059  if( isnan(azimuth) )
1060  {
1061  PG_RETURN_NULL();
1062  }
1063 
1064  PG_RETURN_FLOAT8(azimuth);
1065 }
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:2152
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:129
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:85
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:1346
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:100
Here is the call graph for this function:
Here is the caller graph for this function: