qsc.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // QSC projection rewritten from the original PROJ4
  2. // https://github.com/OSGeo/proj.4/blob/master/src/PJ_qsc.c
  3. import {EPSLN, TWO_PI, SPI, HALF_PI, FORTPI} from '../constants/values';
  4. /* constants */
  5. var FACE_ENUM = {
  6. FRONT: 1,
  7. RIGHT: 2,
  8. BACK: 3,
  9. LEFT: 4,
  10. TOP: 5,
  11. BOTTOM: 6
  12. };
  13. var AREA_ENUM = {
  14. AREA_0: 1,
  15. AREA_1: 2,
  16. AREA_2: 3,
  17. AREA_3: 4
  18. };
  19. export function init() {
  20. this.x0 = this.x0 || 0;
  21. this.y0 = this.y0 || 0;
  22. this.lat0 = this.lat0 || 0;
  23. this.long0 = this.long0 || 0;
  24. this.lat_ts = this.lat_ts || 0;
  25. this.title = this.title || "Quadrilateralized Spherical Cube";
  26. /* Determine the cube face from the center of projection. */
  27. if (this.lat0 >= HALF_PI - FORTPI / 2.0) {
  28. this.face = FACE_ENUM.TOP;
  29. } else if (this.lat0 <= -(HALF_PI - FORTPI / 2.0)) {
  30. this.face = FACE_ENUM.BOTTOM;
  31. } else if (Math.abs(this.long0) <= FORTPI) {
  32. this.face = FACE_ENUM.FRONT;
  33. } else if (Math.abs(this.long0) <= HALF_PI + FORTPI) {
  34. this.face = this.long0 > 0.0 ? FACE_ENUM.RIGHT : FACE_ENUM.LEFT;
  35. } else {
  36. this.face = FACE_ENUM.BACK;
  37. }
  38. /* Fill in useful values for the ellipsoid <-> sphere shift
  39. * described in [LK12]. */
  40. if (this.es !== 0) {
  41. this.one_minus_f = 1 - (this.a - this.b) / this.a;
  42. this.one_minus_f_squared = this.one_minus_f * this.one_minus_f;
  43. }
  44. }
  45. // QSC forward equations--mapping lat,long to x,y
  46. // -----------------------------------------------------------------
  47. export function forward(p) {
  48. var xy = {x: 0, y: 0};
  49. var lat, lon;
  50. var theta, phi;
  51. var t, mu;
  52. /* nu; */
  53. var area = {value: 0};
  54. // move lon according to projection's lon
  55. p.x -= this.long0;
  56. /* Convert the geodetic latitude to a geocentric latitude.
  57. * This corresponds to the shift from the ellipsoid to the sphere
  58. * described in [LK12]. */
  59. if (this.es !== 0) {//if (P->es != 0) {
  60. lat = Math.atan(this.one_minus_f_squared * Math.tan(p.y));
  61. } else {
  62. lat = p.y;
  63. }
  64. /* Convert the input lat, lon into theta, phi as used by QSC.
  65. * This depends on the cube face and the area on it.
  66. * For the top and bottom face, we can compute theta and phi
  67. * directly from phi, lam. For the other faces, we must use
  68. * unit sphere cartesian coordinates as an intermediate step. */
  69. lon = p.x; //lon = lp.lam;
  70. if (this.face === FACE_ENUM.TOP) {
  71. phi = HALF_PI - lat;
  72. if (lon >= FORTPI && lon <= HALF_PI + FORTPI) {
  73. area.value = AREA_ENUM.AREA_0;
  74. theta = lon - HALF_PI;
  75. } else if (lon > HALF_PI + FORTPI || lon <= -(HALF_PI + FORTPI)) {
  76. area.value = AREA_ENUM.AREA_1;
  77. theta = (lon > 0.0 ? lon - SPI : lon + SPI);
  78. } else if (lon > -(HALF_PI + FORTPI) && lon <= -FORTPI) {
  79. area.value = AREA_ENUM.AREA_2;
  80. theta = lon + HALF_PI;
  81. } else {
  82. area.value = AREA_ENUM.AREA_3;
  83. theta = lon;
  84. }
  85. } else if (this.face === FACE_ENUM.BOTTOM) {
  86. phi = HALF_PI + lat;
  87. if (lon >= FORTPI && lon <= HALF_PI + FORTPI) {
  88. area.value = AREA_ENUM.AREA_0;
  89. theta = -lon + HALF_PI;
  90. } else if (lon < FORTPI && lon >= -FORTPI) {
  91. area.value = AREA_ENUM.AREA_1;
  92. theta = -lon;
  93. } else if (lon < -FORTPI && lon >= -(HALF_PI + FORTPI)) {
  94. area.value = AREA_ENUM.AREA_2;
  95. theta = -lon - HALF_PI;
  96. } else {
  97. area.value = AREA_ENUM.AREA_3;
  98. theta = (lon > 0.0 ? -lon + SPI : -lon - SPI);
  99. }
  100. } else {
  101. var q, r, s;
  102. var sinlat, coslat;
  103. var sinlon, coslon;
  104. if (this.face === FACE_ENUM.RIGHT) {
  105. lon = qsc_shift_lon_origin(lon, +HALF_PI);
  106. } else if (this.face === FACE_ENUM.BACK) {
  107. lon = qsc_shift_lon_origin(lon, +SPI);
  108. } else if (this.face === FACE_ENUM.LEFT) {
  109. lon = qsc_shift_lon_origin(lon, -HALF_PI);
  110. }
  111. sinlat = Math.sin(lat);
  112. coslat = Math.cos(lat);
  113. sinlon = Math.sin(lon);
  114. coslon = Math.cos(lon);
  115. q = coslat * coslon;
  116. r = coslat * sinlon;
  117. s = sinlat;
  118. if (this.face === FACE_ENUM.FRONT) {
  119. phi = Math.acos(q);
  120. theta = qsc_fwd_equat_face_theta(phi, s, r, area);
  121. } else if (this.face === FACE_ENUM.RIGHT) {
  122. phi = Math.acos(r);
  123. theta = qsc_fwd_equat_face_theta(phi, s, -q, area);
  124. } else if (this.face === FACE_ENUM.BACK) {
  125. phi = Math.acos(-q);
  126. theta = qsc_fwd_equat_face_theta(phi, s, -r, area);
  127. } else if (this.face === FACE_ENUM.LEFT) {
  128. phi = Math.acos(-r);
  129. theta = qsc_fwd_equat_face_theta(phi, s, q, area);
  130. } else {
  131. /* Impossible */
  132. phi = theta = 0;
  133. area.value = AREA_ENUM.AREA_0;
  134. }
  135. }
  136. /* Compute mu and nu for the area of definition.
  137. * For mu, see Eq. (3-21) in [OL76], but note the typos:
  138. * compare with Eq. (3-14). For nu, see Eq. (3-38). */
  139. mu = Math.atan((12 / SPI) * (theta + Math.acos(Math.sin(theta) * Math.cos(FORTPI)) - HALF_PI));
  140. t = Math.sqrt((1 - Math.cos(phi)) / (Math.cos(mu) * Math.cos(mu)) / (1 - Math.cos(Math.atan(1 / Math.cos(theta)))));
  141. /* Apply the result to the real area. */
  142. if (area.value === AREA_ENUM.AREA_1) {
  143. mu += HALF_PI;
  144. } else if (area.value === AREA_ENUM.AREA_2) {
  145. mu += SPI;
  146. } else if (area.value === AREA_ENUM.AREA_3) {
  147. mu += 1.5 * SPI;
  148. }
  149. /* Now compute x, y from mu and nu */
  150. xy.x = t * Math.cos(mu);
  151. xy.y = t * Math.sin(mu);
  152. xy.x = xy.x * this.a + this.x0;
  153. xy.y = xy.y * this.a + this.y0;
  154. p.x = xy.x;
  155. p.y = xy.y;
  156. return p;
  157. }
  158. // QSC inverse equations--mapping x,y to lat/long
  159. // -----------------------------------------------------------------
  160. export function inverse(p) {
  161. var lp = {lam: 0, phi: 0};
  162. var mu, nu, cosmu, tannu;
  163. var tantheta, theta, cosphi, phi;
  164. var t;
  165. var area = {value: 0};
  166. /* de-offset */
  167. p.x = (p.x - this.x0) / this.a;
  168. p.y = (p.y - this.y0) / this.a;
  169. /* Convert the input x, y to the mu and nu angles as used by QSC.
  170. * This depends on the area of the cube face. */
  171. nu = Math.atan(Math.sqrt(p.x * p.x + p.y * p.y));
  172. mu = Math.atan2(p.y, p.x);
  173. if (p.x >= 0.0 && p.x >= Math.abs(p.y)) {
  174. area.value = AREA_ENUM.AREA_0;
  175. } else if (p.y >= 0.0 && p.y >= Math.abs(p.x)) {
  176. area.value = AREA_ENUM.AREA_1;
  177. mu -= HALF_PI;
  178. } else if (p.x < 0.0 && -p.x >= Math.abs(p.y)) {
  179. area.value = AREA_ENUM.AREA_2;
  180. mu = (mu < 0.0 ? mu + SPI : mu - SPI);
  181. } else {
  182. area.value = AREA_ENUM.AREA_3;
  183. mu += HALF_PI;
  184. }
  185. /* Compute phi and theta for the area of definition.
  186. * The inverse projection is not described in the original paper, but some
  187. * good hints can be found here (as of 2011-12-14):
  188. * http://fits.gsfc.nasa.gov/fitsbits/saf.93/saf.9302
  189. * (search for "Message-Id: <9302181759.AA25477 at fits.cv.nrao.edu>") */
  190. t = (SPI / 12) * Math.tan(mu);
  191. tantheta = Math.sin(t) / (Math.cos(t) - (1 / Math.sqrt(2)));
  192. theta = Math.atan(tantheta);
  193. cosmu = Math.cos(mu);
  194. tannu = Math.tan(nu);
  195. cosphi = 1 - cosmu * cosmu * tannu * tannu * (1 - Math.cos(Math.atan(1 / Math.cos(theta))));
  196. if (cosphi < -1) {
  197. cosphi = -1;
  198. } else if (cosphi > +1) {
  199. cosphi = +1;
  200. }
  201. /* Apply the result to the real area on the cube face.
  202. * For the top and bottom face, we can compute phi and lam directly.
  203. * For the other faces, we must use unit sphere cartesian coordinates
  204. * as an intermediate step. */
  205. if (this.face === FACE_ENUM.TOP) {
  206. phi = Math.acos(cosphi);
  207. lp.phi = HALF_PI - phi;
  208. if (area.value === AREA_ENUM.AREA_0) {
  209. lp.lam = theta + HALF_PI;
  210. } else if (area.value === AREA_ENUM.AREA_1) {
  211. lp.lam = (theta < 0.0 ? theta + SPI : theta - SPI);
  212. } else if (area.value === AREA_ENUM.AREA_2) {
  213. lp.lam = theta - HALF_PI;
  214. } else /* area.value == AREA_ENUM.AREA_3 */ {
  215. lp.lam = theta;
  216. }
  217. } else if (this.face === FACE_ENUM.BOTTOM) {
  218. phi = Math.acos(cosphi);
  219. lp.phi = phi - HALF_PI;
  220. if (area.value === AREA_ENUM.AREA_0) {
  221. lp.lam = -theta + HALF_PI;
  222. } else if (area.value === AREA_ENUM.AREA_1) {
  223. lp.lam = -theta;
  224. } else if (area.value === AREA_ENUM.AREA_2) {
  225. lp.lam = -theta - HALF_PI;
  226. } else /* area.value == AREA_ENUM.AREA_3 */ {
  227. lp.lam = (theta < 0.0 ? -theta - SPI : -theta + SPI);
  228. }
  229. } else {
  230. /* Compute phi and lam via cartesian unit sphere coordinates. */
  231. var q, r, s;
  232. q = cosphi;
  233. t = q * q;
  234. if (t >= 1) {
  235. s = 0;
  236. } else {
  237. s = Math.sqrt(1 - t) * Math.sin(theta);
  238. }
  239. t += s * s;
  240. if (t >= 1) {
  241. r = 0;
  242. } else {
  243. r = Math.sqrt(1 - t);
  244. }
  245. /* Rotate q,r,s into the correct area. */
  246. if (area.value === AREA_ENUM.AREA_1) {
  247. t = r;
  248. r = -s;
  249. s = t;
  250. } else if (area.value === AREA_ENUM.AREA_2) {
  251. r = -r;
  252. s = -s;
  253. } else if (area.value === AREA_ENUM.AREA_3) {
  254. t = r;
  255. r = s;
  256. s = -t;
  257. }
  258. /* Rotate q,r,s into the correct cube face. */
  259. if (this.face === FACE_ENUM.RIGHT) {
  260. t = q;
  261. q = -r;
  262. r = t;
  263. } else if (this.face === FACE_ENUM.BACK) {
  264. q = -q;
  265. r = -r;
  266. } else if (this.face === FACE_ENUM.LEFT) {
  267. t = q;
  268. q = r;
  269. r = -t;
  270. }
  271. /* Now compute phi and lam from the unit sphere coordinates. */
  272. lp.phi = Math.acos(-s) - HALF_PI;
  273. lp.lam = Math.atan2(r, q);
  274. if (this.face === FACE_ENUM.RIGHT) {
  275. lp.lam = qsc_shift_lon_origin(lp.lam, -HALF_PI);
  276. } else if (this.face === FACE_ENUM.BACK) {
  277. lp.lam = qsc_shift_lon_origin(lp.lam, -SPI);
  278. } else if (this.face === FACE_ENUM.LEFT) {
  279. lp.lam = qsc_shift_lon_origin(lp.lam, +HALF_PI);
  280. }
  281. }
  282. /* Apply the shift from the sphere to the ellipsoid as described
  283. * in [LK12]. */
  284. if (this.es !== 0) {
  285. var invert_sign;
  286. var tanphi, xa;
  287. invert_sign = (lp.phi < 0 ? 1 : 0);
  288. tanphi = Math.tan(lp.phi);
  289. xa = this.b / Math.sqrt(tanphi * tanphi + this.one_minus_f_squared);
  290. lp.phi = Math.atan(Math.sqrt(this.a * this.a - xa * xa) / (this.one_minus_f * xa));
  291. if (invert_sign) {
  292. lp.phi = -lp.phi;
  293. }
  294. }
  295. lp.lam += this.long0;
  296. p.x = lp.lam;
  297. p.y = lp.phi;
  298. return p;
  299. }
  300. /* Helper function for forward projection: compute the theta angle
  301. * and determine the area number. */
  302. function qsc_fwd_equat_face_theta(phi, y, x, area) {
  303. var theta;
  304. if (phi < EPSLN) {
  305. area.value = AREA_ENUM.AREA_0;
  306. theta = 0.0;
  307. } else {
  308. theta = Math.atan2(y, x);
  309. if (Math.abs(theta) <= FORTPI) {
  310. area.value = AREA_ENUM.AREA_0;
  311. } else if (theta > FORTPI && theta <= HALF_PI + FORTPI) {
  312. area.value = AREA_ENUM.AREA_1;
  313. theta -= HALF_PI;
  314. } else if (theta > HALF_PI + FORTPI || theta <= -(HALF_PI + FORTPI)) {
  315. area.value = AREA_ENUM.AREA_2;
  316. theta = (theta >= 0.0 ? theta - SPI : theta + SPI);
  317. } else {
  318. area.value = AREA_ENUM.AREA_3;
  319. theta += HALF_PI;
  320. }
  321. }
  322. return theta;
  323. }
  324. /* Helper function: shift the longitude. */
  325. function qsc_shift_lon_origin(lon, offset) {
  326. var slon = lon + offset;
  327. if (slon < -SPI) {
  328. slon += TWO_PI;
  329. } else if (slon > +SPI) {
  330. slon -= TWO_PI;
  331. }
  332. return slon;
  333. }
  334. export var names = ["Quadrilateralized Spherical Cube", "Quadrilateralized_Spherical_Cube", "qsc"];
  335. export default {
  336. init: init,
  337. forward: forward,
  338. inverse: inverse,
  339. names: names
  340. };