PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ ptarray_length_spheroid()

double ptarray_length_spheroid ( const POINTARRAY pa,
const SPHEROID s 
)

Definition at line 3156 of file lwgeodetic.c.

References SPHEROID::a, SPHEROID::b, POINTARRAY::flags, FLAGS_GET_Z, geographic_point_init(), getPoint4d_p(), LW_FALSE, POINTARRAY::npoints, SPHEROID::radius, sphere_distance(), spheroid_distance(), POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by lwgeom_length_spheroid().

3157 {
3158  GEOGRAPHIC_POINT a, b;
3159  double za = 0.0, zb = 0.0;
3160  POINT4D p;
3161  uint32_t i;
3162  int hasz = LW_FALSE;
3163  double length = 0.0;
3164  double seglength = 0.0;
3165 
3166  /* Return zero on non-sensical inputs */
3167  if ( ! pa || pa->npoints < 2 )
3168  return 0.0;
3169 
3170  /* See if we have a third dimension */
3171  hasz = FLAGS_GET_Z(pa->flags);
3172 
3173  /* Initialize first point */
3174  getPoint4d_p(pa, 0, &p);
3175  geographic_point_init(p.x, p.y, &a);
3176  if ( hasz )
3177  za = p.z;
3178 
3179  /* Loop and sum the length for each segment */
3180  for ( i = 1; i < pa->npoints; i++ )
3181  {
3182  seglength = 0.0;
3183  getPoint4d_p(pa, i, &p);
3184  geographic_point_init(p.x, p.y, &b);
3185  if ( hasz )
3186  zb = p.z;
3187 
3188  /* Special sphere case */
3189  if ( s->a == s->b )
3190  seglength = s->radius * sphere_distance(&a, &b);
3191  /* Spheroid case */
3192  else
3193  seglength = spheroid_distance(&a, &b, s);
3194 
3195  /* Add in the vertical displacement if we're in 3D */
3196  if ( hasz )
3197  seglength = sqrt( (zb-za)*(zb-za) + seglength*seglength );
3198 
3199  /* Add this segment length to the total */
3200  length += seglength;
3201 
3202  /* B gets incremented in the next loop, so we save the value here */
3203  a = b;
3204  za = zb;
3205  }
3206  return length;
3207 }
double x
Definition: liblwgeom.h:354
double sphere_distance(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
Given two points on a unit sphere, calculate their distance apart in radians.
Definition: lwgeodetic.c:917
double b
Definition: liblwgeom.h:316
double radius
Definition: liblwgeom.h:320
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:47
unsigned int uint32_t
Definition: uthash.h:78
#define LW_FALSE
Definition: liblwgeom.h:76
uint8_t flags
Definition: liblwgeom.h:371
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:139
double z
Definition: liblwgeom.h:354
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:113
double a
Definition: liblwgeom.h:315
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:171
double spheroid_distance(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid)
Computes the shortest distance along the surface of the spheroid between two points.
Definition: lwspheroid.c:186
double y
Definition: liblwgeom.h:354
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: