GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
proj3.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <grass/gis.h>
3 #include <grass/glocale.h>
4 
5 static int lookup(const char *, const char *, char *, int);
6 static int equal(const char *, const char *);
7 static int lower(char);
8 
9 
21 char *G_database_unit_name(int plural)
22 {
23  int n;
24  static char name[256];
25 
26  switch (n = G_projection()) {
27  case PROJECTION_XY:
28  case PROJECTION_UTM:
29  case PROJECTION_LL:
30  case PROJECTION_SP:
31  return G__unit_name(G__projection_units(n), plural);
32  }
33 
34  if (!lookup(UNIT_FILE, plural ? "units" : "unit", name, sizeof(name)))
35  strcpy(name, plural ? "units" : "unit");
36  return name;
37 }
38 
39 
52 {
53  int n;
54  static char name[256];
55 
56  switch (n = G_projection()) {
57  case PROJECTION_XY:
58  case PROJECTION_UTM:
59  case PROJECTION_LL:
60  case PROJECTION_SP:
61  return G__projection_name(n);
62  }
63  if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
64  strcpy(name, _("Unknown projection"));
65  return name;
66 }
67 
68 
81 {
82  char *unit;
83  double factor;
84  char buf[256];
85  int n;
86 
87  static struct
88  {
89  char *unit;
90  double factor;
91  } table[] = {
92  {"unit", 1.0},
93  {"meter", 1.0},
94  {"foot", .3048},
95  {"inch", .0254},
96  {NULL, 0.0}
97  };
98 
99  factor = 0.0;
100  if (lookup(UNIT_FILE, "meters", buf, sizeof(buf)))
101  sscanf(buf, "%lf", &factor);
102  if (factor <= 0.0) {
103  unit = G_database_unit_name(0);
104  for (n = 0; table[n].unit; n++)
105  if (equal(unit, table[n].unit)) {
106  factor = table[n].factor;
107  break;
108  }
109  }
110  return factor;
111 }
112 
113 /***********************************************************************
114  * G_database_datum_name(void)
115  *
116  * return name of datum of current database
117  *
118  * returns pointer to valid name if ok
119  * NULL otherwise
120  ***********************************************************************/
121 
122 
134 {
135  static char name[256], params[256];
136  struct Key_Value *projinfo;
137  int datumstatus;
138 
139  if (lookup(PROJECTION_FILE, "datum", name, sizeof(name)))
140  return name;
141  else if ((projinfo = G_get_projinfo()) == NULL)
142  return NULL;
143  else
144  datumstatus = G_get_datumparams_from_projinfo(projinfo, name, params);
145 
146  G_free_key_value(projinfo);
147  if (datumstatus == 2)
148  return params;
149  else
150  return NULL;
151 }
152 
153 /***********************************************************************
154  * G_database_ellipse_name(void)
155  *
156  * return name of ellipsoid of current database
157  *
158  * returns pointer to valid name if ok
159  * NULL otherwise
160  ***********************************************************************/
161 
163 {
164  static char name[256];
165 
166  if (!lookup(PROJECTION_FILE, "ellps", name, sizeof(name))) {
167  double a, es;
168 
170  sprintf(name, "a=%.16g es=%.16g", a, es);
171  }
172 
173  /* strcpy (name, "Unknown ellipsoid"); */
174  return name;
175 }
176 
177 static int lookup(const char *file, const char *key, char *value, int len)
178 {
179  char path[GPATH_MAX];
180 
181  /*
182  G__file_name (path, "", file, G_mapset());
183  if (access(path,0) == 0)
184  return G_lookup_key_value_from_file(path, key, value, len) == 1;
185  */
186  G__file_name(path, "", file, "PERMANENT");
187  return G_lookup_key_value_from_file(path, key, value, len) == 1;
188 }
189 
190 static int equal(const char *a, const char *b)
191 {
192  if (a == NULL || b == NULL)
193  return a == b;
194  while (*a && *b)
195  if (lower(*a++) != lower(*b++))
196  return 0;
197  if (*a || *b)
198  return 0;
199  return 1;
200 }
201 
202 static int lower(char c)
203 {
204  if (c >= 'A' && c <= 'Z')
205  c += 'a' - 'A';
206  return c;
207 }