get_ellipse.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 struct table
  24. {
  25. char *name;
  26. char *descr;
  27. double a;
  28. double e2;
  29. double f;
  30. } *table = NULL;
  31. static int count = -1;
  32. static char *PERMANENT = "PERMANENT";
  33. /* static int get_a_e2 (char *, char *, double *,double *); */
  34. static int get_a_e2_f(const char *, const char *, double *, double *,
  35. double *);
  36. void ellipsoid_table_file(char *);
  37. static int compare_table_names(const void *, const void *);
  38. static int read_ellipsoid_table(int);
  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. (void)read_ellipsoid_table(0);
  93. for (i = 0; i < count; i++) {
  94. if (G_strcasecmp(name, table[i].name) == 0) {
  95. *a = table[i].a;
  96. *e2 = table[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. (void)read_ellipsoid_table(0);
  117. return n >= 0 && n < count ? table[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. (void)read_ellipsoid_table(0);
  149. for (i = 0; i < count; i++) {
  150. if (G_strcasecmp(name, table[i].name) == 0) {
  151. *a = table[i].a;
  152. *e2 = table[i].e2;
  153. *f = table[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. (void)read_ellipsoid_table(0);
  174. return n >= 0 && n < count ? table[n].descr : NULL;
  175. }
  176. static int
  177. 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. void ellipsoid_table_file(char *file)
  212. {
  213. sprintf(file, "%s/etc/ellipse.table", G_gisbase());
  214. return;
  215. }
  216. static int compare_table_names(const void *pa, const void *pb)
  217. {
  218. const struct table *a = pa, *b = pb;
  219. /* return strcmp(a->name,b->name); */
  220. return G_strcasecmp(a->name, b->name);
  221. }
  222. static int read_ellipsoid_table(int fatal)
  223. {
  224. FILE *fd;
  225. char file[GPATH_MAX];
  226. char buf[1024];
  227. char name[100], descr[100], buf1[100], buf2[100];
  228. char badlines[256];
  229. int line;
  230. int err;
  231. if (count >= 0)
  232. return 1;
  233. count = 0;
  234. table = NULL;
  235. (void)ellipsoid_table_file(file);
  236. fd = fopen(file, "r");
  237. if (fd == NULL) {
  238. perror(file);
  239. sprintf(buf, _("Unable to open ellipsoid table file <%s>"), file);
  240. fatal ? G_fatal_error(buf) : G_warning(buf);
  241. return 0;
  242. }
  243. err = 0;
  244. *badlines = 0;
  245. for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
  246. G_strip(buf);
  247. if (*buf == 0 || *buf == '#')
  248. continue;
  249. if (sscanf(buf, "%s \"%99[^\"]\" %s %s", name, descr, buf1, buf2) !=
  250. 4) {
  251. err++;
  252. sprintf(buf, " %d", line);
  253. if (*badlines)
  254. strcat(badlines, ",");
  255. strcat(badlines, buf);
  256. continue;
  257. }
  258. table =
  259. (struct table *)G_realloc((char *)table,
  260. (count + 1) * sizeof(*table));
  261. table[count].name = G_store(name);
  262. table[count].descr = G_store(descr);
  263. if (get_a_e2_f
  264. (buf1, buf2, &table[count].a, &table[count].e2, &table[count].f)
  265. || get_a_e2_f(buf2, buf1, &table[count].a, &table[count].e2,
  266. &table[count].f))
  267. count++;
  268. else {
  269. err++;
  270. sprintf(buf, " %d", line);
  271. if (*badlines)
  272. strcat(badlines, ",");
  273. strcat(badlines, buf);
  274. continue;
  275. }
  276. }
  277. fclose(fd);
  278. if (!err) {
  279. /* over correct typed version */
  280. qsort(table, count, sizeof(*table), compare_table_names);
  281. return 1;
  282. }
  283. (fatal ? G_fatal_error : G_warning) ((err > 1)
  284. ?
  285. _("Lines%s of ellipsoid table file <%s> are invalid")
  286. :
  287. _("Line%s of ellipsoid table file <%s> is invalid"),
  288. badlines, file);
  289. return 0;
  290. }
  291. static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a,
  292. double *e2)
  293. {
  294. char *str, *str1;
  295. if (!proj_keys) {
  296. return -1;
  297. }
  298. if ((str = G_find_key_value("ellps", proj_keys)) != NULL) {
  299. if (strncmp(str, "sphere", 6) == 0) {
  300. str = G_find_key_value("a", proj_keys);
  301. if (str != NULL) {
  302. if (sscanf(str, "%lf", a) != 1) {
  303. G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
  304. str, PROJECTION_FILE, PERMANENT);
  305. }
  306. }
  307. else {
  308. *a = 6370997.0;
  309. }
  310. *e2 = 0.0;
  311. return 0;
  312. }
  313. else {
  314. if (G_get_ellipsoid_by_name(str, a, e2) == 0) {
  315. G_fatal_error(_("Invalid ellipsoid '%s' in file %s in <%s>"),
  316. str, PROJECTION_FILE, PERMANENT);
  317. }
  318. else {
  319. return 1;
  320. }
  321. }
  322. }
  323. else {
  324. str = G_find_key_value("a", proj_keys);
  325. str1 = G_find_key_value("es", proj_keys);
  326. if ((str != NULL) && (str1 != NULL)) {
  327. if (sscanf(str, "%lf", a) != 1) {
  328. G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
  329. str, PROJECTION_FILE, PERMANENT);
  330. }
  331. if (sscanf(str1, "%lf", e2) != 1) {
  332. G_fatal_error(_("Invalid es: field '%s' in file %s in <%s>"),
  333. str, PROJECTION_FILE, PERMANENT);
  334. }
  335. return 1;
  336. }
  337. else {
  338. str = G_find_key_value("proj", proj_keys);
  339. if ((str == NULL) || (strcmp(str, "ll") == 0)) {
  340. *a = 6378137.0;
  341. *e2 = .006694385;
  342. return 0;
  343. }
  344. else {
  345. G_fatal_error(_("No ellipsoid info given in file %s in <%s>"),
  346. PROJECTION_FILE, PERMANENT);
  347. }
  348. }
  349. }
  350. return 1;
  351. }