proj3.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*!
  2. \file lib/gis/proj3.c
  3. \brief GIS Library - Projection support (database)
  4. (C) 2001-2014 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. /* TODO: the G_database_*() functions should be renamed to G_location_*()
  10. * because they apply to a GRASS location, not to a GRASS database */
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include <grass/glocale.h>
  14. static const char *lookup_proj(const char *);
  15. static const char *lookup_units(const char *);
  16. static const char *lookup_epsg();
  17. static int equal(const char *, const char *);
  18. static int lower(char);
  19. static int initialized;
  20. static struct Key_Value *proj_info, *proj_units, *proj_epsg;
  21. static void init(void)
  22. {
  23. if (G_is_initialized(&initialized))
  24. return;
  25. proj_info = G_get_projinfo();
  26. proj_units = G_get_projunits();
  27. proj_epsg = G_get_projepsg();
  28. G_initialize_done(&initialized);
  29. }
  30. /*!
  31. \brief Get units (localized) name for the current location
  32. Returns a string describing the database grid units. It returns a
  33. plural form (eg. 'feet') if <i>plural</i> is non-zero. Otherwise it
  34. returns a singular form (eg. 'foot').
  35. \param plural plural form if non-zero
  36. \return localized units name
  37. */
  38. const char *G_database_unit_name(int plural)
  39. {
  40. int units;
  41. units = G_database_unit();
  42. return G_get_units_name(units, plural, FALSE);
  43. }
  44. /*!
  45. \brief Get units id for the current location
  46. \return units id
  47. */
  48. int G_database_unit()
  49. {
  50. int units;
  51. const char *name;
  52. units = G_projection_units(G_projection());
  53. if (units == U_UNDEFINED) {
  54. name = lookup_units("unit");
  55. if (!name)
  56. return U_UNKNOWN;
  57. if (strcasecmp(name, "meter") == 0 || strcasecmp(name, "metre") == 0
  58. || strcasecmp(name, "meters") == 0 || strcasecmp(name, "metres") == 0)
  59. units = U_METERS;
  60. else if (strcasecmp(name, "kilometer") == 0 || strcasecmp(name, "kilometre") == 0
  61. || strcasecmp(name, "kilometers") == 0 || strcasecmp(name, "kilometres") == 0)
  62. units = U_KILOMETERS;
  63. else if (strcasecmp(name, "acre") == 0 || strcasecmp(name, "acres") == 0)
  64. units = U_ACRES;
  65. else if (strcasecmp(name, "hectare") == 0 || strcasecmp(name, "hectares") == 0)
  66. units = U_HECTARES;
  67. else if (strcasecmp(name, "mile") == 0 || strcasecmp(name, "miles") == 0)
  68. units = U_MILES;
  69. else if (strcasecmp(name, "foot") == 0 || strcasecmp(name, "feet") == 0)
  70. units = U_FEET;
  71. else if (strcasecmp(name, "foot_us") == 0 || strcasecmp(name, "foot_uss") == 0)
  72. units = U_USFEET;
  73. else if (strcasecmp(name, "degree") == 0 || strcasecmp(name, "degrees") == 0)
  74. units = U_DEGREES;
  75. else
  76. units = U_UNKNOWN;
  77. }
  78. return units;
  79. }
  80. /*!
  81. \brief Query cartographic projection for the current location
  82. Returns a pointer to a string which is a printable name for
  83. projection code <i>proj</i> (as returned by G_projection). Returns
  84. NULL if <i>proj</i> is not a valid projection.
  85. \return projection name
  86. */
  87. const char *G_database_projection_name(void)
  88. {
  89. int n;
  90. const char *name;
  91. switch (n = G_projection()) {
  92. case PROJECTION_XY:
  93. case PROJECTION_UTM:
  94. case PROJECTION_LL:
  95. return G_projection_name(n);
  96. }
  97. name = lookup_proj("name");
  98. if (!name)
  99. return _("Unknown projection");
  100. return name;
  101. }
  102. /*!
  103. \brief Conversion to meters
  104. Returns a factor which converts the grid unit to meters (by
  105. multiplication). If the database is not metric (eg. imagery) then
  106. 0.0 is returned.
  107. \return value
  108. */
  109. double G_database_units_to_meters_factor(void)
  110. {
  111. const char *unit;
  112. const char *buf;
  113. double factor;
  114. int n;
  115. /* TODO: sync with definitions in ../proj/units.table */
  116. static const struct
  117. {
  118. char *unit;
  119. double factor;
  120. } table[] = {
  121. {"unit", 1.0},
  122. {"meter", 1.0},
  123. {"foot", .3048},
  124. {"foot_us", 1200/3937.},
  125. {"inch", .0254},
  126. {NULL, 0.0}
  127. };
  128. factor = 0.0;
  129. buf = lookup_units("meters");
  130. if (buf)
  131. sscanf(buf, "%lf", &factor);
  132. if (factor <= 0.0) {
  133. unit = G_database_unit_name(0);
  134. for (n = 0; table[n].unit; n++)
  135. if (equal(unit, table[n].unit)) {
  136. factor = table[n].factor;
  137. break;
  138. }
  139. }
  140. return factor;
  141. }
  142. /*!
  143. \brief Get datum name for the current location
  144. Returns a pointer to the name of the map datum of the current
  145. database. If there is no map datum explicitly associated with the
  146. actual database, the standard map datum WGS84 is returned, on error
  147. a NULL pointer is returned.
  148. \return datum name
  149. */
  150. const char *G_database_datum_name(void)
  151. {
  152. const char *name;
  153. char buf[256], params[256];
  154. int datumstatus;
  155. name = lookup_proj("datum");
  156. if (name)
  157. return name;
  158. else if (!proj_info)
  159. return NULL;
  160. else
  161. datumstatus = G_get_datumparams_from_projinfo(proj_info, buf, params);
  162. if (datumstatus == 2)
  163. return G_store(params);
  164. else
  165. return NULL;
  166. }
  167. /*!
  168. \brief Get ellipsoid name for the current location
  169. \return pointer to valid name if ok
  170. \return NULL on error
  171. */
  172. const char *G_database_ellipse_name(void)
  173. {
  174. const char *name;
  175. name = lookup_proj("ellps");
  176. if (!name) {
  177. char buf[256];
  178. double a, es;
  179. G_get_ellipsoid_parameters(&a, &es);
  180. sprintf(buf, "a=%.16g es=%.16g", a, es);
  181. name = G_store(buf);
  182. }
  183. /* strcpy (name, "Unknown ellipsoid"); */
  184. return name;
  185. }
  186. /*!
  187. \brief Get EPGS code for the current location
  188. \return pointer to valid EPSG code on success
  189. \return NULL on error
  190. */
  191. const char *G_database_epsg_code(void)
  192. {
  193. return lookup_epsg();
  194. }
  195. const char *lookup_proj(const char *key)
  196. {
  197. init();
  198. return G_find_key_value(key, proj_info);
  199. }
  200. const char *lookup_units(const char *key)
  201. {
  202. init();
  203. return G_find_key_value(key, proj_units);
  204. }
  205. const char *lookup_epsg()
  206. {
  207. init();
  208. return G_find_key_value("epsg", proj_epsg);
  209. }
  210. int equal(const char *a, const char *b)
  211. {
  212. if (a == NULL || b == NULL)
  213. return a == b;
  214. while (*a && *b)
  215. if (lower(*a++) != lower(*b++))
  216. return 0;
  217. if (*a || *b)
  218. return 0;
  219. return 1;
  220. }
  221. int lower(char c)
  222. {
  223. if (c >= 'A' && c <= 'Z')
  224. c += 'a' - 'A';
  225. return c;
  226. }