PostGIS  3.0.0dev-r@@SVN_REVISION@@

◆ geography_project()

Datum geography_project ( PG_FUNCTION_ARGS  )

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

931 {
932  LWGEOM *lwgeom = NULL;
933  LWPOINT *lwp_projected;
934  GSERIALIZED *g = NULL;
935  GSERIALIZED *g_out = NULL;
936  double azimuth = 0.0;
937  double distance;
938  SPHEROID s;
939  uint32_t type;
940 
941  /* Return NULL on NULL distance or geography */
942  if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
943  PG_RETURN_NULL();
944 
945  /* Get our geometry object loaded into memory. */
946  g = PG_GETARG_GSERIALIZED_P(0);
947 
948  /* Only return for points. */
949  type = gserialized_get_type(g);
950  if ( type != POINTTYPE )
951  {
952  elog(ERROR, "ST_Project(geography) is only valid for point inputs");
953  PG_RETURN_NULL();
954  }
955 
956  distance = PG_GETARG_FLOAT8(1); /* Distance in Meters */
957  lwgeom = lwgeom_from_gserialized(g);
958 
959  /* EMPTY things cannot be projected from */
960  if ( lwgeom_is_empty(lwgeom) )
961  {
962  lwgeom_free(lwgeom);
963  elog(ERROR, "ST_Project(geography) cannot project from an empty start point");
964  PG_RETURN_NULL();
965  }
966 
967  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
968  azimuth = PG_GETARG_FLOAT8(2); /* Azimuth in Radians */
969 
970  /* Initialize spheroid */
971  spheroid_init_from_srid(fcinfo, gserialized_get_srid(g), &s);
972 
973  /* Handle the zero distance case */
974  if( FP_EQUALS(distance, 0.0) )
975  {
976  PG_RETURN_POINTER(g);
977  }
978 
979  /* Calculate the length */
980  lwp_projected = lwgeom_project_spheroid(lwgeom_as_lwpoint(lwgeom), &s, distance, azimuth);
981 
982  /* Something went wrong... */
983  if ( lwp_projected == NULL )
984  {
985  elog(ERROR, "lwgeom_project_spheroid returned null");
986  PG_RETURN_NULL();
987  }
988 
989  /* Clean up, but not all the way to the point arrays */
990  lwgeom_free(lwgeom);
991  g_out = geography_serialize(lwpoint_as_lwgeom(lwp_projected));
992  lwpoint_free(lwp_projected);
993 
994  PG_FREE_IF_COPY(g, 0);
995  PG_RETURN_POINTER(g_out);
996 }
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
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:100
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:1128
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
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwinline.h:172
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:326
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: