ellipse.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. \file ellipse.c
  3. \brief GProj library - Functions for reading datum parameters from the location database
  4. \author Paul Kelly <paul-grass stjohnspoint.co.uk>
  5. (C) 2003-2008 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. **/
  10. #include <unistd.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <math.h> /* for sqrt() */
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. #include <grass/gprojects.h>
  18. #include "local_proto.h"
  19. static int get_a_e2_rf(const char *, const char *, double *, double *,
  20. double *);
  21. /**
  22. * This routine returns the ellipsoid parameters from the database.
  23. * If the PROJECTION_FILE exists in the PERMANENT mapset, read info from
  24. * that file, otherwise return WGS 84 values.
  25. *
  26. * \return 1 ok, 0 default values used.
  27. * Dies with diagnostic if there is an error
  28. **/
  29. int GPJ_get_ellipsoid_params(double *a, double *e2, double *rf)
  30. {
  31. int ret;
  32. struct Key_Value *proj_keys = G_get_projinfo();
  33. if (proj_keys == NULL)
  34. proj_keys = G_create_key_value();
  35. ret = GPJ__get_ellipsoid_params(proj_keys, a, e2, rf);
  36. G_free_key_value(proj_keys);
  37. return ret;
  38. }
  39. int
  40. GPJ__get_ellipsoid_params(struct Key_Value *proj_keys,
  41. double *a, double *e2, double *rf)
  42. {
  43. struct gpj_ellps estruct;
  44. struct gpj_datum dstruct;
  45. char *str, *str1, *str3;
  46. str = G_find_key_value("datum", proj_keys);
  47. if ((str != NULL) && (GPJ_get_datum_by_name(str, &dstruct) > 0)) {
  48. /* If 'datum' key is present, look up correct ellipsoid
  49. * from datum.table */
  50. str = G_store(dstruct.ellps);
  51. GPJ_free_datum(&dstruct);
  52. }
  53. else
  54. /* else use ellipsoid defined in PROJ_INFO */
  55. str = G_find_key_value("ellps", proj_keys);
  56. if (str != NULL) {
  57. if (GPJ_get_ellipsoid_by_name(str, &estruct) < 0) {
  58. G_fatal_error(_("Invalid ellipsoid <%s> in file"), str);
  59. }
  60. else {
  61. *a = estruct.a;
  62. *e2 = estruct.es;
  63. *rf = estruct.rf;
  64. GPJ_free_ellps(&estruct);
  65. return 1;
  66. }
  67. }
  68. else {
  69. str3 = G_find_key_value("a", proj_keys);
  70. if (str3 != NULL) {
  71. G_asprintf(&str, "a=%s", str3);
  72. if ((str3 = G_find_key_value("es", proj_keys)) != NULL)
  73. G_asprintf(&str1, "e=%s", str3);
  74. else if ((str3 = G_find_key_value("f", proj_keys)) != NULL)
  75. G_asprintf(&str1, "f=1/%s", str3);
  76. else if ((str3 = G_find_key_value("rf", proj_keys)) != NULL)
  77. G_asprintf(&str1, "f=1/%s", str3);
  78. else if ((str3 = G_find_key_value("b", proj_keys)) != NULL)
  79. G_asprintf(&str1, "b=%s", str3);
  80. else
  81. G_fatal_error(_("No secondary ellipsoid descriptor "
  82. "(rf, es or b) in file"));
  83. if (get_a_e2_rf(str, str1, a, e2, rf) == 0)
  84. G_fatal_error(_("Invalid ellipsoid descriptors "
  85. "(a, rf, es or b) in file"));
  86. return 1;
  87. }
  88. else {
  89. str = G_find_key_value("proj", proj_keys);
  90. if ((str == NULL) || (strcmp(str, "ll") == 0)) {
  91. *a = 6378137.0;
  92. *e2 = .006694385;
  93. *rf = 298.257223563;
  94. return 0;
  95. }
  96. else {
  97. G_fatal_error(_("No ellipsoid info given in file"));
  98. }
  99. }
  100. }
  101. return 1;
  102. }
  103. /**
  104. * \brief looks up ellipsoid in ellipsoid table and returns the
  105. * a, e2 parameters for the ellipsoid
  106. *
  107. * \return 1 if ok,
  108. * -1 if not found in table
  109. */
  110. int GPJ_get_ellipsoid_by_name(const char *name, struct gpj_ellps *estruct)
  111. {
  112. struct ellps_list *list, *listhead;
  113. list = listhead = read_ellipsoid_table(0);
  114. while (list != NULL) {
  115. if (G_strcasecmp(name, list->name) == 0) {
  116. estruct->name = G_store(list->name);
  117. estruct->longname = G_store(list->longname);
  118. estruct->a = list->a;
  119. estruct->es = list->es;
  120. estruct->rf = list->rf;
  121. free_ellps_list(listhead);
  122. return 1;
  123. }
  124. list = list->next;
  125. }
  126. free_ellps_list(listhead);
  127. return -1;
  128. }
  129. static int
  130. get_a_e2_rf(const char *s1, const char *s2, double *a, double *e2,
  131. double *recipf)
  132. {
  133. double b, f;
  134. if (sscanf(s1, "a=%lf", a) != 1)
  135. return 0;
  136. if (*a <= 0.0)
  137. return 0;
  138. if (sscanf(s2, "e=%lf", e2) == 1) {
  139. f = 1.0 - sqrt(1.0 - *e2);
  140. *recipf = 1.0 / f;
  141. return (*e2 >= 0.0);
  142. }
  143. if (sscanf(s2, "f=1/%lf", recipf) == 1) {
  144. if (*recipf <= 0.0)
  145. return 0;
  146. f = 1.0 / *recipf;
  147. *e2 = f * (2 - f);
  148. return (*e2 >= 0.0);
  149. }
  150. if (sscanf(s2, "b=%lf", &b) == 1) {
  151. if (b <= 0.0)
  152. return 0;
  153. if (b == *a) {
  154. f = 0.0;
  155. *e2 = 0.0;
  156. }
  157. else {
  158. f = (*a - b) / *a;
  159. *e2 = f * (2 - f);
  160. }
  161. *recipf = 1.0 / f;
  162. return (*e2 >= 0.0);
  163. }
  164. return 0;
  165. }
  166. struct ellps_list *read_ellipsoid_table(int fatal)
  167. {
  168. FILE *fd;
  169. char file[GPATH_MAX];
  170. char buf[4096];
  171. char name[100], descr[1024], buf1[1024], buf2[1024];
  172. char badlines[1024];
  173. int line;
  174. int err;
  175. struct ellps_list *current = NULL, *outputlist = NULL;
  176. double a, e2, rf;
  177. int count = 0;
  178. sprintf(file, "%s%s", G_gisbase(), ELLIPSOIDTABLE);
  179. fd = fopen(file, "r");
  180. if (!fd) {
  181. (fatal ? G_fatal_error : G_warning)(
  182. _("Unable to open ellipsoid table file <%s>"), file);
  183. return NULL;
  184. }
  185. err = 0;
  186. *badlines = 0;
  187. for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
  188. G_strip(buf);
  189. if (*buf == 0 || *buf == '#')
  190. continue;
  191. if (sscanf(buf, "%s \"%1023[^\"]\" %s %s", name, descr, buf1, buf2)
  192. != 4) {
  193. err++;
  194. sprintf(buf, " %d", line);
  195. if (*badlines)
  196. strcat(badlines, ",");
  197. strcat(badlines, buf);
  198. continue;
  199. }
  200. if (get_a_e2_rf(buf1, buf2, &a, &e2, &rf)
  201. || get_a_e2_rf(buf2, buf1, &a, &e2, &rf)) {
  202. if (current == NULL)
  203. current = outputlist = G_malloc(sizeof(struct ellps_list));
  204. else
  205. current = current->next = G_malloc(sizeof(struct ellps_list));
  206. current->name = G_store(name);
  207. current->longname = G_store(descr);
  208. current->a = a;
  209. current->es = e2;
  210. current->rf = rf;
  211. current->next = NULL;
  212. count++;
  213. }
  214. else {
  215. err++;
  216. sprintf(buf, " %d", line);
  217. if (*badlines)
  218. strcat(badlines, ",");
  219. strcat(badlines, buf);
  220. continue;
  221. }
  222. }
  223. fclose(fd);
  224. if (!err)
  225. return outputlist;
  226. (fatal ? G_fatal_error : G_warning)(
  227. err == 1
  228. ? _("Line%s of ellipsoid table file <%s> is invalid")
  229. : _("Lines%s of ellipsoid table file <%s> are invalid"),
  230. badlines, file);
  231. return outputlist;
  232. }
  233. void GPJ_free_ellps(struct gpj_ellps *estruct)
  234. {
  235. G_free(estruct->name);
  236. G_free(estruct->longname);
  237. return;
  238. }
  239. void free_ellps_list(struct ellps_list *elist)
  240. {
  241. struct ellps_list *old;
  242. while (elist != NULL) {
  243. G_free(elist->name);
  244. G_free(elist->longname);
  245. old = elist;
  246. elist = old->next;
  247. G_free(old);
  248. }
  249. return;
  250. }