GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2000, 2003 GRASS Development Team
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 *
12 *****************************************************************************/
13
14#define DATUMTABLE "/etc/proj/datum.table"
15
16#include <unistd.h>
17#include <string.h>
18#include <ctype.h>
19#include <stdlib.h>
20
21#include <grass/gis.h>
22#include <grass/glocale.h>
23
24static struct table {
25 struct datum {
26 char *name; /* Short Name / acronym of map datum */
27 char *descr; /* Long Name for map datum */
28 char *ellps; /* acronym for ellipsoid used with this datum */
29 double dx; /* delta x */
30 double dy; /* delta y */
31 double dz; /* delta z */
32 } *datums;
33 int size;
34 int count;
35 int initialized;
36} table;
37
38static int compare_table_names(const void *, const void *);
39
40int G_get_datum_by_name(const char *name)
41{
42 int i;
43
45
46 for (i = 0; i < table.count; i++)
47 if (G_strcasecmp(name, table.datums[i].name) == 0)
48 return i;
49
50 return -1;
51}
52
53const char *G_datum_name(int n)
54{
56
57 if (n < 0 || n >= table.count)
58 return NULL;
59
60 return table.datums[n].name;
61}
62
63const char *G_datum_description(int n)
64{
66
67 if (n < 0 || n >= table.count)
68 return NULL;
69
70 return table.datums[n].descr;
71}
72
73const char *G_datum_ellipsoid(int n)
74{
76
77 if (n < 0 || n >= table.count)
78 return NULL;
79
80 return table.datums[n].ellps;
81}
82
83/***********************************************************
84 * G_get_datumparams_from_projinfo(projinfo, datumname, params)
85 * struct Key_Value *projinfo Set of key_value pairs containing
86 * projection information in PROJ_INFO file
87 * format
88 * char *datumname Pointer into which a string containing
89 * the datum name (if present) will be
90 * placed.
91 * char *params Pointer into which a string containing
92 * the datum parameters (if present) will
93 * be placed.
94 *
95 * Extract the datum transformation-related parameters from a
96 * set of general PROJ_INFO parameters.
97 * This function can be used to test if a location set-up
98 * supports datum transformation.
99 *
100 * returns: -1 error or no datum information found,
101 * 1 only datum name found, 2 params found
102 ************************************************************/
103
105 char *datumname, char *params)
106{
107 int returnval = -1;
108
109 if (NULL != G_find_key_value("datum", projinfo)) {
110 sprintf(datumname, "%s", G_find_key_value("datum", projinfo));
111 returnval = 1;
112 }
113
114 if (G_find_key_value("datumparams", projinfo) != NULL) {
115 sprintf(params, "%s", G_find_key_value("datumparams", projinfo));
116 returnval = 2;
117 }
118 else if (G_find_key_value("nadgrids", projinfo) != NULL) {
119 sprintf(params, "nadgrids=%s", G_find_key_value("nadgrids", projinfo));
120 returnval = 2;
121 }
122 else if (G_find_key_value("towgs84", projinfo) != NULL) {
123 sprintf(params, "towgs84=%s", G_find_key_value("towgs84", projinfo));
124 returnval = 2;
125 }
126 else if (G_find_key_value("dx", projinfo) != NULL &&
127 G_find_key_value("dy", projinfo) != NULL &&
128 G_find_key_value("dz", projinfo) != NULL) {
129 sprintf(params, "towgs84=%s,%s,%s", G_find_key_value("dx", projinfo),
130 G_find_key_value("dy", projinfo),
131 G_find_key_value("dz", projinfo));
132 returnval = 2;
133 }
134
135 return returnval;
136}
137
139{
140 FILE *fd;
141 char file[GPATH_MAX];
142 char buf[1024];
143 int line;
144
145 if (G_is_initialized(&table.initialized))
146 return;
147
148 snprintf(file, sizeof(file), "%s%s", G_gisbase(), DATUMTABLE);
149
150 fd = fopen(file, "r");
151 if (!fd) {
152 G_warning(_("unable to open datum table file: %s"), file);
153 G_initialize_done(&table.initialized);
154 return;
155 }
156
157 for (line = 1; G_getl2(buf, sizeof(buf), fd); line++) {
158 char name[100], descr[100], ellps[100];
159 struct datum *t;
160
161 G_strip(buf);
162 if (*buf == '\0' || *buf == '#')
163 continue;
164
165 if (table.count >= table.size) {
166 table.size += 50;
167 table.datums =
168 G_realloc(table.datums, table.size * sizeof(struct datum));
169 }
170
171 t = &table.datums[table.count];
172
173 if (sscanf(buf, "%s \"%99[^\"]\" %s dx=%lf dy=%lf dz=%lf", name, descr,
174 ellps, &t->dx, &t->dy, &t->dz) != 6) {
175 G_warning(_("error in datum table file, line %d"), line);
176 continue;
177 }
178
179 t->name = G_store(name);
180 t->descr = G_store(descr);
181 t->ellps = G_store(ellps);
182
183 table.count++;
184 }
185
186 qsort(table.datums, table.count, sizeof(struct datum), compare_table_names);
187
188 G_initialize_done(&table.initialized);
189 fclose(fd);
190}
191
192static int compare_table_names(const void *aa, const void *bb)
193{
194 const struct datum *a = aa;
195 const struct datum *b = bb;
196
197 return G_strcasecmp(a->name, b->name);
198}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition getl.c:58
#define G_realloc(p, n)
Definition defs/gis.h:138
void G_warning(const char *,...) __attribute__((format(printf
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition gisbase.c:39
void G_strip(char *)
Removes all leading and trailing white space from string.
Definition strings.c:298
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
Definition key_value1.c:83
int G_is_initialized(int *)
Definition counter.c:60
void G_initialize_done(int *)
Definition counter.c:77
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition strings.c:45
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
const char * G_datum_description(int n)
Definition gis/datum.c:63
int G_get_datumparams_from_projinfo(const struct Key_Value *projinfo, char *datumname, char *params)
Definition gis/datum.c:104
#define DATUMTABLE
Definition gis/datum.c:14
int G_get_datum_by_name(const char *name)
Definition gis/datum.c:40
void G_read_datum_table(void)
Definition gis/datum.c:138
const char * G_datum_ellipsoid(int n)
Definition gis/datum.c:73
const char * G_datum_name(int n)
Definition gis/datum.c:53
#define GPATH_MAX
Definition gis.h:196
#define _(str)
Definition glocale.h:10
int count
#define file
const char * name
Definition named_colr.c:6
double b
Definition r_raster.c:37
double t
Definition r_raster.c:37