PostGIS  2.3.7dev-r@@SVN_REVISION@@
Datum geography_project ( PG_FUNCTION_ARGS  )

Definition at line 939 of file geography_measurement.c.

References distance(), FP_EQUALS, gserialized_get_srid(), gserialized_get_type(), lwgeom_as_lwpoint(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), lwgeom_project_spheroid(), lwpoint_as_lwgeom(), lwpoint_free(), POINTTYPE, s, and geomtype_struct::type.

940 {
941  LWGEOM *lwgeom = NULL;
942  LWPOINT *lwp_projected;
943  GSERIALIZED *g = NULL;
944  GSERIALIZED *g_out = NULL;
945  double azimuth = 0.0;
946  double distance;
947  SPHEROID s;
948  uint32_t type;
949 
950  /* Return NULL on NULL distance or geography */
951  if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
952  PG_RETURN_NULL();
953 
954  /* Get our geometry object loaded into memory. */
955  g = PG_GETARG_GSERIALIZED_P(0);
956 
957  /* Only return for points. */
958  type = gserialized_get_type(g);
959  if ( type != POINTTYPE )
960  {
961  elog(ERROR, "ST_Project(geography) is only valid for point inputs");
962  PG_RETURN_NULL();
963  }
964 
965  distance = PG_GETARG_FLOAT8(1); /* Distance in Meters */
966  lwgeom = lwgeom_from_gserialized(g);
967 
968  /* EMPTY things cannot be projected from */
969  if ( lwgeom_is_empty(lwgeom) )
970  {
971  lwgeom_free(lwgeom);
972  elog(ERROR, "ST_Project(geography) cannot project from an empty start point");
973  PG_RETURN_NULL();
974  }
975 
976  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
977  azimuth = PG_GETARG_FLOAT8(2); /* Azimuth in Radians */
978 
979  /* Initialize spheroid */
980  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
981 
982  /* Handle the zero distance case */
983  if( FP_EQUALS(distance, 0.0) )
984  {
985  PG_RETURN_POINTER(g);
986  }
987 
988  /* Calculate the length */
989  lwp_projected = lwgeom_project_spheroid(lwgeom_as_lwpoint(lwgeom), &s, distance, azimuth);
990 
991  /* Something went wrong... */
992  if ( lwp_projected == NULL )
993  {
994  elog(ERROR, "lwgeom_project_spheroid returned null");
995  PG_RETURN_NULL();
996  }
997 
998  /* Clean up, but not all the way to the point arrays */
999  lwgeom_free(lwgeom);
1000  g_out = geography_serialize(lwpoint_as_lwgeom(lwp_projected));
1001  lwpoint_free(lwp_projected);
1002 
1003  PG_FREE_IF_COPY(g, 0);
1004  PG_RETURN_POINTER(g_out);
1005 }
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:69
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:195
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
LWPOINT * lwgeom_project_spheroid(const LWPOINT *r, const SPHEROID *spheroid, double distance, double azimuth)
Calculate the location of a point on a spheroid, give a start point, bearing and distance.
Definition: lwgeodetic.c:2006
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:93
char * s
Definition: cu_in_wkt.c:23
Datum distance(PG_FUNCTION_ARGS)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
#define FP_EQUALS(A, B)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:267
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:1310
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:83

Here is the call graph for this function: