get_datum_name.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. *
  3. ****************************************************************************
  4. *
  5. * MODULE: GRASS 5.0 gis library, get_datum_name.c
  6. * AUTHOR(S): unknown, updated by Andreas Lange, andreas.lange@rhein-main.de
  7. * PURPOSE: Get datum name for new location database
  8. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. *****************************************************************************/
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. /***********************************************************************
  21. * G_ask_datum_name(char *datumname, char *ellpsname)
  22. *
  23. * ask interactively for a valid datum name
  24. *
  25. * returns <0 on error
  26. * returns 1 on success
  27. ***********************************************************************/
  28. /*!
  29. * \brief ask for a valid datum name
  30. *
  31. * This function asks the user interactively for a valid datum name from the datum
  32. * table. The datum name is stored in the character array pointed to by
  33. * <b>datum</b>. The function returns 1 on sucess, -1 if no datum was entered
  34. * on command line and 0 on internal error.
  35. *
  36. * \param datum
  37. * \return int
  38. */
  39. int G_ask_datum_name(char *datumname, char *ellpsname)
  40. {
  41. char buff[1024], answer[100], ellipse[100];
  42. char *dat, *Tmp_file;
  43. FILE *Tmp_fd = NULL;
  44. int i;
  45. for (;;) {
  46. do {
  47. fprintf(stderr, _("\nPlease specify datum name\n"));
  48. fprintf(stderr,
  49. _("Enter 'list' for the list of available datums\n"));
  50. fprintf(stderr,
  51. _
  52. ("or 'custom' if you wish to enter custom parameters\n"));
  53. fprintf(stderr, _("Hit RETURN to cancel request\n"));
  54. fprintf(stderr, ">");
  55. } while (!G_gets(answer));
  56. G_strip(answer);
  57. if (strlen(answer) == 0)
  58. return -1;
  59. if (strcmp(answer, "list") == 0) {
  60. Tmp_file = G_tempfile();
  61. if (NULL == (Tmp_fd = fopen(Tmp_file, "w")))
  62. G_warning(_("Cannot open temp file"));
  63. else {
  64. char *pager;
  65. fprintf(Tmp_fd, "Short Name\tLong Name / Description\n---\n");
  66. for (i = 0; (dat = G_datum_name(i)); i++) {
  67. fprintf(Tmp_fd, "%s\t%s\n\t\t\t(%s ellipsoid)\n---\n",
  68. dat, G_datum_description(i),
  69. G_datum_ellipsoid(i));
  70. }
  71. fclose(Tmp_fd);
  72. pager = getenv("GRASS_PAGER");
  73. if (!pager || strlen(pager) == 0)
  74. pager = "cat";
  75. sprintf(buff, "%s \"%s\" 1>&2", pager,
  76. G_convert_dirseps_to_host(Tmp_file));
  77. G_system(buff);
  78. remove(Tmp_file);
  79. }
  80. G_free(Tmp_file);
  81. }
  82. else {
  83. if (G_strcasecmp(answer, "custom") == 0)
  84. break;
  85. if (G_get_datum_by_name(answer) < 0) {
  86. fprintf(stderr, _("\ninvalid datum\n"));
  87. }
  88. else
  89. break;
  90. }
  91. }
  92. if (G_strcasecmp(answer, "custom") == 0) {
  93. /* For a custom datum we need to interactively ask for the ellipsoid */
  94. if (G_ask_ellipse_name(ellipse) < 0)
  95. return -1;
  96. sprintf(ellpsname, ellipse);
  97. sprintf(datumname, "custom");
  98. }
  99. else {
  100. /* else can look it up from datum.table */
  101. if ((i = G_get_datum_by_name(answer)) < 0)
  102. return -1;
  103. sprintf(ellpsname, G_datum_ellipsoid(i));
  104. sprintf(datumname, G_datum_name(i));
  105. }
  106. return 1;
  107. }