get_ellipse.c 9.4 KB

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