/Proj4/pj_utils.c

http://github.com/route-me/route-me · C · 175 lines · 80 code · 26 blank · 69 comment · 24 complexity · 7ebfb18201086bc086a83a0071ce088d MD5 · raw file

  1. /******************************************************************************
  2. * $Id: pj_utils.c,v 1.5 2007/03/12 14:05:35 fwarmerdam Exp $
  3. *
  4. * Project: PROJ.4
  5. * Purpose: Some utility functions we don't want to bother putting in
  6. * their own source files.
  7. * Author: Frank Warmerdam, warmerdam@pobox.com
  8. *
  9. ******************************************************************************
  10. * Copyright (c) 2001, Frank Warmerdam
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a
  13. * copy of this software and associated documentation files (the "Software"),
  14. * to deal in the Software without restriction, including without limitation
  15. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  16. * and/or sell copies of the Software, and to permit persons to whom the
  17. * Software is furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included
  20. * in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. * DEALINGS IN THE SOFTWARE.
  29. ******************************************************************************
  30. *
  31. * $Log: pj_utils.c,v $
  32. * Revision 1.5 2007/03/12 14:05:35 fwarmerdam
  33. * Removed duplicate towgs84 definition code.
  34. *
  35. * Revision 1.4 2005/07/06 14:04:09 fwarmerdam
  36. * Improved precision of es encoding for pj_latlong_from_proj() per:
  37. * http://bugzilla.remotesensing.org/show_bug.cgi?id=881
  38. *
  39. * Revision 1.3 2002/12/14 20:14:00 warmerda
  40. * added pj_is_geocent, added +pm support to pj_get_def
  41. *
  42. * Revision 1.2 2002/04/30 17:01:51 warmerda
  43. * Removed printf() statement.
  44. *
  45. * Revision 1.1 2001/04/05 04:22:46 warmerda
  46. * New
  47. *
  48. */
  49. #define PJ_LIB__
  50. #include "projects.h"
  51. #include <string.h>
  52. #include <math.h>
  53. /************************************************************************/
  54. /* pj_is_latlong() */
  55. /* */
  56. /* Returns TRUE if this coordinate system object is */
  57. /* geographic. */
  58. /************************************************************************/
  59. int pj_is_latlong( PJ *pj )
  60. {
  61. return pj == NULL || pj->is_latlong;
  62. }
  63. /************************************************************************/
  64. /* pj_is_geocent() */
  65. /* */
  66. /* Returns TRUE if this coordinate system object is geocentric. */
  67. /************************************************************************/
  68. int pj_is_geocent( PJ *pj )
  69. {
  70. return pj != NULL && pj->is_geocent;
  71. }
  72. /************************************************************************/
  73. /* pj_latlong_from_proj() */
  74. /* */
  75. /* Return a PJ* definition defining the lat/long coordinate */
  76. /* system on which a projection is based. If the coordinate */
  77. /* system passed in is latlong, a clone of the same will be */
  78. /* returned. */
  79. /************************************************************************/
  80. PJ *pj_latlong_from_proj( PJ *pj_in )
  81. {
  82. char defn[512];
  83. int got_datum = FALSE;
  84. pj_errno = 0;
  85. strcpy( defn, "+proj=latlong" );
  86. if( pj_param(pj_in->params, "tdatum").i )
  87. {
  88. got_datum = TRUE;
  89. sprintf( defn+strlen(defn), " +datum=%s",
  90. pj_param(pj_in->params,"sdatum").s );
  91. }
  92. else if( pj_param(pj_in->params, "tellps").i )
  93. {
  94. sprintf( defn+strlen(defn), " +ellps=%s",
  95. pj_param(pj_in->params,"sellps").s );
  96. }
  97. else if( pj_param(pj_in->params, "ta").i )
  98. {
  99. sprintf( defn+strlen(defn), " +a=%s",
  100. pj_param(pj_in->params,"sa").s );
  101. if( pj_param(pj_in->params, "tb").i )
  102. sprintf( defn+strlen(defn), " +b=%s",
  103. pj_param(pj_in->params,"sb").s );
  104. else if( pj_param(pj_in->params, "tes").i )
  105. sprintf( defn+strlen(defn), " +es=%s",
  106. pj_param(pj_in->params,"ses").s );
  107. else if( pj_param(pj_in->params, "tf").i )
  108. sprintf( defn+strlen(defn), " +f=%s",
  109. pj_param(pj_in->params,"sf").s );
  110. else
  111. sprintf( defn+strlen(defn), " +es=%.16g",
  112. pj_in->es );
  113. }
  114. else
  115. {
  116. pj_errno = -13;
  117. return NULL;
  118. }
  119. if( !got_datum )
  120. {
  121. if( pj_param(pj_in->params, "ttowgs84").i )
  122. sprintf( defn+strlen(defn), " +towgs84=%s",
  123. pj_param(pj_in->params,"stowgs84").s );
  124. if( pj_param(pj_in->params, "tnadgrids").i )
  125. sprintf( defn+strlen(defn), " +nadgrids=%s",
  126. pj_param(pj_in->params,"snadgrids").s );
  127. }
  128. /* copy over some other information related to ellipsoid */
  129. if( pj_param(pj_in->params, "tR").i )
  130. sprintf( defn+strlen(defn), " +R=%s",
  131. pj_param(pj_in->params,"sR").s );
  132. if( pj_param(pj_in->params, "tR_A").i )
  133. sprintf( defn+strlen(defn), " +R_A" );
  134. if( pj_param(pj_in->params, "tR_V").i )
  135. sprintf( defn+strlen(defn), " +R_V" );
  136. if( pj_param(pj_in->params, "tR_a").i )
  137. sprintf( defn+strlen(defn), " +R_a" );
  138. if( pj_param(pj_in->params, "tR_lat_a").i )
  139. sprintf( defn+strlen(defn), " +R_lat_a=%s",
  140. pj_param(pj_in->params,"sR_lat_a").s );
  141. if( pj_param(pj_in->params, "tR_lat_g").i )
  142. sprintf( defn+strlen(defn), " +R_lat_g=%s",
  143. pj_param(pj_in->params,"sR_lat_g").s );
  144. /* copy over prime meridian */
  145. if( pj_param(pj_in->params, "tpm").i )
  146. sprintf( defn+strlen(defn), " +pm=%s",
  147. pj_param(pj_in->params,"spm").s );
  148. return pj_init_plus( defn );
  149. }