/Proj4/pj_datum_set.c

http://github.com/route-me/route-me · C · 161 lines · 67 code · 22 blank · 72 comment · 36 complexity · 9c2b9a913d70034e3c20e30cd9fca7e0 MD5 · raw file

  1. /******************************************************************************
  2. * $Id: pj_datum_set.c,v 1.4 2007/11/29 21:06:50 fwarmerdam Exp $
  3. *
  4. * Project: PROJ.4
  5. * Purpose: Apply datum definition to PJ structure from initialization string.
  6. * Author: Frank Warmerdam, warmerda@home.com
  7. *
  8. ******************************************************************************
  9. * Copyright (c) 2000, Frank Warmerdam
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included
  19. * in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  24. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  27. * DEALINGS IN THE SOFTWARE.
  28. ******************************************************************************
  29. *
  30. * $Log: pj_datum_set.c,v $
  31. * Revision 1.4 2007/11/29 21:06:50 fwarmerdam
  32. * make sure we only look for 7 parameters
  33. *
  34. * Revision 1.3 2007/01/31 06:41:01 fwarmerdam
  35. * dont parse more datum parameters than we have room for in datum_params[]
  36. *
  37. * Revision 1.2 2001/04/04 21:13:21 warmerda
  38. * do arcsecond/radian and ppm datum parm transformation in pj_set_datum()
  39. *
  40. * Revision 1.1 2000/07/06 23:32:27 warmerda
  41. * New
  42. *
  43. */
  44. #include "projects.h"
  45. #include <string.h>
  46. /* SEC_TO_RAD = Pi/180/3600 */
  47. #define SEC_TO_RAD 4.84813681109535993589914102357e-6
  48. /************************************************************************/
  49. /* pj_datum_set() */
  50. /************************************************************************/
  51. int pj_datum_set(paralist *pl, PJ *projdef)
  52. {
  53. const char *name, *towgs84;
  54. projdef->datum_type = PJD_UNKNOWN;
  55. /* -------------------------------------------------------------------- */
  56. /* Is there a datum definition in the parameters list? If so, */
  57. /* add the defining values to the parameter list. Note that */
  58. /* this will append the ellipse definition as well as the */
  59. /* towgs84= and related parameters. It should also be pointed */
  60. /* out that the addition is permanent rather than temporary */
  61. /* like most other keyword expansion so that the ellipse */
  62. /* definition will last into the pj_ell_set() function called */
  63. /* after this one. */
  64. /* -------------------------------------------------------------------- */
  65. name = pj_param(pl,"sdatum").s;
  66. if( name != NULL )
  67. {
  68. paralist *curr;
  69. const char *s;
  70. int i;
  71. /* find the end of the list, so we can add to it */
  72. for (curr = pl; curr && curr->next ; curr = curr->next) {}
  73. /* find the datum definition */
  74. for (i = 0; (s = pj_datums[i].id) && strcmp(name, s) ; ++i) {}
  75. if (!s) { pj_errno = -9; return 1; }
  76. if( pj_datums[i].ellipse_id && strlen(pj_datums[i].ellipse_id) > 0 )
  77. {
  78. char entry[100];
  79. strcpy( entry, "ellps=" );
  80. strncat( entry, pj_datums[i].ellipse_id, 80 );
  81. if (curr)
  82. {
  83. curr->next = pj_mkparam(entry);
  84. curr = curr->next;
  85. }
  86. }
  87. if( pj_datums[i].defn && strlen(pj_datums[i].defn) > 0 )
  88. {
  89. if (curr)
  90. curr->next = pj_mkparam(pj_datums[i].defn);
  91. }
  92. }
  93. /* -------------------------------------------------------------------- */
  94. /* Check for nadgrids parameter. */
  95. /* -------------------------------------------------------------------- */
  96. if( pj_param(pl,"snadgrids").s != NULL )
  97. {
  98. /* We don't actually save the value separately. It will continue
  99. to exist int he param list for use in pj_apply_gridshift.c */
  100. projdef->datum_type = PJD_GRIDSHIFT;
  101. }
  102. /* -------------------------------------------------------------------- */
  103. /* Check for towgs84 parameter. */
  104. /* -------------------------------------------------------------------- */
  105. else if( (towgs84 = pj_param(pl,"stowgs84").s) != NULL )
  106. {
  107. int parm_count = 0;
  108. const char *s;
  109. memset( projdef->datum_params, 0, sizeof(double) * 7);
  110. /* parse out the parameters */
  111. for( s = towgs84; *s != '\0' && parm_count < 7; )
  112. {
  113. projdef->datum_params[parm_count++] = atof(s);
  114. while( *s != '\0' && *s != ',' )
  115. s++;
  116. if( *s == ',' )
  117. s++;
  118. }
  119. if( projdef->datum_params[3] != 0.0
  120. || projdef->datum_params[4] != 0.0
  121. || projdef->datum_params[5] != 0.0
  122. || projdef->datum_params[6] != 0.0 )
  123. {
  124. projdef->datum_type = PJD_7PARAM;
  125. /* transform from arc seconds to radians */
  126. projdef->datum_params[3] *= SEC_TO_RAD;
  127. projdef->datum_params[4] *= SEC_TO_RAD;
  128. projdef->datum_params[5] *= SEC_TO_RAD;
  129. /* transform from parts per million to scaling factor */
  130. projdef->datum_params[6] =
  131. (projdef->datum_params[6]/1000000.0) + 1;
  132. }
  133. else
  134. projdef->datum_type = PJD_3PARAM;
  135. /* Note that pj_init() will later switch datum_type to
  136. PJD_WGS84 if shifts are all zero, and ellipsoid is WGS84 or GRS80 */
  137. }
  138. return 0;
  139. }