PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ LWGEOM_angle()

Datum LWGEOM_angle ( PG_FUNCTION_ARGS  )

Definition at line 2694 of file lwgeom_functions_basic.c.

2695 {
2696  GSERIALIZED *seri_geoms[4];
2697  LWGEOM *geom_unser;
2698  LWPOINT *lwpoint;
2699  POINT2D points[4];
2700  double az1, az2;
2701  double result;
2702  int32_t srids[4];
2703  int i = 0;
2704  int j = 0;
2705  int err_code = 0;
2706  int n_args = PG_NARGS();
2707 
2708  /* no deserialize, checking for common error first*/
2709  for (i = 0; i < n_args; i++)
2710  {
2711  seri_geoms[i] = PG_GETARG_GSERIALIZED_P(i);
2712  if (gserialized_is_empty(seri_geoms[i]))
2713  { /* empty geom */
2714  if (i == 3)
2715  {
2716  n_args = 3;
2717  }
2718  else
2719  {
2720  err_code = 1;
2721  break;
2722  }
2723  }
2724  else
2725  {
2726  if (gserialized_get_type(seri_geoms[i]) != POINTTYPE)
2727  { /* geom type */
2728  err_code = 2;
2729  break;
2730  }
2731  else
2732  {
2733  srids[i] = gserialized_get_srid(seri_geoms[i]);
2734  if (srids[0] != srids[i])
2735  { /* error on srid*/
2736  err_code = 3;
2737  break;
2738  }
2739  }
2740  }
2741  }
2742  if (err_code > 0)
2743  switch (err_code)
2744  {
2745  default: /*always executed*/
2746  for (j = 0; j <= i; j++)
2747  PG_FREE_IF_COPY(seri_geoms[j], j);
2748  /*FALLTHROUGH*/
2749  case 1:
2750  lwpgerror("Empty geometry");
2751  PG_RETURN_NULL();
2752  break;
2753 
2754  case 2:
2755  lwpgerror("Argument must be POINT geometries");
2756  PG_RETURN_NULL();
2757  break;
2758 
2759  case 3:
2760  lwpgerror("Operation on mixed SRID geometries");
2761  PG_RETURN_NULL();
2762  break;
2763  }
2764  /* extract points */
2765  for (i = 0; i < n_args; i++)
2766  {
2767  geom_unser = lwgeom_from_gserialized(seri_geoms[i]);
2768  lwpoint = lwgeom_as_lwpoint(geom_unser);
2769  if (!lwpoint)
2770  {
2771  for (j = 0; j < n_args; j++)
2772  PG_FREE_IF_COPY(seri_geoms[j], j);
2773  lwpgerror("Error unserializing geometry");
2774  PG_RETURN_NULL();
2775  }
2776 
2777  if (!getPoint2d_p(lwpoint->point, 0, &points[i]))
2778  {
2779  /* // can't free serialized geom, it might be needed by lw
2780  for (j=0;j<n_args;j++)
2781  PG_FREE_IF_COPY(seri_geoms[j], j); */
2782  lwpgerror("Error extracting point");
2783  PG_RETURN_NULL();
2784  }
2785  /* lwfree(geom_unser);don't do, lw may rely on this memory
2786  lwpoint_free(lwpoint); dont do , this memory is needed ! */
2787  }
2788  /* // can't free serialized geom, it might be needed by lw
2789  for (j=0;j<n_args;j++)
2790  PG_FREE_IF_COPY(seri_geoms[j], j); */
2791 
2792  /* compute azimuth for the 2 pairs of points
2793  * note that angle is not defined identically for 3 points or 4 points*/
2794  if (n_args == 3)
2795  { /* we rely on azimuth to complain if points are identical */
2796  if (!azimuth_pt_pt(&points[0], &points[1], &az1))
2797  PG_RETURN_NULL();
2798  if (!azimuth_pt_pt(&points[2], &points[1], &az2))
2799  PG_RETURN_NULL();
2800  }
2801  else
2802  {
2803  if (!azimuth_pt_pt(&points[0], &points[1], &az1))
2804  PG_RETURN_NULL();
2805  if (!azimuth_pt_pt(&points[2], &points[3], &az2))
2806  PG_RETURN_NULL();
2807  }
2808  result = az2 - az1;
2809  result += (result < 0) * 2 * M_PI; /* we dont want negative angle*/
2810  PG_RETURN_FLOAT8(result);
2811 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
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:155
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:268
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:181
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:118
int azimuth_pt_pt(const POINT2D *p1, const POINT2D *p2, double *ret)
Compute the azimuth of segment AB in radians.
Definition: measures.c:2408
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:127
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: