~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/gis/get_ellipse.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*!
2
 
   \file get_ellipse.c
3
 
 
4
 
   \brief Getting ellipsoid parameters from the database.
5
 
 
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.
9
 
 
10
 
   Returns: 1 ok, 0 default values used.
11
 
   Dies with diagnostic if there is an error
12
 
 
13
 
   (C) 2001-2008 by the GRASS Development Team
14
 
 
15
 
   This program is free software under the 
16
 
   GNU General Public License (>=v2). 
17
 
   Read the file COPYING that comes with GRASS
18
 
   for details.
 
2
  \file lib/gis/get_ellipse.c
 
3
 
 
4
  \brief GIS Library - Getting ellipsoid parameters from the database.
 
5
 
 
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.
 
9
 
 
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-
 
12
  compatibility. 
 
13
  Looks up ellipsoid in ellipsoid table and returns the
 
14
  a, e2 and f parameters for the ellipsoid
 
15
  
 
16
  (C) 2001-2009 by the GRASS Development Team
 
17
 
 
18
  This program is free software under the GNU General Public License
 
19
  (>=v2).  Read the file COPYING that comes with GRASS for details.
19
20
 
20
21
   \author CERL
21
22
 */
28
29
#include <grass/gis.h>
29
30
#include <grass/glocale.h>
30
31
 
31
 
static struct table
32
 
{
33
 
    char *name;
34
 
    char *descr;
35
 
    double a;
36
 
    double e2;
37
 
    double f;
38
 
} *table = NULL;
 
32
static const char PERMANENT[] = "PERMANENT";
39
33
 
40
 
static int count = -1;
41
 
static char *PERMANENT = "PERMANENT";
 
34
static struct table {
 
35
    struct ellipse
 
36
    {
 
37
        char *name;
 
38
        char *descr;
 
39
        double a;
 
40
        double e2;
 
41
        double f;
 
42
    } *ellipses;
 
43
    int count;
 
44
    int size;
 
45
    int initialized;
 
46
} table;
42
47
 
43
48
/* static int get_a_e2 (char *, char *, double *,double *); */
44
 
static int get_a_e2_f(const char *, const char *, double *, double *,
45
 
                      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 *);
50
52
 
51
 
 
52
53
/*!
53
54
 * \brief get ellipsoid parameters
54
55
 *
65
66
 */
66
67
int G_get_ellipsoid_parameters(double *a, double *e2)
67
68
{
68
 
    int in_stat, stat;
 
69
    int stat;
69
70
    char ipath[GPATH_MAX];
70
71
    struct Key_Value *proj_keys;
71
72
 
72
73
    proj_keys = NULL;
73
74
 
74
 
    G__file_name(ipath, "", PROJECTION_FILE, PERMANENT);
 
75
    G_file_name(ipath, "", PROJECTION_FILE, PERMANENT);
75
76
 
76
77
    if (access(ipath, 0) != 0) {
77
78
        *a = 6378137.0;
79
80
        return 0;
80
81
    }
81
82
 
82
 
    proj_keys = G_read_key_value_file(ipath, &in_stat);
83
 
 
84
 
    if (in_stat != 0) {
85
 
        G_fatal_error(_("Unable to open file %s in <%s>"),
86
 
                      PROJECTION_FILE, PERMANENT);
87
 
    }
 
83
    proj_keys = G_read_key_value_file(ipath);
88
84
 
89
85
    stat = get_ellipsoid_parameters(proj_keys, a, e2);
90
86
 
94
90
}
95
91
 
96
92
/*!
97
 
 * \brief get ellipsoid parameters by name
98
 
 *
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.
102
 
 *
103
 
 * \param[in]  name ellipsoid name
 
93
 * \brief Get ellipsoid parameters by name
 
94
 *
 
95
 * This routine returns the semi-major axis <i>a</i> (in meters) and
 
96
 * eccentricity squared <i>e2</i> for the named ellipsoid.
 
97
 *
 
98
 * \param  name ellipsoid name
104
99
 * \param[out] a    semi-major axis
105
100
 * \param[out] e2   eccentricity squared
106
101
 *
111
106
{
112
107
    int i;
113
108
 
114
 
    (void)read_ellipsoid_table(0);
 
109
    G_read_ellipsoid_table(0);
115
110
 
116
 
    for (i = 0; i < count; i++) {
117
 
        if (G_strcasecmp(name, table[i].name) == 0) {
118
 
            *a = table[i].a;
119
 
            *e2 = table[i].e2;
 
111
    for (i = 0; i < table.count; i++) {
 
112
        if (G_strcasecmp(name, table.ellipses[i].name) == 0) {
 
113
            *a = table.ellipses[i].a;
 
114
            *e2 = table.ellipses[i].e2;
120
115
            return 1;
121
116
        }
122
117
    }
124
119
}
125
120
 
126
121
/*!
127
 
 * \brief get ellipsoid name
 
122
 * \brief Get ellipsoid name
128
123
 *
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.
132
127
 *
133
 
 * \param[in] n ellipsoid identificator
 
128
 * \param n ellipsoid identificator
134
129
 *
135
 
 *  \return char * ellipsoid name
136
 
 *  \return NULL if no ellipsoid found
 
130
 * \return ellipsoid name
 
131
 * \return NULL if no ellipsoid found
137
132
 */
138
 
char *G_ellipsoid_name(int n)
 
133
const char *G_ellipsoid_name(int n)
139
134
{
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;
142
137
}
143
138
 
144
 
/*
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-
147
 
 * compatibility. 
148
 
 * looks up ellipsoid in ellipsoid table and returns the
149
 
 * a, e2 and f parameters for the ellipsoid
150
 
 * 
151
 
 * returns 1 if ok,
152
 
 *         0 if not found in table 
153
 
 */
154
 
 
155
139
/*!
156
 
 * \brief get spheroid parameters by name
157
 
 *
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.
162
 
 *
163
 
 * \param[in] name spheroid name
 
140
 * \brief Get spheroid parameters by name
 
141
 *
 
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.
 
145
 *
 
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
172
155
{
173
156
    int i;
174
157
 
175
 
    (void)read_ellipsoid_table(0);
 
158
    G_read_ellipsoid_table(0);
176
159
 
177
 
    for (i = 0; i < count; i++) {
178
 
        if (G_strcasecmp(name, table[i].name) == 0) {
179
 
            *a = table[i].a;
180
 
            *e2 = table[i].e2;
181
 
            *f = table[i].f;
 
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;
182
165
            return 1;
183
166
        }
184
167
    }
187
170
 
188
171
 
189
172
/*!
190
 
 * \brief get description for <b>n</b><i>th</i> ellipsoid
 
173
 * \brief Get description for nth ellipsoid
191
174
 *
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.
195
178
 *
196
 
 * \param[in] n ellipsoid identificator
 
179
 * \param n ellipsoid identificator
197
180
 *
198
181
 * \return pointer to ellipsoid description
199
182
 * \return NULL if no ellipsoid found
200
183
 */
201
 
 
202
 
char *G_ellipsoid_description(int n)
 
184
const char *G_ellipsoid_description(int n)
203
185
{
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;
206
188
}
207
189
 
208
 
static int
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)
210
191
{
211
192
    double b, recipf;
212
193
 
246
227
    return 0;
247
228
}
248
229
 
249
 
void ellipsoid_table_file(char *file)
250
 
{
251
 
    sprintf(file, "%s/etc/ellipse.table", G_gisbase());
252
 
    return;
253
 
}
254
 
 
255
 
static int compare_table_names(const void *pa, const void *pb)
256
 
{
257
 
    const struct table *a = pa, *b = pb;
258
 
 
259
 
    /* return strcmp(a->name,b->name); */
 
230
static int compare_ellipse_names(const void *pa, const void *pb)
 
231
{
 
232
    const struct ellipse *a = pa;
 
233
    const struct ellipse *b = pb;
 
234
 
260
235
    return G_strcasecmp(a->name, b->name);
261
236
}
262
237
 
263
 
static int read_ellipsoid_table(int fatal)
 
238
/*!
 
239
  \brief Read ellipsoid table
 
240
 
 
241
  \param fatal non-zero value for G_fatal_error(), otherwise
 
242
  G_warning() is used
 
243
 
 
244
  \return 1 on sucess
 
245
  \return 0 on error
 
246
*/
 
247
int G_read_ellipsoid_table(int fatal)
264
248
{
265
249
    FILE *fd;
266
250
    char file[GPATH_MAX];
267
251
    char buf[1024];
268
 
    char name[100], descr[100], buf1[100], buf2[100];
269
252
    char badlines[256];
270
253
    int line;
271
254
    int err;
272
255
 
273
 
    if (count >= 0)
 
256
    if (G_is_initialized(&table.initialized))
274
257
        return 1;
275
 
    count = 0;
276
 
    table = NULL;
277
258
 
278
 
    (void)ellipsoid_table_file(file);
 
259
    sprintf(file, "%s/etc/proj/ellipse.table", G_gisbase());
279
260
    fd = fopen(file, "r");
280
261
 
281
262
    if (fd == NULL) {
282
 
        perror(file);
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);
285
265
        return 0;
286
266
    }
287
267
 
288
268
    err = 0;
289
269
    *badlines = 0;
290
270
    for (line = 1; G_getl2(buf, sizeof buf, fd); line++) {
 
271
        char name[100], descr[100], buf1[100], buf2[100];
 
272
        struct ellipse *e;
 
273
 
291
274
        G_strip(buf);
292
275
        if (*buf == 0 || *buf == '#')
293
276
            continue;
294
277
 
295
 
        if (sscanf(buf, "%s  \"%99[^\"]\" %s %s", name, descr, buf1, buf2) !=
296
 
            4) {
 
278
        if (sscanf(buf, "%s  \"%99[^\"]\" %s %s", name, descr, buf1, buf2) != 4) {
297
279
            err++;
298
280
            sprintf(buf, " %d", line);
299
281
            if (*badlines)
300
 
                G_strcat(badlines, ",");
301
 
            G_strcat(badlines, buf);
 
282
                strcat(badlines, ",");
 
283
            strcat(badlines, buf);
302
284
            continue;
303
285
        }
304
286
 
305
 
        table =
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);
310
 
 
311
 
        if (get_a_e2_f
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,
314
 
                          &table[count].f))
315
 
            count++;
 
287
        if (table.count >= table.size) {
 
288
            table.size += 60;
 
289
            table.ellipses = G_realloc(table.ellipses, table.size * sizeof(struct ellipse));
 
290
        }
 
291
 
 
292
        e = &table.ellipses[table.count];
 
293
 
 
294
        e->name = G_store(name);
 
295
        e->descr = G_store(descr);
 
296
 
 
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))
 
299
            table.count++;
316
300
        else {
317
301
            err++;
318
302
            sprintf(buf, " %d", line);
319
303
            if (*badlines)
320
 
                G_strcat(badlines, ",");
321
 
            G_strcat(badlines, buf);
 
304
                strcat(badlines, ",");
 
305
            strcat(badlines, buf);
322
306
            continue;
323
307
        }
324
308
    }
327
311
 
328
312
    if (!err) {
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);
331
316
        return 1;
332
317
    }
333
318
 
334
 
    (fatal ? G_fatal_error : G_warning) ((err > 1)
335
 
                                         ?
336
 
                                         _("Lines%s of ellipsoid table file <%s> are invalid")
337
 
                                         :
338
 
                                         _("Line%s of ellipsoid table file <%s> is invalid"),
339
 
                                         badlines, file);
 
319
    (fatal ? G_fatal_error : G_warning)(
 
320
        _n(
 
321
        ("Line%s of ellipsoid table file <%s> is invalid"),
 
322
        ("Lines%s of ellipsoid table file <%s> are invalid"),
 
323
        err), 
 
324
        badlines, file);
 
325
 
 
326
    G_initialize_done(&table.initialized);
340
327
 
341
328
    return 0;
342
329
}
343
330
 
344
 
static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a,
345
 
                                    double *e2)
 
331
static int get_ellipsoid_parameters(struct Key_Value *proj_keys, double *a, double *e2)
346
332
{
347
 
    char *str, *str1;
 
333
    const char *str, *str1;
348
334
 
349
335
    if (!proj_keys) {
350
336
        return -1;
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);
360
 
                }
361
346
            }
362
 
            else {
 
347
            else
363
348
                *a = 6370997.0;
364
 
            }
 
349
 
365
350
            *e2 = 0.0;
366
351
 
367
352
            return 0;
368
353
        }
369
354
        else {
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);
373
 
            }
374
 
            else {
 
358
            else
375
359
                return 1;
376
 
            }
377
360
        }
378
361
    }
379
362
    else {
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);
386
 
            }
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);
390
 
            }
391
372
 
392
373
            return 1;
393
374
        }
398
379
                *e2 = .006694385;
399
380
                return 0;
400
381
            }
401
 
            else {
 
382
            else
402
383
                G_fatal_error(_("No ellipsoid info given in file %s in <%s>"),
403
384
                              PROJECTION_FILE, PERMANENT);
404
 
            }
405
385
        }
406
386
    }
407
387