PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ LWGEOM_angle()

Datum LWGEOM_angle ( PG_FUNCTION_ARGS  )

Definition at line 2473 of file lwgeom_functions_basic.c.

2474 {
2475  GSERIALIZED *seri_geoms[4];
2476  LWGEOM *geom_unser;
2477  LWPOINT *lwpoint;
2478  POINT2D points[4];
2479  double az1, az2;
2480  double result;
2481  int32_t srids[4];
2482  int i = 0;
2483  int j = 0;
2484  int err_code = 0;
2485  int n_args = PG_NARGS();
2486 
2487  /* no deserialize, checking for common error first*/
2488  for (i = 0; i < n_args; i++)
2489  {
2490  seri_geoms[i] = PG_GETARG_GSERIALIZED_P(i);
2491  if (gserialized_is_empty(seri_geoms[i]))
2492  { /* empty geom */
2493  if (i == 3)
2494  {
2495  n_args = 3;
2496  }
2497  else
2498  {
2499  err_code = 1;
2500  break;
2501  }
2502  }
2503  else
2504  {
2505  if (gserialized_get_type(seri_geoms[i]) != POINTTYPE)
2506  { /* geom type */
2507  err_code = 2;
2508  break;
2509  }
2510  else
2511  {
2512  srids[i] = gserialized_get_srid(seri_geoms[i]);
2513  if (srids[0] != srids[i])
2514  { /* error on srid*/
2515  err_code = 3;
2516  break;
2517  }
2518  }
2519  }
2520  }
2521  if (err_code > 0)
2522  switch (err_code)
2523  {
2524  default: /*always executed*/
2525  for (j = 0; j <= i; j++)
2526  PG_FREE_IF_COPY(seri_geoms[j], j);
2527  /*FALLTHROUGH*/
2528  case 1:
2529  lwpgerror("Empty geometry");
2530  PG_RETURN_NULL();
2531  break;
2532 
2533  case 2:
2534  lwpgerror("Argument must be POINT geometries");
2535  PG_RETURN_NULL();
2536  break;
2537 
2538  case 3:
2539  lwpgerror("Operation on mixed SRID geometries");
2540  PG_RETURN_NULL();
2541  break;
2542  }
2543  /* extract points */
2544  for (i = 0; i < n_args; i++)
2545  {
2546  geom_unser = lwgeom_from_gserialized(seri_geoms[i]);
2547  lwpoint = lwgeom_as_lwpoint(geom_unser);
2548  if (!lwpoint)
2549  {
2550  for (j = 0; j < n_args; j++)
2551  PG_FREE_IF_COPY(seri_geoms[j], j);
2552  lwpgerror("Error unserializing geometry");
2553  PG_RETURN_NULL();
2554  }
2555 
2556  if (!getPoint2d_p(lwpoint->point, 0, &points[i]))
2557  {
2558  /* // can't free serialized geom, it might be needed by lw
2559  for (j=0;j<n_args;j++)
2560  PG_FREE_IF_COPY(seri_geoms[j], j); */
2561  lwpgerror("Error extracting point");
2562  PG_RETURN_NULL();
2563  }
2564  /* lwfree(geom_unser);don't do, lw may rely on this memory
2565  lwpoint_free(lwpoint); dont do , this memory is needed ! */
2566  }
2567  /* // can't free serialized geom, it might be needed by lw
2568  for (j=0;j<n_args;j++)
2569  PG_FREE_IF_COPY(seri_geoms[j], j); */
2570 
2571  /* compute azimuth for the 2 pairs of points
2572  * note that angle is not defined identically for 3 points or 4 points*/
2573  if (n_args == 3)
2574  { /* we rely on azimuth to complain if points are identical */
2575  if (!azimuth_pt_pt(&points[0], &points[1], &az1))
2576  PG_RETURN_NULL();
2577  if (!azimuth_pt_pt(&points[2], &points[1], &az2))
2578  PG_RETURN_NULL();
2579  }
2580  else
2581  {
2582  if (!azimuth_pt_pt(&points[0], &points[1], &az1))
2583  PG_RETURN_NULL();
2584  if (!azimuth_pt_pt(&points[2], &points[3], &az2))
2585  PG_RETURN_NULL();
2586  }
2587  result = az2 - az1;
2588  result += (result < 0) * 2 * M_PI; /* we dont want negative angle*/
2589  PG_RETURN_FLOAT8(result);
2590 }
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: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:2461
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:343
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131
POINTARRAY * point
Definition: liblwgeom.h:485

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: