PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ LWGEOM_angle()

Datum LWGEOM_angle ( PG_FUNCTION_ARGS  )

Definition at line 2702 of file lwgeom_functions_basic.c.

2703 {
2704  GSERIALIZED *seri_geoms[4];
2705  LWGEOM *geom_unser;
2706  LWPOINT *lwpoint;
2707  POINT2D points[4];
2708  double az1, az2;
2709  double result;
2710  int32_t srids[4];
2711  int i = 0;
2712  int j = 0;
2713  int err_code = 0;
2714  int n_args = PG_NARGS();
2715 
2716  /* no deserialize, checking for common error first*/
2717  for (i = 0; i < n_args; i++)
2718  {
2719  seri_geoms[i] = PG_GETARG_GSERIALIZED_P(i);
2720  if (gserialized_is_empty(seri_geoms[i]))
2721  { /* empty geom */
2722  if (i == 3)
2723  {
2724  n_args = 3;
2725  }
2726  else
2727  {
2728  err_code = 1;
2729  break;
2730  }
2731  }
2732  else
2733  {
2734  if (gserialized_get_type(seri_geoms[i]) != POINTTYPE)
2735  { /* geom type */
2736  err_code = 2;
2737  break;
2738  }
2739  else
2740  {
2741  srids[i] = gserialized_get_srid(seri_geoms[i]);
2742  if (srids[0] != srids[i])
2743  { /* error on srid*/
2744  err_code = 3;
2745  break;
2746  }
2747  }
2748  }
2749  }
2750  if (err_code > 0)
2751  switch (err_code)
2752  {
2753  default: /*always executed*/
2754  for (j = 0; j <= i; j++)
2755  PG_FREE_IF_COPY(seri_geoms[j], j);
2756  /*FALLTHROUGH*/
2757  case 1:
2758  lwpgerror("Empty geometry");
2759  PG_RETURN_NULL();
2760  break;
2761 
2762  case 2:
2763  lwpgerror("Argument must be POINT geometries");
2764  PG_RETURN_NULL();
2765  break;
2766 
2767  case 3:
2768  lwpgerror("Operation on mixed SRID geometries");
2769  PG_RETURN_NULL();
2770  break;
2771  }
2772  /* extract points */
2773  for (i = 0; i < n_args; i++)
2774  {
2775  geom_unser = lwgeom_from_gserialized(seri_geoms[i]);
2776  lwpoint = lwgeom_as_lwpoint(geom_unser);
2777  if (!lwpoint)
2778  {
2779  for (j = 0; j < n_args; j++)
2780  PG_FREE_IF_COPY(seri_geoms[j], j);
2781  lwpgerror("Error unserializing geometry");
2782  PG_RETURN_NULL();
2783  }
2784 
2785  if (!getPoint2d_p(lwpoint->point, 0, &points[i]))
2786  {
2787  /* // can't free serialized geom, it might be needed by lw
2788  for (j=0;j<n_args;j++)
2789  PG_FREE_IF_COPY(seri_geoms[j], j); */
2790  lwpgerror("Error extracting point");
2791  PG_RETURN_NULL();
2792  }
2793  /* lwfree(geom_unser);don't do, lw may rely on this memory
2794  lwpoint_free(lwpoint); dont do , this memory is needed ! */
2795  }
2796  /* // can't free serialized geom, it might be needed by lw
2797  for (j=0;j<n_args;j++)
2798  PG_FREE_IF_COPY(seri_geoms[j], j); */
2799 
2800  /* compute azimuth for the 2 pairs of points
2801  * note that angle is not defined identically for 3 points or 4 points*/
2802  if (n_args == 3)
2803  { /* we rely on azimuth to complain if points are identical */
2804  if (!azimuth_pt_pt(&points[0], &points[1], &az1))
2805  PG_RETURN_NULL();
2806  if (!azimuth_pt_pt(&points[2], &points[1], &az2))
2807  PG_RETURN_NULL();
2808  }
2809  else
2810  {
2811  if (!azimuth_pt_pt(&points[0], &points[1], &az1))
2812  PG_RETURN_NULL();
2813  if (!azimuth_pt_pt(&points[2], &points[3], &az2))
2814  PG_RETURN_NULL();
2815  }
2816  result = az2 - az1;
2817  result += (result < 0) * 2 * M_PI; /* we dont want negative angle*/
2818  PG_RETURN_FLOAT8(result);
2819 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
int azimuth_pt_pt(const POINT2D *p1, const POINT2D *p2, double *ret)
Compute the azimuth of segment AB in radians.
Definition: measures.c:2462
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:342
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131
POINTARRAY * point
Definition: liblwgeom.h:471

References azimuth_pt_pt(), getPoint2d_p(), gserialized_get_srid(), gserialized_get_type(), gserialized_is_empty(), lwgeom_as_lwpoint(), lwgeom_from_gserialized(), LWPOINT::point, POINTTYPE, and result.

Here is the call graph for this function: