4
\brief Getting ellipsoid parameters from the database.
6
This routine returns the ellipsoid parameters from the database.
7
If the PROJECTION_FILE exists in the PERMANENT mapset, read info
8
from that file, otherwise return WGS 84 values.
10
Returns: 1 ok, 0 default values used.
11
Dies with diagnostic if there is an error
13
(C) 2001-2008 by the GRASS Development Team
15
This program is free software under the
16
GNU General Public License (>=v2).
17
Read the file COPYING that comes with GRASS
2
\file lib/gis/get_ellipse.c
4
\brief GIS Library - Getting ellipsoid parameters from the database.
6
This routine returns the ellipsoid parameters from the database.
7
If the PROJECTION_FILE exists in the PERMANENT mapset, read info
8
from that file, otherwise return WGS 84 values.
10
New 05/2000 by al: for datum shift the f parameter is needed too.
11
This all is not a clean design, but it keeps backward-
13
Looks up ellipsoid in ellipsoid table and returns the
14
a, e2 and f parameters for the ellipsoid
16
(C) 2001-2009 by the GRASS Development Team
18
This program is free software under the GNU General Public License
19
(>=v2). Read the file COPYING that comes with GRASS for details.
28
29
#include <grass/gis.h>
29
30
#include <grass/glocale.h>
32
static const char PERMANENT[] = "PERMANENT";
40
static int count = -1;
41
static char *PERMANENT = "PERMANENT";
43
48
/* static int get_a_e2 (char *, char *, double *,double *); */
44
static int get_a_e2_f(const char *, const char *, double *, double *,
46
void ellipsoid_table_file(char *);
47
static int compare_table_names(const void *, const void *);
48
static int read_ellipsoid_table(int);
49
static int get_a_e2_f(const char *, const char *, double *, double *, double *);
50
static int compare_ellipse_names(const void *, const void *);
49
51
static int get_ellipsoid_parameters(struct Key_Value *, double *, double *);
53
54
* \brief get ellipsoid parameters
97
* \brief get ellipsoid parameters by name
99
* This routine returns the semi-major axis <b>a</b> (in meters) and
100
* eccentricity squared <b>e2</b> for the named ellipsoid. Returns 1
101
* if <b>name</b> is a known ellipsoid, 0 otherwise.
103
* \param[in] name ellipsoid name
93
* \brief Get ellipsoid parameters by name
95
* This routine returns the semi-major axis <i>a</i> (in meters) and
96
* eccentricity squared <i>e2</i> for the named ellipsoid.
98
* \param name ellipsoid name
104
99
* \param[out] a semi-major axis
105
100
* \param[out] e2 eccentricity squared
127
* \brief get ellipsoid name
122
* \brief Get ellipsoid name
129
124
* This function returns a pointer to the short name for the
130
* <b>n</b><i>th</i> ellipsoid. If <b>n</b> is less than 0 or greater
125
* <i>n</i><i>th</i> ellipsoid. If <i>n</i> is less than 0 or greater
131
126
* than the number of known ellipsoids, it returns a NULL pointer.
133
* \param[in] n ellipsoid identificator
128
* \param n ellipsoid identificator
135
* \return char * ellipsoid name
136
* \return NULL if no ellipsoid found
130
* \return ellipsoid name
131
* \return NULL if no ellipsoid found
138
char *G_ellipsoid_name(int n)
133
const char *G_ellipsoid_name(int n)
140
(void)read_ellipsoid_table(0);
141
return n >= 0 && n < count ? table[n].name : NULL;
135
G_read_ellipsoid_table(0);
136
return n >= 0 && n < table.count ? table.ellipses[n].name : NULL;
145
* new 05/2000 by al: for datum shift the f parameter is needed too.
146
* this all is not a clean design, but it keeps backward-
148
* looks up ellipsoid in ellipsoid table and returns the
149
* a, e2 and f parameters for the ellipsoid
152
* 0 if not found in table
156
* \brief get spheroid parameters by name
158
* This function returns the semi-major axis <b>a</b> (in meters), the
159
* eccentricity squared <b>e2</b> and the inverse flattening <b>f</b>
160
* for the named ellipsoid. Returns 1 if <b>name</b> is a known
161
* ellipsoid, 0 otherwise.
163
* \param[in] name spheroid name
140
* \brief Get spheroid parameters by name
142
* This function returns the semi-major axis <i>a</i> (in meters), the
143
* eccentricity squared <i>e2</i> and the inverse flattening <i>f</i>
144
* for the named ellipsoid.
146
* \param name spheroid name
164
147
* \param[out] a semi-major axis
165
148
* \param[out] e2 eccentricity squared
166
149
* \param[out] f inverse flattening
175
(void)read_ellipsoid_table(0);
158
G_read_ellipsoid_table(0);
177
for (i = 0; i < count; i++) {
178
if (G_strcasecmp(name, table[i].name) == 0) {
160
for (i = 0; i < table.count; i++) {
161
if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
162
*a = table.ellipses[i].a;
163
*e2 = table.ellipses[i].e2;
164
*f = table.ellipses[i].f;
190
* \brief get description for <b>n</b><i>th</i> ellipsoid
173
* \brief Get description for nth ellipsoid
192
175
* This function returns a pointer to the description text for the
193
* <b>n</b><i>th</i> ellipsoid. If <b>n</b> is less than 0 or greater
176
* <i>n</i>th ellipsoid. If <i>n</i> is less than 0 or greater
194
177
* than the number of known ellipsoids, it returns a NULL pointer.
196
* \param[in] n ellipsoid identificator
179
* \param n ellipsoid identificator
198
181
* \return pointer to ellipsoid description
199
182
* \return NULL if no ellipsoid found
202
char *G_ellipsoid_description(int n)
184
const char *G_ellipsoid_description(int n)
204
(void)read_ellipsoid_table(0);
205
return n >= 0 && n < count ? table[n].descr : NULL;
186
G_read_ellipsoid_table(0);
187
return n >= 0 && n < table.count ? table.ellipses[n].descr : NULL;
209
get_a_e2_f(const char *s1, const char *s2, double *a, double *e2, double *f)
190
static int get_a_e2_f(const char *s1, const char *s2, double *a, double *e2, double *f)
211
192
double b, recipf;
249
void ellipsoid_table_file(char *file)
251
sprintf(file, "%s/etc/ellipse.table", G_gisbase());
255
static int compare_table_names(const void *pa, const void *pb)
257
const struct table *a = pa, *b = pb;
259
/* return strcmp(a->name,b->name); */
230
static int compare_ellipse_names(const void *pa, const void *pb)
232
const struct ellipse *a = pa;
233
const struct ellipse *b = pb;
260
235
return G_strcasecmp(a->name, b->name);
263
static int read_ellipsoid_table(int fatal)
239
\brief Read ellipsoid table
241
\param fatal non-zero value for G_fatal_error(), otherwise
247
int G_read_ellipsoid_table(int fatal)
266
250
char file[GPATH_MAX];
268
char name[100], descr[100], buf1[100], buf2[100];
269
252
char badlines[256];
256
if (G_is_initialized(&table.initialized))
278
(void)ellipsoid_table_file(file);
259
sprintf(file, "%s/etc/proj/ellipse.table", G_gisbase());
279
260
fd = fopen(file, "r");
281
262
if (fd == NULL) {
283
sprintf(buf, _("Unable to open ellipsoid table file <%s>"), file);
284
fatal ? G_fatal_error(buf) : G_warning(buf);
263
(fatal ? G_fatal_error : G_warning)(_("Unable to open ellipsoid table file <%s>"), file);
264
G_initialize_done(&table.initialized);
290
270
for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
271
char name[100], descr[100], buf1[100], buf2[100];
292
275
if (*buf == 0 || *buf == '#')
295
if (sscanf(buf, "%s \"%99[^\"]\" %s %s", name, descr, buf1, buf2) !=
278
if (sscanf(buf, "%s \"%99[^\"]\" %s %s", name, descr, buf1, buf2) != 4) {
298
280
sprintf(buf, " %d", line);
300
G_strcat(badlines, ",");
301
G_strcat(badlines, buf);
282
strcat(badlines, ",");
283
strcat(badlines, buf);
306
(struct table *)G_realloc((char *)table,
307
(count + 1) * sizeof(*table));
308
table[count].name = G_store(name);
309
table[count].descr = G_store(descr);
312
(buf1, buf2, &table[count].a, &table[count].e2, &table[count].f)
313
|| get_a_e2_f(buf2, buf1, &table[count].a, &table[count].e2,
287
if (table.count >= table.size) {
289
table.ellipses = G_realloc(table.ellipses, table.size * sizeof(struct ellipse));
292
e = &table.ellipses[table.count];
294
e->name = G_store(name);
295
e->descr = G_store(descr);
297
if (get_a_e2_f(buf1, buf2, &e->a, &e->e2, &e->f) ||
298
get_a_e2_f(buf2, buf1, &e->a, &e->e2, &e->f))
318
302
sprintf(buf, " %d", line);
320
G_strcat(badlines, ",");
321
G_strcat(badlines, buf);
304
strcat(badlines, ",");
305
strcat(badlines, buf);
329
313
/* over correct typed version */
330
qsort(table, count, sizeof(*table), compare_table_names);
314
qsort(table.ellipses, table.count, sizeof(struct ellipse), compare_ellipse_names);
315
G_initialize_done(&table.initialized);
334
(fatal ? G_fatal_error : G_warning) ((err > 1)
336
_("Lines%s of ellipsoid table file <%s> are invalid")
338
_("Line%s of ellipsoid table file <%s> is invalid"),
319
(fatal ? G_fatal_error : G_warning)(
321
("Line%s of ellipsoid table file <%s> is invalid"),
322
("Lines%s of ellipsoid table file <%s> are invalid"),
326
G_initialize_done(&table.initialized);
344
static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a,
331
static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a, double *e2)
333
const char *str, *str1;
349
335
if (!proj_keys) {
354
340
if (strncmp(str, "sphere", 6) == 0) {
355
341
str = G_find_key_value("a", proj_keys);
356
342
if (str != NULL) {
357
if (sscanf(str, "%lf", a) != 1) {
343
if (sscanf(str, "%lf", a) != 1)
358
344
G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
359
345
str, PROJECTION_FILE, PERMANENT);
370
if (G_get_ellipsoid_by_name(str, a, e2) == 0) {
355
if (G_get_ellipsoid_by_name(str, a, e2) == 0)
371
356
G_fatal_error(_("Invalid ellipsoid '%s' in file %s in <%s>"),
372
357
str, PROJECTION_FILE, PERMANENT);
380
363
str = G_find_key_value("a", proj_keys);
381
364
str1 = G_find_key_value("es", proj_keys);
382
365
if ((str != NULL) && (str1 != NULL)) {
383
if (sscanf(str, "%lf", a) != 1) {
366
if (sscanf(str, "%lf", a) != 1)
384
367
G_fatal_error(_("Invalid a: field '%s' in file %s in <%s>"),
385
368
str, PROJECTION_FILE, PERMANENT);
387
if (sscanf(str1, "%lf", e2) != 1) {
369
if (sscanf(str1, "%lf", e2) != 1)
388
370
G_fatal_error(_("Invalid es: field '%s' in file %s in <%s>"),
389
371
str, PROJECTION_FILE, PERMANENT);