File: datum.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (424 lines) | stat: -rw-r--r-- 13,110 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
   \file lib/proj/datum.c

   \brief GProj library - Functions for reading datum parameters from the
location database

   \author Andreas Lange <andreas.lange rhein-main.de>, Paul Kelly <paul-grass
stjohnspoint.co.uk>

   (C) 2003-2008 by the GRASS Development Team

   This program is free software under the GNU General Public
   License (>=v2). Read the file COPYING that comes with GRASS
   for details.
**/

#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#include <grass/gis.h>
#include <grass/glocale.h>
#include <grass/gprojects.h>
#include "local_proto.h"

/**
 * \brief Look up a string in datum.table file to see if it is a valid datum
 *        name and if so place its information into a gpj_datum struct
 *
 * \param name String containing datum name to look up
 * \param dstruct gpj_datum struct into which datum parameters will be placed
 *        if found
 *
 * \return 1 if datum found, -1 if not
 **/

int GPJ_get_datum_by_name(const char *name, struct gpj_datum *dstruct)
{
    struct datum_list *list, *listhead;

    list = listhead = read_datum_table();

    while (list != NULL) {
        if (G_strcasecmp(name, list->name) == 0) {
            dstruct->name = G_store(list->name);
            dstruct->longname = G_store(list->longname);
            dstruct->ellps = G_store(list->ellps);
            dstruct->dx = list->dx;
            dstruct->dy = list->dy;
            dstruct->dz = list->dz;
            free_datum_list(listhead);
            return 1;
        }
        list = list->next;
    }
    free_datum_list(listhead);
    return -1;
}

/**
 * \brief "Last resort" function to retrieve a "default" set of datum
 * parameters for a datum (N.B. there really is no such thing as a
 * catch-all default!)
 *
 * Kind of a "last resort" function as there really is no such thing
 * as a default set of datum transformation parameters. Only should
 * really be used where user interaction to choose a set of parameters
 * is not desirable. Use of this function is not likely to result in
 * selection of the optimum set of datum transformation parameters
 * for the location
 *
 * \param name      String containing GRASS datum name for which default
 *                  parameters are to be retrieved
 *
 * \param params    Pointer to a pointer which will have memory
 *                  allocated and into which a string containing
 *                  the datum parameters (if present) will
 *                  be placed
 *
 * \return          The number of possible parameter sets GRASS knows
 *                  about for this datum
 *
 **/

int GPJ_get_default_datum_params_by_name(const char *name, char **params)
{
    struct gpj_datum_transform_list *list, *old;
    int count = 0;

    list = GPJ_get_datum_transform_by_name(name);

    if (list == NULL) {
        *params = NULL;
        return -1;
    }

    /* Take the first parameter set in the list as the default
     * (will normally be a 3-parameter transformation)        */
    *params = G_store(list->params);

    while (list != NULL) {
        count++;
        old = list;
        list = list->next;
        GPJ_free_datum_transform(old);
    }

    return count;
}

/**
 *
 * \brief Extract the datum transformation-related parameters for
 *  the current location.
 *
 *  This function can be used to test if a location's co-ordinate
 *  system set-up supports datum transformation.
 *
 * \param name      Pointer to a pointer which will have memory
 *                  allocated and into which a string containing the
 *                  datum name (if present) will be placed. Otherwise
 *                  set to NULL.
 *
 * \param params    Pointer to a pointer which will have memory
 *                  allocated and into which a string containing
 *                  the datum parameters (if present) will
 *                  be placed. Otherwise set to NULL.
 *
 * \return  -1 error or no datum information found,
 *           1 only datum name found, 2 params found
 *
 **/

int GPJ_get_datum_params(char **name, char **params)
{
    int ret;
    struct Key_Value *proj_keys = G_get_projinfo();

    ret = GPJ__get_datum_params(proj_keys, name, params);
    G_free_key_value(proj_keys);

    return ret;
}

/**
 *
 * \brief Extract the datum transformation-related parameters from a
 *  set of general PROJ_INFO parameters.
 *
 *  This function can be used to test if a location's co-ordinate
 *  system set-up supports datum transformation.
 *
 * \param projinfo  Set of key_value pairs containing
 *                  projection information in PROJ_INFO file
 *                  format
 *
 * \param datumname Pointer to a pointer which will have memory
 *                  allocated and into which a string containing the
 *                  datum name (if present) will be placed. Otherwise
 *                  set to NULL.
 *
 * \param params    Pointer to a pointer which will have memory
 *                  allocated and into which a string containing
 *                  the datum parameters (if present) will
 *                  be placed. Otherwise set to NULL.
 *
 * \return  -1 error or no datum information found,
 *           1 only datum name found, 2 params found
 *
 **/

int GPJ__get_datum_params(const struct Key_Value *projinfo, char **datumname,
                          char **params)
{
    int returnval = -1;

    if (NULL != G_find_key_value("datum", projinfo)) {
        *datumname = G_store(G_find_key_value("datum", projinfo));
        G_debug(3, "GPJ__get_datum_params: datumname: <%s>",
                G_find_key_value("datum", projinfo));
        returnval = 1;
    }
    else
        *datumname = NULL;

    if (G_find_key_value("datumparams", projinfo) != NULL) {
        *params = G_store(G_find_key_value("datumparams", projinfo));
        G_debug(3, "GPJ__get_datum_params: datumparams: <%s>",
                G_find_key_value("datumparams", projinfo));
        returnval = 2;
    }
    else if (G_find_key_value("nadgrids", projinfo) != NULL) {
        /* 1. beware of '@', do not create something like
         *    /usr/share/proj/@null, correct is @null or
         *    @/usr/share/proj/null
         * 2. do not add path to the grid, there might already be a
         *    path, and it is safer to use pj_set_finder with PROJ.4 in
         *    datum.c */

        G_asprintf(params, "nadgrids=%s",
                   G_find_key_value("nadgrids", projinfo));

        returnval = 2;
    }
    else if (G_find_key_value("towgs84", projinfo) != NULL) {
        G_asprintf(params, "towgs84=%s", G_find_key_value("towgs84", projinfo));
        returnval = 2;
    }
    else if (G_find_key_value("dx", projinfo) != NULL &&
             G_find_key_value("dy", projinfo) != NULL &&
             G_find_key_value("dz", projinfo) != NULL) {
        G_asprintf(params, "towgs84=%s,%s,%s", G_find_key_value("dx", projinfo),
                   G_find_key_value("dy", projinfo),
                   G_find_key_value("dz", projinfo));
        returnval = 2;
    }
    else
        *params = NULL;

    return returnval;
}

/**
 * \brief Internal function to find all possible sets of
 *        transformation parameters for a particular datum
 *
 * \param inputname   String containing the datum name we
 *                    are going to look up parameters for
 *
 * \return   Pointer to struct gpj_datum_transform_list (a linked
 *           list containing transformation parameters),
 *           or NULL if no suitable parameters were found.
 **/

struct gpj_datum_transform_list *
GPJ_get_datum_transform_by_name(const char *inputname)
{
    FILE *fd;
    char file[GPATH_MAX];
    char buf[1024];
    int line;
    struct gpj_datum_transform_list *current = NULL, *outputlist = NULL;
    struct gpj_datum dstruct;
    int count = 0;

    GPJ_get_datum_by_name(inputname, &dstruct);
    if (dstruct.dx < 99999 && dstruct.dy < 99999 && dstruct.dz < 99999) {
        /* Include the old-style dx dy dz parameters from datum.table at the
         * start of the list, unless these have been set to all 99999 to
         * indicate only entries in datumtransform.table should be used */
        if (current == NULL)
            current = outputlist =
                G_malloc(sizeof(struct gpj_datum_transform_list));
        else
            current = current->next =
                G_malloc(sizeof(struct gpj_datum_transform_list));
        G_asprintf(&(current->params), "towgs84=%.3f,%.3f,%.3f", dstruct.dx,
                   dstruct.dy, dstruct.dz);
        G_asprintf(&(current->where_used), "whole %s region", inputname);
        G_asprintf(&(current->comment),
                   "Default 3-Parameter Transformation (May not be optimum for "
                   "older datums; use this only if no more appropriate options "
                   "are available.)");
        count++;
        current->count = count;
        current->next = NULL;
    }
    GPJ_free_datum(&dstruct);

    /* Now check for additional parameters in datumtransform.table */

    sprintf(file, "%s%s", G_gisbase(), DATUMTRANSFORMTABLE);

    fd = fopen(file, "r");
    if (!fd) {
        G_warning(_("Unable to open datum table file <%s>"), file);
        return outputlist;
    }

    for (line = 1; G_getl2(buf, sizeof(buf), fd); line++) {
        char name[100], params[1024], where_used[1024], comment[1024];

        G_strip(buf);
        if (*buf == '\0' || *buf == '#')
            continue;

        if (sscanf(buf, "%99s \"%1023[^\"]\" \"%1023[^\"]\" \"%1023[^\"]\"",
                   name, params, where_used, comment) != 4) {
            G_warning(_("Error in datum table file <%s>, line %d"), file, line);
            continue;
        }

        if (G_strcasecmp(inputname, name) == 0) {
            /* If the datum name in this line matches the one we are
             * looking for, add an entry to the linked list */
            if (current == NULL)
                current = outputlist =
                    G_malloc(sizeof(struct gpj_datum_transform_list));
            else
                current = current->next =
                    G_malloc(sizeof(struct gpj_datum_transform_list));
            current->params = G_store(params);
            current->where_used = G_store(where_used);
            current->comment = G_store(comment);
            count++;
            current->count = count;
            current->next = NULL;
        }
    }

    fclose(fd);

    return outputlist;
}

/**
 * \brief Free the memory used by a gpj_datum_transform_list struct
 *
 * \param item gpj_datum_transform_list struct to be freed
 **/

void GPJ_free_datum_transform(struct gpj_datum_transform_list *item)
{
    G_free(item->params);
    G_free(item->where_used);
    G_free(item->comment);
    G_free(item);
    return;
}

/**
 * \brief Read the current GRASS datum.table from disk and store in
 *        memory
 *
 * The datum information is stored in a datum_list linked list structure.
 *
 * \return Pointer to first datum_list element in linked list, or NULL
 *         if unable to open datum.table file
 **/

struct datum_list *read_datum_table(void)
{
    FILE *fd;
    char file[GPATH_MAX];
    char buf[4096];
    int line;
    struct datum_list *current = NULL, *outputlist = NULL;

    sprintf(file, "%s%s", G_gisbase(), DATUMTABLE);

    fd = fopen(file, "r");
    if (!fd) {
        G_warning(_("Unable to open datum table file <%s>"), file);
        return NULL;
    }

    for (line = 1; G_getl2(buf, sizeof(buf), fd); line++) {
        char name[100], descr[1024], ellps[100];
        double dx, dy, dz;

        G_strip(buf);
        if (*buf == '\0' || *buf == '#')
            continue;

        if (sscanf(buf, "%s \"%1023[^\"]\" %s dx=%lf dy=%lf dz=%lf", name,
                   descr, ellps, &dx, &dy, &dz) != 6) {
            G_warning(_("Error in datum table file <%s>, line %d"), file, line);
            continue;
        }

        if (current == NULL)
            current = outputlist = G_malloc(sizeof(struct datum_list));
        else
            current = current->next = G_malloc(sizeof(struct datum_list));
        current->name = G_store(name);
        current->longname = G_store(descr);
        current->ellps = G_store(ellps);
        current->dx = dx;
        current->dy = dy;
        current->dz = dz;
        current->next = NULL;
    }

    fclose(fd);

    return outputlist;
}

/**
 * \brief Free the memory used for the strings in a gpj_datum struct
 *
 * \param dstruct gpj_datum struct to be freed
 **/

void GPJ_free_datum(struct gpj_datum *dstruct)
{
    G_free(dstruct->name);
    G_free(dstruct->longname);
    G_free(dstruct->ellps);
    return;
}

/**
 * \brief Free the memory used by a datum_list linked list structure
 *
 * \param dstruct datum_list struct to be freed
 **/

void free_datum_list(struct datum_list *dstruct)
{
    struct datum_list *old;

    while (dstruct != NULL) {
        G_free(dstruct->name);
        G_free(dstruct->longname);
        G_free(dstruct->ellps);
        old = dstruct;
        dstruct = old->next;
        G_free(old);
    }

    return;
}