Module: Vincenty

Defined in:
ext/vincenty.c

Class Method Summary collapse

Class Method Details

.distance(rb_lon1, rb_lat1, rb_lon2, rb_lat2, rb_a, rb_b) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'ext/vincenty.c', line 45

static VALUE distance(VALUE self, VALUE rb_lon1, VALUE rb_lat1, VALUE rb_lon2, VALUE rb_lat2, VALUE rb_a, VALUE rb_b) {

	Check_Type(rb_lon1, T_FLOAT);
	Check_Type(rb_lat1, T_FLOAT);
	Check_Type(rb_lon2, T_FLOAT);
	Check_Type(rb_lat2, T_FLOAT);
	Check_Type(rb_a, T_FLOAT);
	Check_Type(rb_b, T_FLOAT);

	double lon1 = RFLOAT_VALUE(rb_lon1);
	double lat1 = RFLOAT_VALUE(rb_lat1);
	double lon2 = RFLOAT_VALUE(rb_lon2);
	double lat2 = RFLOAT_VALUE(rb_lat2);
	double a = RFLOAT_VALUE(rb_a);
	double b = RFLOAT_VALUE(rb_b);

	double sinLambda, cosLambda, sinSigma, cosSigma, sigma, sinAlpha, cosSqAlpha, cos2SigmaM, C;
	
	double f = (a-b) / a;
  double L = (lon2-lon1) * DEG_TO_RAD;
  double U1 = atan((1-f) * tan(lat1 * DEG_TO_RAD));
  double U2 = atan((1-f) * tan(lat2 * DEG_TO_RAD));
	double sinU1 = sin(U1), cosU1 = cos(U1);
  double sinU2 = sin(U2), cosU2 = cos(U2);

	int iterLimit = 20;
  double lambda = L, lambdaP=2*PI;
  while ((fabs(lambda-lambdaP) > 1e-12 && --iterLimit>0)) {
		sinLambda = sin(lambda); cosLambda = cos(lambda);
    sinSigma = sqrt((cosU2*sinLambda) * (cosU2*sinLambda) + (cosU1*sinU2-sinU1*cosU2*cosLambda) * (cosU1*sinU2-sinU1*cosU2*cosLambda));
    if (sinSigma==0) return rb_float_new(0);  // co-incident points
    cosSigma = sinU1*sinU2 + cosU1*cosU2*cosLambda;
    sigma = atan2(sinSigma, cosSigma);
    sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
    cosSqAlpha = 1 - sinAlpha*sinAlpha;
    cos2SigmaM = cosSigma - 2*sinU1*sinU2/cosSqAlpha;
    if (isnan(cos2SigmaM)) cos2SigmaM = 0;  // equatorial line: cosSqAlpha=0 (ยง6)
    C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
    lambdaP = lambda;
    lambda = L + (1-C) * f * sinAlpha * (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));
  }

	if (iterLimit==0) return Qnil;  // formula failed to converge

  double uSq = cosSqAlpha * (a*a - b*b) / (b*b);
  double A = 1 + uSq/16384.0*(4096+uSq*(-768+uSq*(320-175*uSq)));
  double B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
  double deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM) -
    B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));

	double s = b*A*(sigma-deltaSigma);
	
	return rb_float_new(s);
}

.point_from_lon_lat(rb_lon1, rb_lat1, rb_bearing, rb_distance, rb_a, rb_b) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'ext/vincenty.c', line 100

static VALUE point_from_lon_lat(VALUE self, VALUE rb_lon1, VALUE rb_lat1, VALUE rb_bearing, VALUE rb_distance, VALUE rb_a, VALUE rb_b) {
	Check_Type(rb_lon1, T_FLOAT);
	Check_Type(rb_lat1, T_FLOAT);
	Check_Type(rb_bearing, T_FLOAT);
	Check_Type(rb_distance, T_FLOAT);
	Check_Type(rb_a, T_FLOAT);
	Check_Type(rb_b, T_FLOAT);
	
	VALUE ret;

	double lon1 = RFLOAT_VALUE(rb_lon1);
	double lat1 = RFLOAT_VALUE(rb_lat1);
	double brng = RFLOAT_VALUE(rb_bearing);
	double s = RFLOAT_VALUE(rb_distance);
	double a = RFLOAT_VALUE(rb_a);
	double b = RFLOAT_VALUE(rb_b);
	
	double f = (a-b) / a;
  double alpha1 = brng * DEG_TO_RAD;
  double sinAlpha1 = sin(alpha1), cosAlpha1 = cos(alpha1);
  
  double tanU1 = (1-f) * tan(lat1 * DEG_TO_RAD);
  double cosU1 = 1 / sqrt((1 + tanU1*tanU1)), sinU1 = tanU1*cosU1;
  double sigma1 = atan2(tanU1, cosAlpha1);
  double sinAlpha = cosU1 * sinAlpha1;
  double cosSqAlpha = 1 - sinAlpha*sinAlpha;
  double uSq = cosSqAlpha * (a*a - b*b) / (b*b);
  double A = 1 + uSq/16384*(4096+uSq*(-768+uSq*(320-175*uSq)));
  double B = uSq/1024 * (256+uSq*(-128+uSq*(74-47*uSq)));
  
  double sigma = s / (b*A), sigmaP = 2*PI;
	double cos2SigmaM, sinSigma, deltaSigma, cosSigma;
  while (fabs(sigma-sigmaP) > 1e-12) {
    cos2SigmaM = cos(2*sigma1 + sigma);
    sinSigma = sin(sigma), cosSigma = cos(sigma);
    deltaSigma = B*sinSigma*(cos2SigmaM+B/4*(cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)-
      B/6*cos2SigmaM*(-3+4*sinSigma*sinSigma)*(-3+4*cos2SigmaM*cos2SigmaM)));
    sigmaP = sigma;
    sigma = s / (b*A) + deltaSigma;
  }

  double tmp = sinU1*sinSigma - cosU1*cosSigma*cosAlpha1;
  double lat2 = atan2(sinU1*cosSigma + cosU1*sinSigma*cosAlpha1, (1-f)*sqrt(sinAlpha*sinAlpha + tmp*tmp));
  double lambda = atan2(sinSigma*sinAlpha1, cosU1*cosSigma - sinU1*sinSigma*cosAlpha1);
  double C = f/16*cosSqAlpha*(4+f*(4-3*cosSqAlpha));
  double L = lambda - (1-C) * f * sinAlpha * (sigma + C*sinSigma*(cos2SigmaM+C*cosSigma*(-1+2*cos2SigmaM*cos2SigmaM)));

  double revAz = atan2(sinAlpha, -tmp);  // final bearing

	ret = rb_ary_new2(2); /* [lon, lat] */
	rb_ary_store(ret, 0, rb_float_new(lon1+L/DEG_TO_RAD));
	rb_ary_store(ret, 1, rb_float_new(lat2/DEG_TO_RAD));

	return ret;
}