get_ellipse.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*!
  2. \file lib/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 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);
  69. stat = get_ellipsoid_parameters(proj_keys, a, e2);
  70. G_free_key_value(proj_keys);
  71. return stat;
  72. }
  73. /*!
  74. * \brief Get ellipsoid parameters by name
  75. *
  76. * This routine returns the semi-major axis <i>a</i> (in meters) and
  77. * eccentricity squared <i>e2</i> for the named ellipsoid.
  78. *
  79. * \param name ellipsoid name
  80. * \param[out] a semi-major axis
  81. * \param[out] e2 eccentricity squared
  82. *
  83. * \return 1 on success
  84. * \return 0 if ellipsoid not found
  85. */
  86. int G_get_ellipsoid_by_name(const char *name, double *a, double *e2)
  87. {
  88. int i;
  89. G_read_ellipsoid_table(0);
  90. for (i = 0; i < table.count; i++) {
  91. if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
  92. *a = table.ellipses[i].a;
  93. *e2 = table.ellipses[i].e2;
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }
  99. /*!
  100. * \brief Get ellipsoid name
  101. *
  102. * This function returns a pointer to the short name for the
  103. * <i>n</i><i>th</i> ellipsoid. If <i>n</i> is less than 0 or greater
  104. * than the number of known ellipsoids, it returns a NULL pointer.
  105. *
  106. * \param n ellipsoid identificator
  107. *
  108. * \return ellipsoid name
  109. * \return NULL if no ellipsoid found
  110. */
  111. const char *G_ellipsoid_name(int n)
  112. {
  113. G_read_ellipsoid_table(0);
  114. return n >= 0 && n < table.count ? table.ellipses[n].name : NULL;
  115. }
  116. /*!
  117. * \brief Get spheroid parameters by name
  118. *
  119. * This function returns the semi-major axis <i>a</i> (in meters), the
  120. * eccentricity squared <i>e2</i> and the inverse flattening <i>f</i>
  121. * for the named ellipsoid.
  122. *
  123. * \param name spheroid name
  124. * \param[out] a semi-major axis
  125. * \param[out] e2 eccentricity squared
  126. * \param[out] f inverse flattening
  127. *
  128. * \return 1 on success
  129. * \return 0 if no spheroid found
  130. */
  131. int G_get_spheroid_by_name(const char *name, double *a, double *e2, double *f)
  132. {
  133. int i;
  134. G_read_ellipsoid_table(0);
  135. for (i = 0; i < table.count; i++) {
  136. if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
  137. *a = table.ellipses[i].a;
  138. *e2 = table.ellipses[i].e2;
  139. *f = table.ellipses[i].f;
  140. return 1;
  141. }
  142. }
  143. return 0;
  144. }
  145. /*!
  146. * \brief Get description for nth ellipsoid
  147. *
  148. * This function returns a pointer to the description text for the
  149. * <i>n</i>th ellipsoid. If <i>n</i> is less than 0 or greater
  150. * than the number of known ellipsoids, it returns a NULL pointer.
  151. *
  152. * \param n ellipsoid identificator
  153. *
  154. * \return pointer to ellipsoid description
  155. * \return NULL if no ellipsoid found
  156. */
  157. const char *G_ellipsoid_description(int n)
  158. {
  159. G_read_ellipsoid_table(0);
  160. return n >= 0 && n < table.count ? table.ellipses[n].descr : NULL;
  161. }
  162. static int get_a_e2_f(const char *s1, const char *s2, double *a, double *e2, double *f)
  163. {
  164. double b, recipf;
  165. if (sscanf(s1, "a=%lf", a) != 1)
  166. return 0;
  167. if (*a <= 0.0)
  168. return 0;
  169. if (sscanf(s2, "e=%lf", e2) == 1) {
  170. *f = (double)1.0 / -sqrt(((double)1.0 - *e2)) + (double)1.0;
  171. return (*e2 >= 0.0);
  172. }
  173. if (sscanf(s2, "f=1/%lf", f) == 1) {
  174. if (*f <= 0.0)
  175. return 0;
  176. recipf = (double)1.0 / (*f);
  177. *e2 = recipf + recipf - recipf * recipf;
  178. return (*e2 >= 0.0);
  179. }
  180. if (sscanf(s2, "b=%lf", &b) == 1) {
  181. if (b <= 0.0)
  182. return 0;
  183. if (b == *a) {
  184. *f = 0.0;
  185. *e2 = 0.0;
  186. }
  187. else {
  188. recipf = ((*a) - b) / (*a);
  189. *f = (double)1.0 / recipf;
  190. *e2 = recipf + recipf - recipf * recipf;
  191. }
  192. return (*e2 >= 0.0);
  193. }
  194. return 0;
  195. }
  196. static int compare_ellipse_names(const void *pa, const void *pb)
  197. {
  198. const struct ellipse *a = pa;
  199. const struct ellipse *b = pb;
  200. return G_strcasecmp(a->name, b->name);
  201. }
  202. /*!
  203. \brief Read ellipsoid table
  204. \param fatal non-zero value for G_fatal_error(), otherwise
  205. G_warning() is used
  206. \return 1 on success
  207. \return 0 on error
  208. */
  209. int G_read_ellipsoid_table(int fatal)
  210. {
  211. FILE *fd;
  212. char file[GPATH_MAX];
  213. char buf[1024];
  214. char badlines[256];
  215. int line;
  216. int err;
  217. if (G_is_initialized(&table.initialized))
  218. return 1;
  219. sprintf(file, "%s/etc/proj/ellipse.table", G_gisbase());
  220. fd = fopen(file, "r");
  221. if (fd == NULL) {
  222. (fatal ? G_fatal_error : G_warning)(_("Unable to open ellipsoid table file <%s>"), file);
  223. G_initialize_done(&table.initialized);
  224. return 0;
  225. }
  226. err = 0;
  227. *badlines = 0;
  228. for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
  229. char name[100], descr[100], buf1[100], buf2[100];
  230. struct ellipse *e;
  231. G_strip(buf);
  232. if (*buf == 0 || *buf == '#')
  233. continue;
  234. if (sscanf(buf, "%s \"%99[^\"]\" %s %s", name, descr, buf1, buf2) != 4) {
  235. err++;
  236. sprintf(buf, " %d", line);
  237. if (*badlines)
  238. strcat(badlines, ",");
  239. strcat(badlines, buf);
  240. continue;
  241. }
  242. if (table.count >= table.size) {
  243. table.size += 60;
  244. table.ellipses = G_realloc(table.ellipses, table.size * sizeof(struct ellipse));
  245. }
  246. e = &table.ellipses[table.count];
  247. e->name = G_store(name);
  248. e->descr = G_store(descr);
  249. if (get_a_e2_f(buf1, buf2, &e->a, &e->e2, &e->f) ||
  250. get_a_e2_f(buf2, buf1, &e->a, &e->e2, &e->f))
  251. table.count++;
  252. else {
  253. err++;
  254. sprintf(buf, " %d", line);
  255. if (*badlines)
  256. strcat(badlines, ",");
  257. strcat(badlines, buf);
  258. continue;
  259. }
  260. }
  261. fclose(fd);
  262. if (!err) {
  263. /* over correct typed version */
  264. qsort(table.ellipses, table.count, sizeof(struct ellipse), compare_ellipse_names);
  265. G_initialize_done(&table.initialized);
  266. return 1;
  267. }
  268. (fatal ? G_fatal_error : G_warning)(
  269. n_(
  270. ("Line%s of ellipsoid table file <%s> is invalid"),
  271. ("Lines%s of ellipsoid table file <%s> are invalid"),
  272. err),
  273. badlines, file);
  274. G_initialize_done(&table.initialized);
  275. return 0;
  276. }
  277. static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a, double *e2)
  278. {
  279. const char *str, *str1;
  280. if (!proj_keys) {
  281. return -1;
  282. }
  283. if ((str = G_find_key_value("ellps", proj_keys)) != NULL) {
  284. if (strncmp(str, "sphere", 6) == 0) {
  285. str = G_find_key_value("a", proj_keys);
  286. if (str != NULL) {
  287. if (sscanf(str, "%lf", a) != 1)
  288. G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
  289. str, PROJECTION_FILE, PERMANENT);
  290. }
  291. else
  292. *a = 6370997.0;
  293. *e2 = 0.0;
  294. return 0;
  295. }
  296. else {
  297. if (G_get_ellipsoid_by_name(str, a, e2) == 0)
  298. G_fatal_error(_("Invalid ellipsoid '%s' in file %s in <%s>"),
  299. str, PROJECTION_FILE, PERMANENT);
  300. else
  301. return 1;
  302. }
  303. }
  304. else {
  305. str = G_find_key_value("a", proj_keys);
  306. str1 = G_find_key_value("es", proj_keys);
  307. if ((str != NULL) && (str1 != NULL)) {
  308. if (sscanf(str, "%lf", a) != 1)
  309. G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
  310. str, PROJECTION_FILE, PERMANENT);
  311. if (sscanf(str1, "%lf", e2) != 1)
  312. G_fatal_error(_("Invalid es: field '%s' in file %s in <%s>"),
  313. str, PROJECTION_FILE, PERMANENT);
  314. return 1;
  315. }
  316. else {
  317. str = G_find_key_value("proj", proj_keys);
  318. if ((str == NULL) || (strcmp(str, "ll") == 0)) {
  319. *a = 6378137.0;
  320. *e2 = .006694385;
  321. return 0;
  322. }
  323. else
  324. G_fatal_error(_("No ellipsoid info given in file %s in <%s>"),
  325. PROJECTION_FILE, PERMANENT);
  326. }
  327. }
  328. return 1;
  329. }