get_ellipse.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*!
  2. \file gis/get_ellipse.c
  3. \brief GIS Library - Getting ellipsoid parameters from the database.
  4. This routine returns the ellipsoid parameters from the database.
  5. If the PROJECTION_FILE exists in the PERMANENT mapset, read info
  6. from that file, otherwise return WGS 84 values.
  7. New 05/2000 by al: for datum shift the f parameter is needed too.
  8. This all is not a clean design, but it keeps backward-
  9. compatibility.
  10. Looks up ellipsoid in ellipsoid table and returns the
  11. a, e2 and f parameters for the ellipsoid
  12. (C) 2001-2009 by the GRASS Development Team
  13. This program is free software under the GNU General Public License
  14. (>=v2). Read the file COPYING that comes with GRASS for details.
  15. \author CERL
  16. */
  17. #include <unistd.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <math.h> /* for sqrt() */
  22. #include <grass/gis.h>
  23. #include <grass/glocale.h>
  24. static const char PERMANENT[] = "PERMANENT";
  25. static struct table {
  26. struct ellipse
  27. {
  28. char *name;
  29. char *descr;
  30. double a;
  31. double e2;
  32. double f;
  33. } *ellipses;
  34. int count;
  35. int size;
  36. int initialized;
  37. } table;
  38. /* static int get_a_e2 (char *, char *, double *,double *); */
  39. static int get_a_e2_f(const char *, const char *, double *, double *, double *);
  40. static int compare_ellipse_names(const void *, const void *);
  41. static int get_ellipsoid_parameters(struct Key_Value *, double *, double *);
  42. /*!
  43. * \brief get ellipsoid parameters
  44. *
  45. * This routine returns the semi-major axis <b>a</b> (in meters) and
  46. * the eccentricity squared <b>e2</b> for the ellipsoid associated
  47. * with the database. If there is no ellipsoid explicitly associated
  48. * with the database, it returns the values for the WGS 84 ellipsoid.
  49. *
  50. * \param[out] a semi-major axis
  51. * \param[out] e2 eccentricity squared
  52. *
  53. * \return 1 success
  54. * \return 0 default values used
  55. */
  56. int G_get_ellipsoid_parameters(double *a, double *e2)
  57. {
  58. int in_stat, stat;
  59. char ipath[GPATH_MAX];
  60. struct Key_Value *proj_keys;
  61. proj_keys = NULL;
  62. G__file_name(ipath, "", PROJECTION_FILE, PERMANENT);
  63. if (access(ipath, 0) != 0) {
  64. *a = 6378137.0;
  65. *e2 = .006694385;
  66. return 0;
  67. }
  68. proj_keys = G_read_key_value_file(ipath, &in_stat);
  69. if (in_stat != 0) {
  70. G_fatal_error(_("Unable to open file %s in <%s>"),
  71. PROJECTION_FILE, PERMANENT);
  72. }
  73. stat = get_ellipsoid_parameters(proj_keys, a, e2);
  74. G_free_key_value(proj_keys);
  75. return stat;
  76. }
  77. /*!
  78. * \brief Get ellipsoid parameters by name
  79. *
  80. * This routine returns the semi-major axis <i>a</i> (in meters) and
  81. * eccentricity squared <i>e2</i> for the named ellipsoid.
  82. *
  83. * \param name ellipsoid name
  84. * \param[out] a semi-major axis
  85. * \param[out] e2 eccentricity squared
  86. *
  87. * \return 1 on success
  88. * \return 0 if ellipsoid not found
  89. */
  90. int G_get_ellipsoid_by_name(const char *name, double *a, double *e2)
  91. {
  92. int i;
  93. G_read_ellipsoid_table(0);
  94. for (i = 0; i < table.count; i++) {
  95. if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
  96. *a = table.ellipses[i].a;
  97. *e2 = table.ellipses[i].e2;
  98. return 1;
  99. }
  100. }
  101. return 0;
  102. }
  103. /*!
  104. * \brief Get ellipsoid name
  105. *
  106. * This function returns a pointer to the short name for the
  107. * <i>n</i><i>th</i> ellipsoid. If <i>n</i> is less than 0 or greater
  108. * than the number of known ellipsoids, it returns a NULL pointer.
  109. *
  110. * \param n ellipsoid identificator
  111. *
  112. * \return ellipsoid name
  113. * \return NULL if no ellipsoid found
  114. */
  115. const char *G_ellipsoid_name(int n)
  116. {
  117. G_read_ellipsoid_table(0);
  118. return n >= 0 && n < table.count ? table.ellipses[n].name : NULL;
  119. }
  120. /*!
  121. * \brief Get spheroid parameters by name
  122. *
  123. * This function returns the semi-major axis <i>a</i> (in meters), the
  124. * eccentricity squared <i>e2</i> and the inverse flattening <i>f</i>
  125. * for the named ellipsoid.
  126. *
  127. * \param name spheroid name
  128. * \param[out] a semi-major axis
  129. * \param[out] e2 eccentricity squared
  130. * \param[out] f inverse flattening
  131. *
  132. * \return 1 on success
  133. * \return 0 if no spheroid found
  134. */
  135. int G_get_spheroid_by_name(const char *name, double *a, double *e2, double *f)
  136. {
  137. int i;
  138. G_read_ellipsoid_table(0);
  139. for (i = 0; i < table.count; i++) {
  140. if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
  141. *a = table.ellipses[i].a;
  142. *e2 = table.ellipses[i].e2;
  143. *f = table.ellipses[i].f;
  144. return 1;
  145. }
  146. }
  147. return 0;
  148. }
  149. /*!
  150. * \brief Get description for nth ellipsoid
  151. *
  152. * This function returns a pointer to the description text for the
  153. * <i>n</i>th ellipsoid. If <i>n</i> is less than 0 or greater
  154. * than the number of known ellipsoids, it returns a NULL pointer.
  155. *
  156. * \param n ellipsoid identificator
  157. *
  158. * \return pointer to ellipsoid description
  159. * \return NULL if no ellipsoid found
  160. */
  161. const char *G_ellipsoid_description(int n)
  162. {
  163. G_read_ellipsoid_table(0);
  164. return n >= 0 && n < table.count ? table.ellipses[n].descr : NULL;
  165. }
  166. static int get_a_e2_f(const char *s1, const char *s2, double *a, double *e2, double *f)
  167. {
  168. double b, recipf;
  169. if (sscanf(s1, "a=%lf", a) != 1)
  170. return 0;
  171. if (*a <= 0.0)
  172. return 0;
  173. if (sscanf(s2, "e=%lf", e2) == 1) {
  174. *f = (double)1.0 / -sqrt(((double)1.0 - *e2)) + (double)1.0;
  175. return (*e2 >= 0.0);
  176. }
  177. if (sscanf(s2, "f=1/%lf", f) == 1) {
  178. if (*f <= 0.0)
  179. return 0;
  180. recipf = (double)1.0 / (*f);
  181. *e2 = recipf + recipf - recipf * recipf;
  182. return (*e2 >= 0.0);
  183. }
  184. if (sscanf(s2, "b=%lf", &b) == 1) {
  185. if (b <= 0.0)
  186. return 0;
  187. if (b == *a) {
  188. *f = 0.0;
  189. *e2 = 0.0;
  190. }
  191. else {
  192. recipf = ((*a) - b) / (*a);
  193. *f = (double)1.0 / recipf;
  194. *e2 = recipf + recipf - recipf * recipf;
  195. }
  196. return (*e2 >= 0.0);
  197. }
  198. return 0;
  199. }
  200. static int compare_ellipse_names(const void *pa, const void *pb)
  201. {
  202. const struct ellipse *a = pa;
  203. const struct ellipse *b = pb;
  204. return G_strcasecmp(a->name, b->name);
  205. }
  206. /*!
  207. \brief Read ellipsoid table
  208. \param fatal non-zero value for G_fatal_error(), otherwise
  209. G_warning() is used
  210. \return 1 on sucess
  211. \return 0 on error
  212. */
  213. int G_read_ellipsoid_table(int fatal)
  214. {
  215. FILE *fd;
  216. char file[GPATH_MAX];
  217. char buf[1024];
  218. char badlines[256];
  219. int line;
  220. int err;
  221. if (G_is_initialized(&table.initialized))
  222. return 1;
  223. sprintf(file, "%s/etc/ellipse.table", G_gisbase());
  224. fd = fopen(file, "r");
  225. if (fd == NULL) {
  226. (fatal ? G_fatal_error : G_warning)(_("Unable to open ellipsoid table file <%s>"), file);
  227. G_initialize_done(&table.initialized);
  228. return 0;
  229. }
  230. err = 0;
  231. *badlines = 0;
  232. for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
  233. char name[100], descr[100], buf1[100], buf2[100];
  234. struct ellipse *e;
  235. G_strip(buf);
  236. if (*buf == 0 || *buf == '#')
  237. continue;
  238. if (sscanf(buf, "%s \"%99[^\"]\" %s %s", name, descr, buf1, buf2) != 4) {
  239. err++;
  240. sprintf(buf, " %d", line);
  241. if (*badlines)
  242. strcat(badlines, ",");
  243. strcat(badlines, buf);
  244. continue;
  245. }
  246. if (table.count >= table.size) {
  247. table.size += 60;
  248. table.ellipses = G_realloc(table.ellipses, table.size * sizeof(struct ellipse));
  249. }
  250. e = &table.ellipses[table.count];
  251. e->name = G_store(name);
  252. e->descr = G_store(descr);
  253. if (get_a_e2_f(buf1, buf2, &e->a, &e->e2, &e->f) ||
  254. get_a_e2_f(buf2, buf1, &e->a, &e->e2, &e->f))
  255. table.count++;
  256. else {
  257. err++;
  258. sprintf(buf, " %d", line);
  259. if (*badlines)
  260. strcat(badlines, ",");
  261. strcat(badlines, buf);
  262. continue;
  263. }
  264. }
  265. fclose(fd);
  266. if (!err) {
  267. /* over correct typed version */
  268. qsort(table.ellipses, table.count, sizeof(struct ellipse), compare_ellipse_names);
  269. G_initialize_done(&table.initialized);
  270. return 1;
  271. }
  272. (fatal ? G_fatal_error : G_warning)(
  273. (err > 1)
  274. ? _("Lines%s of ellipsoid table file <%s> are invalid")
  275. : _("Line%s of ellipsoid table file <%s> is invalid"),
  276. badlines, file);
  277. G_initialize_done(&table.initialized);
  278. return 0;
  279. }
  280. static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a, double *e2)
  281. {
  282. const char *str, *str1;
  283. if (!proj_keys) {
  284. return -1;
  285. }
  286. if ((str = G_find_key_value("ellps", proj_keys)) != NULL) {
  287. if (strncmp(str, "sphere", 6) == 0) {
  288. str = G_find_key_value("a", proj_keys);
  289. if (str != NULL) {
  290. if (sscanf(str, "%lf", a) != 1)
  291. G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
  292. str, PROJECTION_FILE, PERMANENT);
  293. }
  294. else
  295. *a = 6370997.0;
  296. *e2 = 0.0;
  297. return 0;
  298. }
  299. else {
  300. if (G_get_ellipsoid_by_name(str, a, e2) == 0)
  301. G_fatal_error(_("Invalid ellipsoid '%s' in file %s in <%s>"),
  302. str, PROJECTION_FILE, PERMANENT);
  303. else
  304. return 1;
  305. }
  306. }
  307. else {
  308. str = G_find_key_value("a", proj_keys);
  309. str1 = G_find_key_value("es", proj_keys);
  310. if ((str != NULL) && (str1 != NULL)) {
  311. if (sscanf(str, "%lf", a) != 1)
  312. G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
  313. str, PROJECTION_FILE, PERMANENT);
  314. if (sscanf(str1, "%lf", e2) != 1)
  315. G_fatal_error(_("Invalid es: field '%s' in file %s in <%s>"),
  316. str, PROJECTION_FILE, PERMANENT);
  317. return 1;
  318. }
  319. else {
  320. str = G_find_key_value("proj", proj_keys);
  321. if ((str == NULL) || (strcmp(str, "ll") == 0)) {
  322. *a = 6378137.0;
  323. *e2 = .006694385;
  324. return 0;
  325. }
  326. else
  327. G_fatal_error(_("No ellipsoid info given in file %s in <%s>"),
  328. PROJECTION_FILE, PERMANENT);
  329. }
  330. }
  331. return 1;
  332. }