PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum geography_azimuth ( PG_FUNCTION_ARGS  )

Definition at line 904 of file geography_measurement.c.

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

905 {
906  LWGEOM *lwgeom1 = NULL;
907  LWGEOM *lwgeom2 = NULL;
908  GSERIALIZED *g1 = NULL;
909  GSERIALIZED *g2 = NULL;
910  double azimuth;
911  SPHEROID s;
912  uint32_t type1, type2;
913 
914  /* Get our geometry object loaded into memory. */
915  g1 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
916  g2 = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
917 
918  /* Only return for points. */
919  type1 = gserialized_get_type(g1);
920  type2 = gserialized_get_type(g2);
921  if ( type1 != POINTTYPE || type2 != POINTTYPE )
922  {
923  elog(ERROR, "ST_Azimuth(geography, geography) is only valid for point inputs");
924  PG_RETURN_NULL();
925  }
926 
927  lwgeom1 = lwgeom_from_gserialized(g1);
928  lwgeom2 = lwgeom_from_gserialized(g2);
929 
930  /* EMPTY things cannot be used */
931  if ( lwgeom_is_empty(lwgeom1) || lwgeom_is_empty(lwgeom2) )
932  {
933  lwgeom_free(lwgeom1);
934  lwgeom_free(lwgeom2);
935  elog(ERROR, "ST_Azimuth(geography, geography) cannot work with empty points");
936  PG_RETURN_NULL();
937  }
938 
939  /* Initialize spheroid */
940  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g1), &s);
941 
942  /* Calculate the direction */
943  azimuth = lwgeom_azumith_spheroid(lwgeom_as_lwpoint(lwgeom1), lwgeom_as_lwpoint(lwgeom2), &s);
944 
945  /* Clean up */
946  lwgeom_free(lwgeom1);
947  lwgeom_free(lwgeom2);
948 
949  PG_FREE_IF_COPY(g1, 0);
950  PG_FREE_IF_COPY(g2, 1);
951 
952  /* Return NULL for unknown (same point) azimuth */
953  if( isnan(azimuth) )
954  {
955  PG_RETURN_NULL();
956  }
957 
958  PG_RETURN_FLOAT8(azimuth);
959 }
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:56
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:2047
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:80
char * s
Definition: cu_in_wkt.c:24
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60
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:1229
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:70

Here is the call graph for this function: