PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ geography_project()

Datum geography_project ( PG_FUNCTION_ARGS  )

Definition at line 922 of file geography_measurement.c.

References distance(), FP_EQUALS, geography_azimuth(), 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(), PG_FUNCTION_INFO_V1(), POINTTYPE, s, and ovdump::type.

Referenced by geography_bestsrid().

923 {
924  LWGEOM *lwgeom = NULL;
925  LWPOINT *lwp_projected;
926  GSERIALIZED *g = NULL;
927  GSERIALIZED *g_out = NULL;
928  double azimuth = 0.0;
929  double distance;
930  SPHEROID s;
931  uint32_t type;
932 
933  /* Return NULL on NULL distance or geography */
934  if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
935  PG_RETURN_NULL();
936 
937  /* Get our geometry object loaded into memory. */
938  g = PG_GETARG_GSERIALIZED_P(0);
939 
940  /* Only return for points. */
941  type = gserialized_get_type(g);
942  if ( type != POINTTYPE )
943  {
944  elog(ERROR, "ST_Project(geography) is only valid for point inputs");
945  PG_RETURN_NULL();
946  }
947 
948  distance = PG_GETARG_FLOAT8(1); /* Distance in Meters */
949  lwgeom = lwgeom_from_gserialized(g);
950 
951  /* EMPTY things cannot be projected from */
952  if ( lwgeom_is_empty(lwgeom) )
953  {
954  lwgeom_free(lwgeom);
955  elog(ERROR, "ST_Project(geography) cannot project from an empty start point");
956  PG_RETURN_NULL();
957  }
958 
959  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
960  azimuth = PG_GETARG_FLOAT8(2); /* Azimuth in Radians */
961 
962  /* Initialize spheroid */
963  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
964 
965  /* Handle the zero distance case */
966  if( FP_EQUALS(distance, 0.0) )
967  {
968  PG_RETURN_POINTER(g);
969  }
970 
971  /* Calculate the length */
972  lwp_projected = lwgeom_project_spheroid(lwgeom_as_lwpoint(lwgeom), &s, distance, azimuth);
973 
974  /* Something went wrong... */
975  if ( lwp_projected == NULL )
976  {
977  elog(ERROR, "lwgeom_project_spheroid returned null");
978  PG_RETURN_NULL();
979  }
980 
981  /* Clean up, but not all the way to the point arrays */
982  lwgeom_free(lwgeom);
983  g_out = geography_serialize(lwpoint_as_lwgeom(lwp_projected));
984  lwpoint_free(lwp_projected);
985 
986  PG_FREE_IF_COPY(g, 0);
987  PG_RETURN_POINTER(g_out);
988 }
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:55
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:182
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
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:1995
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:80
char * s
Definition: cu_in_wkt.c:23
Datum distance(PG_FUNCTION_ARGS)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
type
Definition: ovdump.py:41
#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:1297
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:69
Here is the call graph for this function:
Here is the caller graph for this function: