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

Definition at line 828 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.

829 {
830  LWGEOM *lwgeom = NULL;
831  LWPOINT *lwp_projected;
832  GSERIALIZED *g = NULL;
833  GSERIALIZED *g_out = NULL;
834  double azimuth, distance;
835  SPHEROID s;
836  uint32_t type;
837 
838  /* Return NULL on NULL distance or geography */
839  if ( PG_ARGISNULL(0) || PG_ARGISNULL(1) )
840  PG_RETURN_NULL();
841 
842  /* Get our geometry object loaded into memory. */
843  g = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
844 
845  /* Only return for points. */
846  type = gserialized_get_type(g);
847  if ( type != POINTTYPE )
848  {
849  elog(ERROR, "ST_Project(geography) is only valid for point inputs");
850  PG_RETURN_NULL();
851  }
852 
853  distance = PG_GETARG_FLOAT8(1); /* Distance in Meters */
854  lwgeom = lwgeom_from_gserialized(g);
855 
856  /* EMPTY things cannot be projected from */
857  if ( lwgeom_is_empty(lwgeom) )
858  {
859  lwgeom_free(lwgeom);
860  elog(ERROR, "ST_Project(geography) cannot project from an empty start point");
861  PG_RETURN_NULL();
862  }
863 
864  if ( PG_ARGISNULL(2) )
865  azimuth = 0.0;
866  else
867  azimuth = PG_GETARG_FLOAT8(2); /* Azimuth in Radians */
868 
869  /* Initialize spheroid */
870  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
871 
872  /* Handle the zero distance case */
873  if( FP_EQUALS(distance, 0.0) )
874  {
875  PG_RETURN_POINTER(g);
876  }
877 
878  /* Calculate the length */
879  lwp_projected = lwgeom_project_spheroid(lwgeom_as_lwpoint(lwgeom), &s, distance, azimuth);
880 
881  /* Something went wrong... */
882  if ( lwp_projected == NULL )
883  {
884  elog(ERROR, "lwgeom_project_spheroid returned null");
885  PG_RETURN_NULL();
886  }
887 
888  /* Clean up, but not all the way to the point arrays */
889  lwgeom_free(lwgeom);
890  g_out = geography_serialize(lwpoint_as_lwgeom(lwp_projected));
891  lwpoint_free(lwp_projected);
892 
893  PG_FREE_IF_COPY(g, 0);
894  PG_RETURN_POINTER(g_out);
895 }
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
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:180
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
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:1992
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:80
char * s
Definition: cu_in_wkt.c:24
Datum distance(PG_FUNCTION_ARGS)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60
#define FP_EQUALS(A, B)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
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: