proj3.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*!
  2. \file gis/proj3.c
  3. \brief GIS Library - Projection support (database)
  4. (C) 2001-2010 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <string.h>
  10. #include <grass/gis.h>
  11. #include <grass/glocale.h>
  12. static const char *lookup_proj(const char *);
  13. static const char *lookup_units(const char *);
  14. static int equal(const char *, const char *);
  15. static int lower(char);
  16. static int initialized;
  17. static struct Key_Value *proj_info, *proj_units;
  18. static void init(void)
  19. {
  20. if (G_is_initialized(&initialized))
  21. return;
  22. proj_info = G_get_projinfo();
  23. proj_units = G_get_projunits();
  24. G_initialize_done(&initialized);
  25. }
  26. /*!
  27. \brief Get database units (localized) name
  28. Returns a string describing the database grid units. It returns a
  29. plural form (eg. 'feet') if <i>plural</i> is non-zero. Otherwise it
  30. returns a singular form (eg. 'foot').
  31. \param plural plural form if non-zero
  32. \return localized units name
  33. */
  34. const char *G_database_unit_name(int plural)
  35. {
  36. int units;
  37. const char *name;
  38. units = G__projection_units(G_projection());
  39. if (units == U_UNDEFINED) {
  40. name = lookup_units(plural ? "units" : "unit");
  41. if (!name)
  42. return plural ? _("units") : _("unit");
  43. if (strcasecmp(name, "meter") == 0 || strcasecmp(name, "meters") == 0)
  44. units = U_METERS;
  45. else if (strcasecmp(name, "kilometer") == 0 || strcasecmp(name, "kilometers") == 0)
  46. units = U_KILOMETERS;
  47. else if (strcasecmp(name, "acre") == 0 || strcasecmp(name, "acres") == 0)
  48. units = U_ACRES;
  49. else if (strcasecmp(name, "hectare") == 0 || strcasecmp(name, "hectares") == 0)
  50. units = U_HECTARES;
  51. else if (strcasecmp(name, "mile") == 0 || strcasecmp(name, "miles") == 0)
  52. units = U_MILES;
  53. else if (strcasecmp(name, "foot") == 0 || strcasecmp(name, "feet") == 0)
  54. units = U_FEET;
  55. else if (strcasecmp(name, "degree") == 0 || strcasecmp(name, "degrees") == 0)
  56. units = U_DEGREES;
  57. else
  58. units = U_UNDEFINED;
  59. }
  60. return G_get_units_name(units, plural, FALSE);
  61. }
  62. /*!
  63. \brief Query cartographic projection
  64. Returns a pointer to a string which is a printable name for
  65. projection code <i>proj</i> (as returned by G_projection). Returns
  66. NULL if <i>proj</i> is not a valid projection.
  67. \return projection name
  68. */
  69. const char *G_database_projection_name(void)
  70. {
  71. int n;
  72. const char *name;
  73. switch (n = G_projection()) {
  74. case PROJECTION_XY:
  75. case PROJECTION_UTM:
  76. case PROJECTION_LL:
  77. case PROJECTION_SP:
  78. return G__projection_name(n);
  79. }
  80. name = lookup_proj("name");
  81. if (!name)
  82. return _("Unknown projection");
  83. return name;
  84. }
  85. /*!
  86. \brief Conversion to meters
  87. Returns a factor which converts the grid unit to meters (by
  88. multiplication). If the database is not metric (eg. imagery) then
  89. 0.0 is returned.
  90. \return value
  91. */
  92. double G_database_units_to_meters_factor(void)
  93. {
  94. const char *unit;
  95. const char *buf;
  96. double factor;
  97. int n;
  98. static const struct
  99. {
  100. char *unit;
  101. double factor;
  102. } table[] = {
  103. {"unit", 1.0},
  104. {"meter", 1.0},
  105. {"foot", .3048},
  106. {"inch", .0254},
  107. {NULL, 0.0}
  108. };
  109. factor = 0.0;
  110. buf = lookup_units("meters");
  111. if (buf)
  112. sscanf(buf, "%lf", &factor);
  113. if (factor <= 0.0) {
  114. unit = G_database_unit_name(0);
  115. for (n = 0; table[n].unit; n++)
  116. if (equal(unit, table[n].unit)) {
  117. factor = table[n].factor;
  118. break;
  119. }
  120. }
  121. return factor;
  122. }
  123. /*!
  124. \brief Get datum name for database
  125. Returns a pointer to the name of the map datum of the current
  126. database. If there is no map datum explicitely associated with the
  127. acutal database, the standard map datum WGS84 is returned, on error
  128. a NULL pointer is returned.
  129. \return datum name
  130. */
  131. const char *G_database_datum_name(void)
  132. {
  133. const char *name;
  134. char buf[256], params[256];
  135. int datumstatus;
  136. name = lookup_proj("datum");
  137. if (name)
  138. return name;
  139. else if (!proj_info)
  140. return NULL;
  141. else
  142. datumstatus = G_get_datumparams_from_projinfo(proj_info, buf, params);
  143. if (datumstatus == 2)
  144. return G_store(params);
  145. else
  146. return NULL;
  147. }
  148. /*!
  149. \brief Get ellipsoid name of current database
  150. \return pointer to valid name if ok
  151. \return NULL on error
  152. */
  153. const char *G_database_ellipse_name(void)
  154. {
  155. const char *name;
  156. name = lookup_proj("ellps");
  157. if (!name) {
  158. char buf[256];
  159. double a, es;
  160. G_get_ellipsoid_parameters(&a, &es);
  161. sprintf(buf, "a=%.16g es=%.16g", a, es);
  162. name = G_store(buf);
  163. }
  164. /* strcpy (name, "Unknown ellipsoid"); */
  165. return name;
  166. }
  167. static const char *lookup_proj(const char *key)
  168. {
  169. init();
  170. return G_find_key_value(key, proj_info);
  171. }
  172. static const char *lookup_units(const char *key)
  173. {
  174. init();
  175. return G_find_key_value(key, proj_units);
  176. }
  177. static int equal(const char *a, const char *b)
  178. {
  179. if (a == NULL || b == NULL)
  180. return a == b;
  181. while (*a && *b)
  182. if (lower(*a++) != lower(*b++))
  183. return 0;
  184. if (*a || *b)
  185. return 0;
  186. return 1;
  187. }
  188. static int lower(char c)
  189. {
  190. if (c >= 'A' && c <= 'Z')
  191. c += 'a' - 'A';
  192. return c;
  193. }