GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
get_datum_name.c
Go to the documentation of this file.
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 
16 #include <string.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 
22 /***********************************************************************
23  * G_ask_datum_name(char *datumname, char *ellpsname)
24  *
25  * ask interactively for a valid datum name
26  *
27  * returns <0 on error
28  * returns 1 on success
29  ***********************************************************************/
30 
43 int G_ask_datum_name(char *datumname, char *ellpsname)
44 {
45  char buff[1024], answer[100], ellipse[100];
46  char *dat, *Tmp_file;
47  FILE *Tmp_fd = NULL;
48  int i;
49 
50 
51  for (;;) {
52  do {
53  fprintf(stderr, _("\nPlease specify datum name\n"));
54  fprintf(stderr,
55  _("Enter 'list' for the list of available datums\n"));
56  fprintf(stderr,
57  _("or 'custom' if you wish to enter custom parameters\n"));
58  fprintf(stderr, _("Hit RETURN to cancel request\n"));
59  fprintf(stderr, ">");
60  } while (!G_gets(answer));
61  G_strip(answer);
62 
63  if (strlen(answer) == 0)
64  return -1;
65 
66  if (strcmp(answer, "list") == 0) {
67  Tmp_file = G_tempfile();
68  if (NULL == (Tmp_fd = fopen(Tmp_file, "w")))
69  G_warning(_("Cannot open temp file"));
70  else {
71  char *pager;
72 
73  fprintf(Tmp_fd, "Short Name\tLong Name / Description\n---\n");
74  for (i = 0; (dat = G_datum_name(i)); i++) {
75  fprintf(Tmp_fd, "%s\t%s\n\t\t\t(%s ellipsoid)\n---\n",
76  dat, G_datum_description(i),
78  }
79  fclose(Tmp_fd);
80 
81  pager = getenv("GRASS_PAGER");
82  if (!pager || strlen(pager) == 0)
83  pager = "cat";
84  sprintf(buff, "%s \"%s\" 1>&2", pager,
85  G_convert_dirseps_to_host(Tmp_file));
86  G_system(buff);
87 
88  remove(Tmp_file);
89  }
90  G_free(Tmp_file);
91  }
92  else {
93  if (G_strcasecmp(answer, "custom") == 0)
94  break;
95 
96  if (G_get_datum_by_name(answer) < 0) {
97  fprintf(stderr, _("\ninvalid datum\n"));
98  }
99  else
100  break;
101  }
102  }
103 
104 
105  if (G_strcasecmp(answer, "custom") == 0) {
106  /* For a custom datum we need to interactively ask for the ellipsoid */
107  if (G_ask_ellipse_name(ellipse) < 0)
108  return -1;
109  sprintf(ellpsname, ellipse);
110  sprintf(datumname, "custom");
111  }
112  else {
113  /* else can look it up from datum.table */
114  if ((i = G_get_datum_by_name(answer)) < 0)
115  return -1;
116  sprintf(ellpsname, G_datum_ellipsoid(i));
117  sprintf(datumname, G_datum_name(i));
118  }
119 
120  return 1;
121 
122 }
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G_gets(char *buf)
Definition: gets.c:39
char * G_convert_dirseps_to_host(char *path)
Converts directory separator characters in a string to the native host separator character (/ on Unix...
Definition: paths.c:73
char * G_tempfile(void)
Returns a temporary file name.
Definition: tempfile.c:47
char * G_datum_name(int n)
Definition: gis/datum.c:56
char * getenv()
int G_ask_datum_name(char *datumname, char *ellpsname)
ask for a valid datum name
char buff[1024]
Definition: g3dcats.c:89
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
char * G_datum_ellipsoid(int n)
Definition: gis/datum.c:76
int G_ask_ellipse_name(char *spheroid)
Definition: get_ell_name.c:21
char * G_datum_description(int n)
Definition: gis/datum.c:66
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
fclose(fd)
int G_system(const char *command)
Run a shell level command.
Definition: system.c:51
int G_get_datum_by_name(const char *name)
Definition: gis/datum.c:43