PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ geography_project()

Datum geography_project ( PG_FUNCTION_ARGS  )

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

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