GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis/datum.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * MODULE: gis library
5  * AUTHOR(S): Andreas Lange - andreas.lange@rhein-main.de
6  * Paul Kelly - paul-grass@stjohnspoint.co.uk
7  * PURPOSE: provide functions for reading datum parameters from the
8  * location database.
9  * COPYRIGHT: (C) 2000, 2003 by the GRASS Development Team
10  *
11  * This program is free software under the GNU General Public
12  * License (>=v2). Read the file COPYING that comes with GRASS
13  * for details.
14  *
15  *****************************************************************************/
16 
17 #define DATUMTABLE "/etc/datum.table"
18 
19 #include <unistd.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 
24 #include <grass/gis.h>
25 #include <grass/glocale.h>
26 
27 static struct table
28 {
29  char *name; /* Short Name / acronym of map datum */
30  char *descr; /* Long Name for map datum */
31  char *ellps; /* acronym for ellipsoid used with this datum */
32  double dx; /* delta x */
33  double dy; /* delta y */
34  double dz; /* delta z */
35 } *table;
36 
37 static int size;
38 static int count = -1;
39 
40 static int compare_table_names(const void *, const void *);
41 static void read_datum_table(void);
42 
43 int G_get_datum_by_name(const char *name)
44 {
45  int i;
46 
47  read_datum_table();
48 
49  for (i = 0; i < count; i++)
50  if (G_strcasecmp(name, table[i].name) == 0)
51  return i;
52 
53  return -1;
54 }
55 
56 char *G_datum_name(int n)
57 {
58  read_datum_table();
59 
60  if (n < 0 || n >= count)
61  return NULL;
62 
63  return table[n].name;
64 }
65 
67 {
68  read_datum_table();
69 
70  if (n < 0 || n >= count)
71  return NULL;
72 
73  return table[n].descr;
74 }
75 
76 char *G_datum_ellipsoid(int n)
77 {
78  read_datum_table();
79 
80  if (n < 0 || n >= count)
81  return NULL;
82 
83  return table[n].ellps;
84 }
85 
86 /***********************************************************
87  * G_get_datumparams_from_projinfo(projinfo, datumname, params)
88  * struct Key_Value *projinfo Set of key_value pairs containing
89  * projection information in PROJ_INFO file
90  * format
91  * char *datumname Pointer into which a string containing
92  * the datum name (if present) will be
93  * placed.
94  * char *params Pointer into which a string containing
95  * the datum parameters (if present) will
96  * be placed.
97  *
98  * Extract the datum transformation-related parameters from a
99  * set of general PROJ_INFO parameters.
100  * This function can be used to test if a location set-up
101  * supports datum transformation.
102  *
103  * returns: -1 error or no datum information found,
104  * 1 only datum name found, 2 params found
105  ************************************************************/
106 
107 int G_get_datumparams_from_projinfo(const struct Key_Value *projinfo,
108  char *datumname, char *params)
109 {
110  int returnval = -1;
111 
112  if (NULL != G_find_key_value("datum", projinfo)) {
113  sprintf(datumname, "%s", G_find_key_value("datum", projinfo));
114  returnval = 1;
115  }
116 
117  if (G_find_key_value("datumparams", projinfo) != NULL) {
118  sprintf(params, "%s", G_find_key_value("datumparams", projinfo));
119  returnval = 2;
120  }
121  else if (G_find_key_value("nadgrids", projinfo) != NULL) {
122  sprintf(params, "nadgrids=%s",
123  G_find_key_value("nadgrids", projinfo));
124  returnval = 2;
125  }
126  else if (G_find_key_value("towgs84", projinfo) != NULL) {
127  sprintf(params, "towgs84=%s", G_find_key_value("towgs84", projinfo));
128  returnval = 2;
129  }
130  else if (G_find_key_value("dx", projinfo) != NULL
131  && G_find_key_value("dy", projinfo) != NULL
132  && G_find_key_value("dz", projinfo) != NULL) {
133  sprintf(params, "towgs84=%s,%s,%s",
134  G_find_key_value("dx", projinfo),
135  G_find_key_value("dy", projinfo),
136  G_find_key_value("dz", projinfo));
137  returnval = 2;
138  }
139 
140  return returnval;
141 
142 }
143 
144 static void read_datum_table(void)
145 {
146  FILE *fd;
147  char file[1024];
148  char buf[1024];
149  int line;
150 
151  if (count >= 0)
152  return;
153 
154  count = 0;
155 
156  sprintf(file, "%s%s", G_gisbase(), DATUMTABLE);
157 
158  fd = fopen(file, "r");
159  if (!fd) {
160  G_warning(_("unable to open datum table file: %s"), file);
161  return;
162  }
163 
164  for (line = 1; G_getl2(buf, sizeof(buf), fd); line++) {
165  char name[100], descr[100], ellps[100];
166  struct table *t;
167 
168  G_strip(buf);
169  if (*buf == '\0' || *buf == '#')
170  continue;
171 
172  if (count >= size) {
173  size += 50;
174  table = G_realloc(table, size * sizeof(struct table));
175  }
176 
177  t = &table[count];
178 
179  if (sscanf(buf, "%s \"%99[^\"]\" %s dx=%lf dy=%lf dz=%lf",
180  name, descr, ellps, &t->dx, &t->dy, &t->dz) != 6) {
181  G_warning(_("error in datum table file, line %d"), line);
182  continue;
183  }
184 
185  t->name = G_store(name);
186  t->descr = G_store(descr);
187  t->ellps = G_store(ellps);
188 
189  count++;
190  }
191 
192  qsort(table, count, sizeof(struct table), compare_table_names);
193 }
194 
195 static int compare_table_names(const void *aa, const void *bb)
196 {
197  const struct table *a = aa;
198  const struct table *b = bb;
199 
200  return G_strcasecmp(a->name, b->name);
201 }
char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key.
Definition: key_value1.c:128
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)
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
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
FILE * fd
Definition: g3dcolor.c:368
#define DATUMTABLE
Definition: gis/datum.c:17
string name
Definition: render.py:1314
int count
char * G_datum_name(int n)
Definition: gis/datum.c:56
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int G_getl2(char *buf, int n, FILE *fd)
gets a line of text from a file of any pedigree
Definition: getl.c:52
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
char * G_datum_description(int n)
Definition: gis/datum.c:66
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
#define file
char * G_gisbase(void)
top level module directory
Definition: gisbase.c:42
int n
Definition: dataquad.c:291
int G_get_datum_by_name(const char *name)
Definition: gis/datum.c:43