GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 }
char * G__unit_name(int unit, int plural)
Definition: proj2.c:20
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_get_datumparams_from_projinfo(const struct Key_Value *projinfo, char *datumname, char *params)
Definition: gis/datum.c:107
float b
Definition: named_colr.c:8
string name
Definition: render.py:1314
int G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:145
int G_get_ellipsoid_parameters(double *a, double *e2)
get ellipsoid parameters
Definition: get_ellipse.c:66
char * G_database_unit_name(int plural)
database units
Definition: proj3.c:21
int G_lookup_key_value_from_file(const char *file, const char *key, char value[], int n)
Look up for key in file.
Definition: key_value4.c:64
int G__projection_units(int n)
Definition: proj2.c:4
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
char * value
Definition: env.c:30
tuple unit
struct Key_Value * G_get_projinfo(void)
Gets projection information for location.
Definition: get_projinfo.c:52
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
#define file
char * G_database_ellipse_name(void)
Definition: proj3.c:162
char * G__projection_name(int n)
Definition: proj2.c:36
char * G_database_datum_name(void)
get datum name for database
Definition: proj3.c:133
double G_database_units_to_meters_factor(void)
conversion to meters
Definition: proj3.c:80
int n
Definition: dataquad.c:291
int G_projection(void)
query cartographic projection
Definition: proj1.c:33
char * G_database_projection_name(void)
query cartographic projection
Definition: proj3.c:51