PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ geography_azimuth()

Datum geography_azimuth ( PG_FUNCTION_ARGS  )

Definition at line 1037 of file geography_measurement.c.

1038 {
1039  LWGEOM *lwgeom1 = NULL;
1040  LWGEOM *lwgeom2 = NULL;
1041  GSERIALIZED *g1 = NULL;
1042  GSERIALIZED *g2 = NULL;
1043  double azimuth;
1044  SPHEROID s;
1045  uint32_t type1, type2;
1046 
1047  /* Get our geometry object loaded into memory. */
1048  g1 = PG_GETARG_GSERIALIZED_P(0);
1049  g2 = PG_GETARG_GSERIALIZED_P(1);
1050 
1051  /* Only return for points. */
1052  type1 = gserialized_get_type(g1);
1053  type2 = gserialized_get_type(g2);
1054  if ( type1 != POINTTYPE || type2 != POINTTYPE )
1055  {
1056  elog(ERROR, "ST_Azimuth(geography, geography) is only valid for point inputs");
1057  PG_RETURN_NULL();
1058  }
1059 
1060  lwgeom1 = lwgeom_from_gserialized(g1);
1061  lwgeom2 = lwgeom_from_gserialized(g2);
1062 
1063  /* EMPTY things cannot be used */
1064  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
1065  {
1066  lwgeom_free(lwgeom1);
1067  lwgeom_free(lwgeom2);
1068  elog(ERROR, "ST_Azimuth(geography, geography) cannot work with empty points");
1069  PG_RETURN_NULL();
1070  }
1071 
1072  /* Initialize spheroid */
1073  spheroid_init_from_srid(gserialized_get_srid(g1), &s);
1074 
1075  /* Calculate the direction */
1076  azimuth = lwgeom_azumith_spheroid(lwgeom_as_lwpoint(lwgeom1), lwgeom_as_lwpoint(lwgeom2), &s);
1077 
1078  /* Clean up */
1079  lwgeom_free(lwgeom1);
1080  lwgeom_free(lwgeom2);
1081 
1082  PG_FREE_IF_COPY(g1, 0);
1083  PG_FREE_IF_COPY(g2, 1);
1084 
1085  /* Return NULL for unknown (same point) azimuth */
1086  if( isnan(azimuth) )
1087  {
1088  PG_RETURN_NULL();
1089  }
1090 
1091  PG_RETURN_FLOAT8(azimuth);
1092 }
char * s
Definition: cu_in_wkt.c:23
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
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:2156
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131

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

Here is the call graph for this function: