GRASS GIS 8 Programmer's Manual 8.3.2(2024)-exported
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 * 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/proj/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
27static struct table {
28 struct datum {
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 } *datums;
36 int size;
37 int count;
38 int initialized;
39} table;
40
41static int compare_table_names(const void *, const void *);
42
43int G_get_datum_by_name(const char *name)
44{
45 int i;
46
48
49 for (i = 0; i < table.count; i++)
50 if (G_strcasecmp(name, table.datums[i].name) == 0)
51 return i;
52
53 return -1;
54}
55
56const char *G_datum_name(int n)
57{
59
60 if (n < 0 || n >= table.count)
61 return NULL;
62
63 return table.datums[n].name;
64}
65
66const char *G_datum_description(int n)
67{
69
70 if (n < 0 || n >= table.count)
71 return NULL;
72
73 return table.datums[n].descr;
74}
75
76const char *G_datum_ellipsoid(int n)
77{
79
80 if (n < 0 || n >= table.count)
81 return NULL;
82
83 return table.datums[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
107int 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", G_find_key_value("nadgrids", projinfo));
123 returnval = 2;
124 }
125 else if (G_find_key_value("towgs84", projinfo) != NULL) {
126 sprintf(params, "towgs84=%s", G_find_key_value("towgs84", projinfo));
127 returnval = 2;
128 }
129 else if (G_find_key_value("dx", projinfo) != NULL &&
130 G_find_key_value("dy", projinfo) != NULL &&
131 G_find_key_value("dz", projinfo) != NULL) {
132 sprintf(params, "towgs84=%s,%s,%s", G_find_key_value("dx", projinfo),
133 G_find_key_value("dy", projinfo),
134 G_find_key_value("dz", projinfo));
135 returnval = 2;
136 }
137
138 return returnval;
139}
140
142{
143 FILE *fd;
144 char file[GPATH_MAX];
145 char buf[1024];
146 int line;
147
148 if (G_is_initialized(&table.initialized))
149 return;
150
151 sprintf(file, "%s%s", G_gisbase(), DATUMTABLE);
152
153 fd = fopen(file, "r");
154 if (!fd) {
155 G_warning(_("unable to open datum table file: %s"), file);
156 G_initialize_done(&table.initialized);
157 return;
158 }
159
160 for (line = 1; G_getl2(buf, sizeof(buf), fd); line++) {
161 char name[100], descr[100], ellps[100];
162 struct datum *t;
163
164 G_strip(buf);
165 if (*buf == '\0' || *buf == '#')
166 continue;
167
168 if (table.count >= table.size) {
169 table.size += 50;
170 table.datums =
171 G_realloc(table.datums, table.size * sizeof(struct datum));
172 }
173
174 t = &table.datums[table.count];
175
176 if (sscanf(buf, "%s \"%99[^\"]\" %s dx=%lf dy=%lf dz=%lf", name, descr,
177 ellps, &t->dx, &t->dy, &t->dz) != 6) {
178 G_warning(_("error in datum table file, line %d"), line);
179 continue;
180 }
181
182 t->name = G_store(name);
183 t->descr = G_store(descr);
184 t->ellps = G_store(ellps);
185
186 table.count++;
187 }
188
189 qsort(table.datums, table.count, sizeof(struct datum), compare_table_names);
190
191 G_initialize_done(&table.initialized);
192}
193
194static int compare_table_names(const void *aa, const void *bb)
195{
196 const struct datum *a = aa;
197 const struct datum *b = bb;
198
199 return G_strcasecmp(a->name, b->name);
200}
#define NULL
Definition ccmath.h:32
void G_initialize_done(int *p)
Definition counter.c:77
int G_is_initialized(int *p)
Definition counter.c:60
double b
double t
int G_getl2(char *buf, int n, FILE *fd)
Gets a line of text from a file of any pedigree.
Definition getl.c:65
const char * G_datum_description(int n)
Definition gis/datum.c:66
int G_get_datumparams_from_projinfo(const struct Key_Value *projinfo, char *datumname, char *params)
Definition gis/datum.c:107
#define DATUMTABLE
Definition gis/datum.c:17
int G_get_datum_by_name(const char *name)
Definition gis/datum.c:43
void G_read_datum_table(void)
Definition gis/datum.c:141
const char * G_datum_ellipsoid(int n)
Definition gis/datum.c:76
const char * G_datum_name(int n)
Definition gis/datum.c:56
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition gis/error.c:203
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition gisbase.c:39
int count
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
Definition key_value1.c:85
#define file
const char * name
Definition named_colr.c:6
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition strings.c:47
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
void G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition strings.c:299