PostGIS  2.5.0beta1dev-r@@SVN_REVISION@@

◆ geography_project()

Datum geography_project ( PG_FUNCTION_ARGS  )

Definition at line 927 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().

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