GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
proj3.c
Go to the documentation of this file.
1 
14 #include <string.h>
15 #include <grass/gis.h>
16 #include <grass/glocale.h>
17 
18 static const char *lookup_proj(const char *);
19 static const char *lookup_units(const char *);
20 static const char *lookup_epsg();
21 static int equal(const char *, const char *);
22 static int lower(char);
23 
24 static int initialized;
25 static struct Key_Value *proj_info, *proj_units, *proj_epsg;
26 
27 static void init(void)
28 {
29  if (G_is_initialized(&initialized))
30  return;
31 
32  proj_info = G_get_projinfo();
33  proj_units = G_get_projunits();
34  proj_epsg = G_get_projepsg();
35 
36  G_initialize_done(&initialized);
37 }
38 
50 const char *G_database_unit_name(int plural)
51 {
52  int units;
53  const char *name;
54 
56 
57  if (units == U_UNDEFINED) {
58  name = lookup_units(plural ? "units" : "unit");
59  if (!name)
60  return plural ? _("units") : _("unit");
61 
62  if (strcasecmp(name, "meter") == 0 || strcasecmp(name, "metre") == 0
63  || strcasecmp(name, "meters") == 0 || strcasecmp(name, "metres") == 0)
64  units = U_METERS;
65  else if (strcasecmp(name, "kilometer") == 0 || strcasecmp(name, "kilometre") == 0
66  || strcasecmp(name, "kilometers") == 0 || strcasecmp(name, "kilometres") == 0)
67  units = U_KILOMETERS;
68  else if (strcasecmp(name, "acre") == 0 || strcasecmp(name, "acres") == 0)
69  units = U_ACRES;
70  else if (strcasecmp(name, "hectare") == 0 || strcasecmp(name, "hectares") == 0)
71  units = U_HECTARES;
72  else if (strcasecmp(name, "mile") == 0 || strcasecmp(name, "miles") == 0)
73  units = U_MILES;
74  else if (strcasecmp(name, "foot") == 0 || strcasecmp(name, "feet") == 0)
75  units = U_FEET;
76  else if (strcasecmp(name, "foot_us") == 0 || strcasecmp(name, "foot_uss") == 0)
77  units = U_USFEET;
78  else if (strcasecmp(name, "degree") == 0 || strcasecmp(name, "degrees") == 0)
79  units = U_DEGREES;
80  else
81  units = U_UNKNOWN;
82  }
83 
84  return G_get_units_name(units, plural, FALSE);
85 }
86 
96 const char *G_database_projection_name(void)
97 {
98  int n;
99  const char *name;
100 
101  switch (n = G_projection()) {
102  case PROJECTION_XY:
103  case PROJECTION_UTM:
104  case PROJECTION_LL:
105  return G_projection_name(n);
106  }
107 
108  name = lookup_proj("name");
109  if (!name)
110  return _("Unknown projection");
111 
112  return name;
113 }
114 
125 {
126  const char *unit;
127  const char *buf;
128  double factor;
129  int n;
130 
131  /* TODO: sync with definitions in ../proj/units.table */
132  static const struct
133  {
134  char *unit;
135  double factor;
136  } table[] = {
137  {"unit", 1.0},
138  {"meter", 1.0},
139  {"foot", .3048},
140  {"foot_us", 1200/3937.},
141  {"inch", .0254},
142  {NULL, 0.0}
143  };
144 
145  factor = 0.0;
146  buf = lookup_units("meters");
147  if (buf)
148  sscanf(buf, "%lf", &factor);
149  if (factor <= 0.0) {
150  unit = G_database_unit_name(0);
151  for (n = 0; table[n].unit; n++)
152  if (equal(unit, table[n].unit)) {
153  factor = table[n].factor;
154  break;
155  }
156  }
157  return factor;
158 }
159 
170 const char *G_database_datum_name(void)
171 {
172  const char *name;
173  char buf[256], params[256];
174  int datumstatus;
175 
176  name = lookup_proj("datum");
177  if (name)
178  return name;
179  else if (!proj_info)
180  return NULL;
181  else
182  datumstatus = G_get_datumparams_from_projinfo(proj_info, buf, params);
183 
184  if (datumstatus == 2)
185  return G_store(params);
186  else
187  return NULL;
188 }
189 
196 const char *G_database_ellipse_name(void)
197 {
198  const char *name;
199 
200  name = lookup_proj("ellps");
201  if (!name) {
202  char buf[256];
203  double a, es;
204 
206  sprintf(buf, "a=%.16g es=%.16g", a, es);
207  name = G_store(buf);
208  }
209 
210  /* strcpy (name, "Unknown ellipsoid"); */
211  return name;
212 }
213 
220 const char *G_database_epsg_code(void)
221 {
222  return lookup_epsg();
223 }
224 
225 const char *lookup_proj(const char *key)
226 {
227  init();
228  return G_find_key_value(key, proj_info);
229 }
230 
231 const char *lookup_units(const char *key)
232 {
233  init();
234  return G_find_key_value(key, proj_units);
235 }
236 
237 const char *lookup_epsg()
238 {
239  init();
240  return G_find_key_value("epsg", proj_epsg);
241 }
242 
243 int equal(const char *a, const char *b)
244 {
245  if (a == NULL || b == NULL)
246  return a == b;
247  while (*a && *b)
248  if (lower(*a++) != lower(*b++))
249  return 0;
250  if (*a || *b)
251  return 0;
252  return 1;
253 }
254 
255 int lower(char c)
256 {
257  if (c >= 'A' && c <= 'Z')
258  c += 'a' - 'A';
259  return c;
260 }
const char * G_database_datum_name(void)
Get datum name for the current location.
Definition: proj3.c:170
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition: key_value1.c:84
int G_get_datumparams_from_projinfo(const struct Key_Value *projinfo, char *datumname, char *params)
Definition: gis/datum.c:109
struct Key_Value * G_get_projunits(void)
Gets units information for location.
Definition: get_projinfo.c:29
#define FALSE
Definition: dbfopen.c:117
const char * G_database_ellipse_name(void)
Get ellipsoid name for the current location.
Definition: proj3.c:196
const char * G_projection_name(int n)
Get projection name.
Definition: proj2.c:55
const char * G_database_epsg_code(void)
Get EPGS code for the current location.
Definition: proj3.c:220
char * G_store(const char *s)
Copy string to allocated memory.
Definition: strings.c:86
int G_get_ellipsoid_parameters(double *a, double *e2)
get ellipsoid parameters
Definition: get_ellipse.c:67
int G_is_initialized(int *p)
Definition: counter.c:59
#define NULL
Definition: ccmath.h:32
void G_initialize_done(int *p)
Definition: counter.c:76
int G_projection_units(int n)
Get projection units code (for internal use only)
Definition: proj2.c:32
double b
struct Key_Value * G_get_projinfo(void)
Gets projection information for location.
Definition: get_projinfo.c:58
const char * G_database_projection_name(void)
Query cartographic projection for the current location.
Definition: proj3.c:96
struct Key_Value * G_get_projepsg(void)
Gets EPSG information for the current location.
Definition: get_projinfo.c:85
const char * G_get_units_name(int units, int plural, int square)
Get localized units name.
Definition: units.c:203
const char * name
Definition: named_colr.c:7
double G_database_units_to_meters_factor(void)
Conversion to meters.
Definition: proj3.c:124
int G_projection(void)
Query cartographic projection.
Definition: proj1.c:32
void init(double work[])
Definition: as177.c:65
const char * G_database_unit_name(int plural)
Get units (localized) name for the current location.
Definition: proj3.c:50