proj3.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <string.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. static int lookup(const char *, const char *, char *, int);
  5. static int equal(const char *, const char *);
  6. static int lower(char);
  7. /*!
  8. * \brief database units
  9. *
  10. * Returns a
  11. * string describing the database grid units. It returns a plural form (eg. feet)
  12. * if <b>plural</b> is true. Otherwise it returns a singular form (eg. foot).
  13. *
  14. * \param plural
  15. * \return char *
  16. */
  17. char *G_database_unit_name(int plural)
  18. {
  19. int n;
  20. static char name[256];
  21. switch (n = G_projection()) {
  22. case PROJECTION_XY:
  23. case PROJECTION_UTM:
  24. case PROJECTION_LL:
  25. case PROJECTION_SP:
  26. return G__unit_name(G__projection_units(n), plural);
  27. }
  28. if (!lookup(UNIT_FILE, plural ? "units" : "unit", name, sizeof(name)))
  29. strcpy(name, plural ? "units" : "unit");
  30. return name;
  31. }
  32. /*!
  33. * \brief query cartographic projection
  34. *
  35. * Returns a pointer to a string which is a printable name for
  36. * projection code <b>proj</b> (as returned by <i>G_projection</i>). Returns
  37. * NULL if <b>proj</b> is not a valid projection.
  38. *
  39. * \param proj
  40. * \return char *
  41. */
  42. char *G_database_projection_name(void)
  43. {
  44. int n;
  45. static char name[256];
  46. switch (n = G_projection()) {
  47. case PROJECTION_XY:
  48. case PROJECTION_UTM:
  49. case PROJECTION_LL:
  50. case PROJECTION_SP:
  51. return G__projection_name(n);
  52. }
  53. if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
  54. strcpy(name, _("Unknown projection"));
  55. return name;
  56. }
  57. /*!
  58. * \brief conversion to meters
  59. *
  60. * Returns a factor which converts the grid unit to meters (by
  61. * multiplication). If the database is not metric (eg. imagery) then 0.0 is
  62. * returned.
  63. *
  64. * \param void
  65. * \return double
  66. */
  67. double G_database_units_to_meters_factor(void)
  68. {
  69. char *unit;
  70. double factor;
  71. char buf[256];
  72. int n;
  73. static struct
  74. {
  75. char *unit;
  76. double factor;
  77. } table[] = {
  78. {"unit", 1.0},
  79. {"meter", 1.0},
  80. {"foot", .3048},
  81. {"inch", .0254},
  82. {NULL, 0.0}
  83. };
  84. factor = 0.0;
  85. if (lookup(UNIT_FILE, "meters", buf, sizeof(buf)))
  86. sscanf(buf, "%lf", &factor);
  87. if (factor <= 0.0) {
  88. unit = G_database_unit_name(0);
  89. for (n = 0; table[n].unit; n++)
  90. if (equal(unit, table[n].unit)) {
  91. factor = table[n].factor;
  92. break;
  93. }
  94. }
  95. return factor;
  96. }
  97. /***********************************************************************
  98. * G_database_datum_name(void)
  99. *
  100. * return name of datum of current database
  101. *
  102. * returns pointer to valid name if ok
  103. * NULL otherwise
  104. ***********************************************************************/
  105. /*!
  106. * \brief get datum name for database
  107. *
  108. * Returns a pointer to the name of the map datum of the current database. If
  109. * there is no map datum explicitely associated with the acutal database, the
  110. * standard map datum WGS84 is returned, on error a NULL pointer is returned.
  111. *
  112. * \return char *
  113. */
  114. char *G_database_datum_name(void)
  115. {
  116. static char name[256], params[256];
  117. struct Key_Value *projinfo;
  118. int datumstatus;
  119. if (lookup(PROJECTION_FILE, "datum", name, sizeof(name)))
  120. return name;
  121. else if ((projinfo = G_get_projinfo()) == NULL)
  122. return NULL;
  123. else
  124. datumstatus = G_get_datumparams_from_projinfo(projinfo, name, params);
  125. G_free_key_value(projinfo);
  126. if (datumstatus == 2)
  127. return params;
  128. else
  129. return NULL;
  130. }
  131. /***********************************************************************
  132. * G_database_ellipse_name(void)
  133. *
  134. * return name of ellipsoid of current database
  135. *
  136. * returns pointer to valid name if ok
  137. * NULL otherwise
  138. ***********************************************************************/
  139. char *G_database_ellipse_name(void)
  140. {
  141. static char name[256];
  142. if (!lookup(PROJECTION_FILE, "ellps", name, sizeof(name))) {
  143. double a, es;
  144. G_get_ellipsoid_parameters(&a, &es);
  145. sprintf(name, "a=%.16g es=%.16g", a, es);
  146. }
  147. /* strcpy (name, "Unknown ellipsoid"); */
  148. return name;
  149. }
  150. static int lookup(const char *file, const char *key, char *value, int len)
  151. {
  152. char path[GPATH_MAX];
  153. /*
  154. G__file_name (path, "", file, G_mapset());
  155. if (access(path,0) == 0)
  156. return G_lookup_key_value_from_file(path, key, value, len) == 1;
  157. */
  158. G__file_name(path, "", file, "PERMANENT");
  159. return G_lookup_key_value_from_file(path, key, value, len) == 1;
  160. }
  161. static int equal(const char *a, const char *b)
  162. {
  163. if (a == NULL || b == NULL)
  164. return a == b;
  165. while (*a && *b)
  166. if (lower(*a++) != lower(*b++))
  167. return 0;
  168. if (*a || *b)
  169. return 0;
  170. return 1;
  171. }
  172. static int lower(char c)
  173. {
  174. if (c >= 'A' && c <= 'Z')
  175. c += 'a' - 'A';
  176. return c;
  177. }