XS.xs

filter: Statementstmt 84.6% 77 / 91 Branchbran 50.0% 11 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.8% 88 / 113 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Geo/Coordinates/VandH/XS.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include <math.h>
5  
6 #define PI 3.1415926535897932384626433832795
7  
8 /*
9 * Polynomial constants
10 */
11 #define K1 .99435487
12 #define K2 .00336523
13 #define K3 -.00065596
14 #define K4 .00005606
15 #define K5 -.00000188
16  
17 /*
18 * spherical coordinates of eastern reference point
19 * EX^2 + EY^2 + EZ^2 = 1
20 */
21 #define EX .40426992
22 #define EY .68210848
23 #define EZ .60933887
24  
25 /* spherical coordinates of western reference point
26 * WX^2 + WY^2 + WZ^2 = 1
27 */
28 #define WX .65517646
29 #define WY .37733790
30 #define WZ .65449210
31  
32 /*
33 * spherical coordinates of V-H coordinate system
34 * PX^2 + PY^2 + PZ^2 = 1
35 */
36 #define PX -0.555977821730048699
37 #define PY -0.345728488161089920
38 #define PZ 0.755883902605524030
39  
40 /*
41 * Rotation by 76.597497064 degrees
42 * We use the cosine ROTC and sin ROTS values
43 */
44 #define ROTC 0.2317903984757393
45 #define ROTS 0.9727657534959061
46  
47 /*
48 * orthogonal translation values
49 */
50 #define TRANSV 6363.235
51 #define TRANSH 2250.700
52  
53 /*
54 * radius of earth in sqrt(0.1)-mile units, minus 0.3 percent
55 */
56 #define RADIUS 12481.103
57  
58  
59 3 double radians (double degrees) {
60 3 return degrees*PI/180.0;
61 }
62  
63 2 double degrees (double radians) {
64 2 return radians*180.0/PI;
65 }
66  
67 /*
68 * Return the distance in miles between 2 VH pairs.
69 */
70 0 double distance(double v1, double h1, double v2, double h2) {
71 0 double dv = v2 - v1;
72 0 double dh = h2 - h1;
73 0 return sqrt((dv*dv + dh*dh)/10.0);
74 }
75  
76 /**
77 * Computes Bellcore/AT&T V & H (vertical and horizontal)
78 * coordinates from latitude and longitude. Used primarily by
79 * local exchange carriers (LEC's) to compute the V & H coordinates
80 * for wire centers.
81 *
82 * This is an implementation of the Donald Elliptical Projection,
83 * a Two-Point Equidistant projection developed by Jay K. Donald
84 * of AT&T in 1956 to establish long-distance telephone rates.
85 * (ref: "V-H Coordinate Rediscovered", Eric K. Grimmelmann, Bell
86 * Labs Tech. Memo, 9/80. (References Jay Donald notes of Jan 17, 1957.))
87 * Ashok Ingle of Bellcore also wrote an internal memo on the subject.
88 *
89 * The projection is specially modified for the ellipsoid and
90 * is confined to the United States and southern Canada.
91 *
92 * Derived from a program obtained from an anonymous author
93 * within Bellcore by way of the National Exchange Carrier
94 * Association. Cleaned up and improved a bit by
95 * Tom Libert (tom@comsol.com, libert@citi.umich.edu).
96 */
97  
98  
99 /** lat and lon are in degrees, positive north and east. */
100 1 void toVH(double lat, double lon) {
101  
102 double lon1,latsq,lat1,cos_lat1,x,y,z,e,w,ht,vt,v,h;
103 1 dXSARGS;
104 1 sp = mark;
105  
106 1 lat = radians(lat);
107 1 lon = radians(lon);
108  
109 /* Translate east by 52 degrees */
110 1 lon1 = lon + radians(52.0);
111  
112 /* Convert latitude to geocentric latitude using Horner's rule */
113 1 latsq = lat*lat;
114 1 lat1 = lat*(K1 + (K2 + (K3 + (K4 + K5*latsq)*latsq)*latsq)*latsq);
115  
116 /* x, y, and z are the spherical coordinates corresponding to lat, lon. */
117 1 cos_lat1 = cos(lat1);
118 1 x = cos_lat1*sin(-lon1);
119 1 y = cos_lat1*cos(-lon1);
120 1 z = sin(lat1);
121  
122 /* e and w are the cosine of the angular distance (radians) between
123 * our point and the east and west centers.
124 */
125 1 e = EX*x + EY*y + EZ*z;
126 1 w = WX*x + WY*y + WZ*z;
127 1 ▶ e = e > 1.0 ? 1.0 : e;
Branch: 1 branch
BranchTrueFalseTotal
e = e > 1.0 ? 1.0 : e; 0 1 1
128 1 ▶ w = w > 1.0 ? 1.0 : w;
Branch: 1 branch
BranchTrueFalseTotal
w = w > 1.0 ? 1.0 : w; 0 1 1
129 1 e = (PI/2.0) - atan(e/sqrt(1 - e*e));
130 1 w = (PI/2.0) - atan(w/sqrt(1 - w*w));
131  
132 /* e and w are now in radians. */
133 1 ht = (e*e - w*w + .16)/.8;
134 1 vt = sqrt(fabs(e*e - ht*ht));
135 1 ▶ vt = (PX*x + PY*y + PZ*z) < 0 ? -vt : vt;
Branch: 1 branch
BranchTrueFalseTotal
vt = (PX*x + PY*y + PZ*z) < 0 ? -vt : vt; 0 1 1
136  
137 /* rotate and translate to get final v and h. */
138 1 v = TRANSV + RADIUS*ROTC*ht - RADIUS*ROTS*vt;
139 1 h = TRANSH + RADIUS*ROTS*ht + RADIUS*ROTC*vt;
140  
141 1 ▶ XPUSHs(sv_2mortal(newSVnv(v)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVnv(v))); 0 1 1
142 1 ▶ XPUSHs(sv_2mortal(newSVnv(h)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVnv(h))); 0 1 1
143 1 PUTBACK;
144  
145 1 }
146  
147 /**
148 * V&H is a system of coordinates (V and H) for describing
149 * locations of rate centers in the United States. The
150 * projection, devised by J. K. Donald, is an "elliptical,"
151 * or "doubly equidistant" projection, scaled down by a factor
152 * of 0.003 to balance errors.
153 *
154 * The foci of the projection, from which distances are
155 * measured accurately (except for the scale correction),
156 * are at 37d 42m 14.69s N, 82d 39m 15.27s W (in Floyd Co.,
157 * Ky.) and 41d 02m 55.53s N, 112d 03m 39.35 W (in Webster
158 * Co., Utah). They are just 0.4 radians apart.
159 *
160 * Here is the transformation from latitude and longitude to V&H:
161 * First project the earth from its ellipsoidal surface
162 * to a sphere. This alters the latitude; the coefficients
163 * bi in the program are the coefficients of the polynomial
164 * approximation for the inverse transformation. (The
165 * function is odd, so the coefficients are for the linear
166 * term, the cubic term, and so on.) Also subtract 52 degrees
167 * from the longitude.
168 *
169 * For the rest, compute the arc distances of the given point
170 * to the reference points, and transform them to the coordinate
171 * system in which the line through the reference points is the
172 * X-axis and the origin is the eastern reference point.
173 * The solution is
174 * h = (square of distance to E - square of distance to W
175 * + square of distance between E and W) /
176 * twice distance between E and W;
177 * v = square root of absolute value of (square of
178 * distance to E - square of h).
179 * Reduce by three-tenths of a percent, rotate by 76.597497
180 * degrees, and add 6363.235 to V and 2250.7 to H.
181 *
182 * To go the other way, as this program does, undo the final translation,
183 * rotation, and scaling. The z-value Pz of the point on the x-y-z sphere
184 * satisfies the quadratic Azz+Bz+c=0, where
185 * A = (ExWz-EzWx)^2 + (EyWzx-EzWy)^2 + (ExWy-EyWx)^2;
186 * B = -2[(Ex cos(arc to W) - Wx cos(arc to E))(ExWz-EzWx) -
187 * (Ey cos(arc to W) -Wy cos(arc to E))(EyWz-EzWy)];
188 * C = (Ex cos(arc to W) - Wx cos(arc to E))^2 +
189 * (Ey cos(arc to W) - Wy cos(arc to E))^2 -
190 * (ExWy - EyWx)^2.
191 * Solve with the quadratic formula. The latitude is simply the
192 * arc sine of Pz. Px and Py satisfy
193 * ExPx + EyPy + EzPz = cos(arc to E);
194 * WxPx + WyPy + WzPz = cos(arc to W).
195 * Substitute Pz's value, and solve linearly to get Px and Py.
196 * The longitude is the arc tangent of Px/Py.
197 * Finally, this latitude and longitude are spherical; use the
198 * inverse polynomial approximation on the latitude to get the
199 * ellipsoidal earth latitude, and add 52 degrees to the longitude.
200 */
201  
202 1 void toLatLon (double v, double h) {
203  
204 double GX,GY,A,Q,Q2,EPSILON,t1,t2,vhat,hhat,e,w,fx,fy,b,c,disc,x,y,z;
205 double delta,lat,lat2,earthlat,lon,earthlon;
206 /*
207 * Use polynomial approximation for inverse mapping (sphere to spheroid)
208 */
209 1 double bi[] = {
210 1.00567724920722457,
211 -0.00344230425560210245,
212 0.000713971534527667990,
213 -0.0000777240053499279217,
214 0.00000673180367053244284,
215 -0.000000742595338885741395,
216 0.0000000905058919926194134
217 };
218 1 dXSARGS;
219 1 sp = mark;
220  
221 /* GX = ExWz - EzWx; GY = EyWz - EzWy */
222 1 GX = 0.216507961908834992;
223 1 GY = -0.134633014879368199;
224 /* A = (ExWz-EzWx)^2 + (EyWz-EzWy)^2 + (ExWy-EyWx)^2 */
225 1 A = 0.151646645621077297;
226 /* Q = ExWy-EyWx; Q2 = Q*Q */
227 1 Q = -0.294355056616412800;
228 1 Q2= 0.0866448993556515751;
229 1 EPSILON = .0000001;
230  
231 1 t1 = (v - TRANSV) / RADIUS;
232 1 t2 = (h - TRANSH) / RADIUS;
233 1 vhat = ROTC*t2 - ROTS*t1;
234 1 hhat = ROTS*t2 + ROTC*t1;
235 1 e = cos(sqrt(vhat*vhat + hhat*hhat));
236 1 w = cos(sqrt(vhat*vhat + (hhat-0.4)*(hhat-0.4)));
237 1 fx = EY*w - WY*e;
238 1 fy = EX*w - WX*e;
239 1 b = fx*GX + fy*GY;
240 1 c = fx*fx + fy*fy - Q2;
241 1 disc = b*b - A*c; /* discriminant */
242 x, y, z, delta;
243 1 ▶ if (fabs(disc) < EPSILON) {
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(disc) < EPSILON) { 0 1 1
244 0 z = b/A;
245 0 x = (GX*z - fx)/Q;
246 0 y = (fy - GY*z)/Q;
247 } else {
248 1 delta = sqrt(disc);
249 1 z = (b + delta)/A;
250 1 x = (GX*z - fx)/Q;
251 1 y = (fy - GY*z)/Q;
252 1 ▶ if (vhat * (PX*x + PY*y + PZ*z) < 0) { /* wrong direction */
Branch: 1 branch
BranchTrueFalseTotal
if (vhat * (PX*x + PY*y + PZ*z) < 0) { /* wrong direction */ 0 1 1
253 0 z = (b - delta)/A;
254 0 x = (GX*z - fx)/Q;
255 0 y = (fy - GY*z)/Q;
256 }
257 }
258 1 lat = asin(z);
259 1 lat2 = lat*lat;
260 1 earthlat = lat*(bi[0] + lat2*(bi[1] + lat2*(bi[2] + lat2*(bi[3] + \
261 1 lat2*(bi[4] + lat2*(bi[5] + lat2*(bi[6])))))));
262 1 earthlat = degrees(earthlat);
263  
264 /* Adjust longitude by 52 degrees */
265 1 lon = degrees(atan2(x, y));
266 1 earthlon = lon + 52.0;
267  
268 1 ▶ XPUSHs(sv_2mortal(newSVnv(earthlat)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVnv(earthlat))); 0 1 1
269 1 ▶ XPUSHs(sv_2mortal(newSVnv(earthlon)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVnv(earthlon))); 0 1 1
270 1 PUTBACK;
271 1 }
272  
273  
274 MODULE = Geo::Coordinates::VandH::XS PACKAGE = Geo::Coordinates::VandH::XS
275  
276 PROTOTYPES: DISABLE
277  
278 double
279 radians (degrees)
280 double degrees
281  
282 double
283 degrees (radians)
284 double radians
285  
286 double
287 distance (v1, h1, v2, h2)
288 double v1
289 double h1
290 double v2
291 double h2
292  
293 void
294 toVH (lat, lon)
295 double lat
296 double lon
297 PREINIT:
298 I32* temp;
299 PPCODE:
300 1 temp = PL_markstack_ptr++;
301 1 toVH(lat, lon);
302 1 ▶ if (PL_markstack_ptr != temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_markstack_ptr != temp) { 0 1 1
303 /* truly void, because dXSARGS not invoked */
304 0 PL_markstack_ptr = temp;
305 0 XSRETURN_EMPTY; /* return empty stack */
306 }
307 /* must have used dXSARGS; list context implied */
308 1 return; /* assume stack size is correct */
309  
310 void
311 toLatLon (v, h)
312 double v
313 double h
314 PREINIT:
315 I32* temp;
316 PPCODE:
317 1 temp = PL_markstack_ptr++;
318 1 toLatLon(v, h);
319 1 ▶ if (PL_markstack_ptr != temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_markstack_ptr != temp) { 0 1 1
320 /* truly void, because dXSARGS not invoked */
321 0 PL_markstack_ptr = temp;
322 0 XSRETURN_EMPTY; /* return empty stack */
323 }
324 /* must have used dXSARGS; list context implied */
325 1 return; /* assume stack size is correct */
326  
327  
328  
↑ Summary blib/lib/Geo/Coordinates/VandH/XS.pm »