get_ellipse.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. } table;
  36. /* static int get_a_e2 (char *, char *, double *,double *); */
  37. static int get_a_e2_f(const char *, const char *, double *, double *, double *);
  38. static int compare_ellipse_names(const void *, const void *);
  39. static int get_ellipsoid_parameters(struct Key_Value *, double *, double *);
  40. /*!
  41. * \brief get ellipsoid parameters
  42. *
  43. * This routine returns the semi-major axis <b>a</b> (in meters) and
  44. * the eccentricity squared <b>e2</b> for the ellipsoid associated
  45. * with the database. If there is no ellipsoid explicitly associated
  46. * with the database, it returns the values for the WGS 84 ellipsoid.
  47. *
  48. * \param[out] a semi-major axis
  49. * \param[out] e2 eccentricity squared
  50. *
  51. * \return 1 success
  52. * \return 0 default values used
  53. */
  54. int G_get_ellipsoid_parameters(double *a, double *e2)
  55. {
  56. int in_stat, stat;
  57. char ipath[GPATH_MAX];
  58. struct Key_Value *proj_keys;
  59. proj_keys = NULL;
  60. G__file_name(ipath, "", PROJECTION_FILE, PERMANENT);
  61. if (access(ipath, 0) != 0) {
  62. *a = 6378137.0;
  63. *e2 = .006694385;
  64. return 0;
  65. }
  66. proj_keys = G_read_key_value_file(ipath, &in_stat);
  67. if (in_stat != 0) {
  68. G_fatal_error(_("Unable to open file %s in <%s>"),
  69. PROJECTION_FILE, PERMANENT);
  70. }
  71. stat = get_ellipsoid_parameters(proj_keys, a, e2);
  72. G_free_key_value(proj_keys);
  73. return stat;
  74. }
  75. /*!
  76. * \brief get ellipsoid parameters by name
  77. *
  78. * This routine returns the semi-major axis <b>a</b> (in meters) and
  79. * eccentricity squared <b>e2</b> for the named ellipsoid. Returns 1
  80. * if <b>name</b> is a known ellipsoid, 0 otherwise.
  81. *
  82. * \param[in] name ellipsoid name
  83. * \param[out] a semi-major axis
  84. * \param[out] e2 eccentricity squared
  85. *
  86. * \return 1 on success
  87. * \return 0 if ellipsoid not found
  88. */
  89. int G_get_ellipsoid_by_name(const char *name, double *a, double *e2)
  90. {
  91. int i;
  92. G_read_ellipsoid_table(0);
  93. for (i = 0; i < table.count; i++) {
  94. if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
  95. *a = table.ellipses[i].a;
  96. *e2 = table.ellipses[i].e2;
  97. return 1;
  98. }
  99. }
  100. return 0;
  101. }
  102. /*!
  103. * \brief get ellipsoid name
  104. *
  105. * This function returns a pointer to the short name for the
  106. * <b>n</b><i>th</i> ellipsoid. If <b>n</b> is less than 0 or greater
  107. * than the number of known ellipsoids, it returns a NULL pointer.
  108. *
  109. * \param[in] n ellipsoid identificator
  110. *
  111. * \return char * ellipsoid name
  112. * \return NULL if no ellipsoid found
  113. */
  114. char *G_ellipsoid_name(int n)
  115. {
  116. G_read_ellipsoid_table(0);
  117. return n >= 0 && n < table.count ? table.ellipses[n].name : NULL;
  118. }
  119. /*
  120. * new 05/2000 by al: for datum shift the f parameter is needed too.
  121. * this all is not a clean design, but it keeps backward-
  122. * compatibility.
  123. * looks up ellipsoid in ellipsoid table and returns the
  124. * a, e2 and f parameters for the ellipsoid
  125. *
  126. * returns 1 if ok,
  127. * 0 if not found in table
  128. */
  129. /*!
  130. * \brief get spheroid parameters by name
  131. *
  132. * This function returns the semi-major axis <b>a</b> (in meters), the
  133. * eccentricity squared <b>e2</b> and the inverse flattening <b>f</b>
  134. * for the named ellipsoid. Returns 1 if <b>name</b> is a known
  135. * ellipsoid, 0 otherwise.
  136. *
  137. * \param[in] name spheroid name
  138. * \param[out] a semi-major axis
  139. * \param[out] e2 eccentricity squared
  140. * \param[out] f inverse flattening
  141. *
  142. * \return 1 on success
  143. * \return 0 if no spheroid found
  144. */
  145. int G_get_spheroid_by_name(const char *name, double *a, double *e2, double *f)
  146. {
  147. int i;
  148. G_read_ellipsoid_table(0);
  149. for (i = 0; i < table.count; i++) {
  150. if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
  151. *a = table.ellipses[i].a;
  152. *e2 = table.ellipses[i].e2;
  153. *f = table.ellipses[i].f;
  154. return 1;
  155. }
  156. }
  157. return 0;
  158. }
  159. /*!
  160. * \brief get description for <b>n</b><i>th</i> ellipsoid
  161. *
  162. * This function returns a pointer to the description text for the
  163. * <b>n</b><i>th</i> ellipsoid. If <b>n</b> is less than 0 or greater
  164. * than the number of known ellipsoids, it returns a NULL pointer.
  165. *
  166. * \param[in] n ellipsoid identificator
  167. *
  168. * \return pointer to ellipsoid description
  169. * \return NULL if no ellipsoid found
  170. */
  171. char *G_ellipsoid_description(int n)
  172. {
  173. G_read_ellipsoid_table(0);
  174. return n >= 0 && n < table.count ? table.ellipses[n].descr : NULL;
  175. }
  176. static int get_a_e2_f(const char *s1, const char *s2, double *a, double *e2, double *f)
  177. {
  178. double b, recipf;
  179. if (sscanf(s1, "a=%lf", a) != 1)
  180. return 0;
  181. if (*a <= 0.0)
  182. return 0;
  183. if (sscanf(s2, "e=%lf", e2) == 1) {
  184. *f = (double)1.0 / -sqrt(((double)1.0 - *e2)) + (double)1.0;
  185. return (*e2 >= 0.0);
  186. }
  187. if (sscanf(s2, "f=1/%lf", f) == 1) {
  188. if (*f <= 0.0)
  189. return 0;
  190. recipf = (double)1.0 / (*f);
  191. *e2 = recipf + recipf - recipf * recipf;
  192. return (*e2 >= 0.0);
  193. }
  194. if (sscanf(s2, "b=%lf", &b) == 1) {
  195. if (b <= 0.0)
  196. return 0;
  197. if (b == *a) {
  198. *f = 0.0;
  199. *e2 = 0.0;
  200. }
  201. else {
  202. recipf = ((*a) - b) / (*a);
  203. *f = (double)1.0 / recipf;
  204. *e2 = recipf + recipf - recipf * recipf;
  205. }
  206. return (*e2 >= 0.0);
  207. }
  208. return 0;
  209. }
  210. static int compare_ellipse_names(const void *pa, const void *pb)
  211. {
  212. const struct ellipse *a = pa;
  213. const struct ellipse *b = pb;
  214. return G_strcasecmp(a->name, b->name);
  215. }
  216. int G_read_ellipsoid_table(int fatal)
  217. {
  218. FILE *fd;
  219. char file[GPATH_MAX];
  220. char buf[1024];
  221. char badlines[256];
  222. int line;
  223. int err;
  224. if (table.count > 0)
  225. return 1;
  226. sprintf(file, "%s/etc/ellipse.table", G_gisbase());
  227. fd = fopen(file, "r");
  228. if (fd == NULL) {
  229. (fatal ? G_fatal_error : G_warning)(_("Unable to open ellipsoid table file <%s>"), file);
  230. return 0;
  231. }
  232. err = 0;
  233. *badlines = 0;
  234. for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
  235. char name[100], descr[100], buf1[100], buf2[100];
  236. struct ellipse *e;
  237. G_strip(buf);
  238. if (*buf == 0 || *buf == '#')
  239. continue;
  240. if (sscanf(buf, "%s \"%99[^\"]\" %s %s", name, descr, buf1, buf2) != 4) {
  241. err++;
  242. sprintf(buf, " %d", line);
  243. if (*badlines)
  244. strcat(badlines, ",");
  245. strcat(badlines, buf);
  246. continue;
  247. }
  248. if (table.count >= table.size) {
  249. table.size += 60;
  250. table.ellipses = G_realloc(table.ellipses, table.size * sizeof(struct ellipse));
  251. }
  252. e = &table.ellipses[table.count];
  253. e->name = G_store(name);
  254. e->descr = G_store(descr);
  255. if (get_a_e2_f(buf1, buf2, &e->a, &e->e2, &e->f) ||
  256. get_a_e2_f(buf2, buf1, &e->a, &e->e2, &e->f))
  257. table.count++;
  258. else {
  259. err++;
  260. sprintf(buf, " %d", line);
  261. if (*badlines)
  262. strcat(badlines, ",");
  263. strcat(badlines, buf);
  264. continue;
  265. }
  266. }
  267. fclose(fd);
  268. if (!err) {
  269. /* over correct typed version */
  270. qsort(table.ellipses, table.count, sizeof(struct ellipse), compare_ellipse_names);
  271. return 1;
  272. }
  273. (fatal ? G_fatal_error : G_warning)(
  274. (err > 1)
  275. ? _("Lines%s of ellipsoid table file <%s> are invalid")
  276. : _("Line%s of ellipsoid table file <%s> is invalid"),
  277. badlines, file);
  278. return 0;
  279. }
  280. static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a, double *e2)
  281. {
  282. 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. }