Loading...
Searching...
No Matches
gis/datum.c
Go to the documentation of this file.
/*
****************************************************************************
*
* MODULE: gis library
* AUTHOR(S): Andreas Lange - andreas.lange@rhein-main.de
* Paul Kelly - paul-grass@stjohnspoint.co.uk
* PURPOSE: provide functions for reading datum parameters from the
* location database.
* COPYRIGHT: (C) 2000, 2003 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.
*
*****************************************************************************/
#define DATUMTABLE "/etc/proj/datum.table"
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <grass/gis.h>
#include <grass/glocale.h>
static struct table {
struct datum {
char *name; /* Short Name / acronym of map datum */
char *descr; /* Long Name for map datum */
char *ellps; /* acronym for ellipsoid used with this datum */
double dx; /* delta x */
double dy; /* delta y */
double dz; /* delta z */
} *datums;
int size;
int count;
int initialized;
} table;
static int compare_table_names(const void *, const void *);
int G_get_datum_by_name(const char *name)
{
int i;
G_read_datum_table();
for (i = 0; i < table.count; i++)
if (G_strcasecmp(name, table.datums[i].name) == 0)
return i;
return -1;
}
const char *G_datum_name(int n)
{
G_read_datum_table();
if (n < 0 || n >= table.count)
return NULL;
return table.datums[n].name;
}
const char *G_datum_description(int n)
{
G_read_datum_table();
if (n < 0 || n >= table.count)
return NULL;
return table.datums[n].descr;
}
const char *G_datum_ellipsoid(int n)
{
G_read_datum_table();
if (n < 0 || n >= table.count)
return NULL;
return table.datums[n].ellps;
}
/***********************************************************
* G_get_datumparams_from_projinfo(projinfo, datumname, params)
* struct Key_Value *projinfo Set of key_value pairs containing
* projection information in PROJ_INFO file
* format
* char *datumname Pointer into which a string containing
* the datum name (if present) will be
* placed.
* char *params Pointer into which a string containing
* the datum parameters (if present) will
* be placed.
*
* Extract the datum transformation-related parameters from a
* set of general PROJ_INFO parameters.
* This function can be used to test if a location set-up
* supports datum transformation.
*
* returns: -1 error or no datum information found,
* 1 only datum name found, 2 params found
************************************************************/
int G_get_datumparams_from_projinfo(const struct Key_Value *projinfo,
char *datumname, char *params)
{
int returnval = -1;
if (NULL != G_find_key_value("datum", projinfo)) {
sprintf(datumname, "%s", G_find_key_value("datum", projinfo));
returnval = 1;
}
if (G_find_key_value("datumparams", projinfo) != NULL) {
sprintf(params, "%s", G_find_key_value("datumparams", projinfo));
returnval = 2;
}
else if (G_find_key_value("nadgrids", projinfo) != NULL) {
sprintf(params, "nadgrids=%s", G_find_key_value("nadgrids", projinfo));
returnval = 2;
}
else if (G_find_key_value("towgs84", projinfo) != NULL) {
sprintf(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) {
sprintf(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;
}
return returnval;
}
void G_read_datum_table(void)
{
FILE *fd;
char file[GPATH_MAX];
char buf[1024];
int line;
if (G_is_initialized(&table.initialized))
return;
snprintf(file, sizeof(file), "%s%s", G_gisbase(), DATUMTABLE);
fd = fopen(file, "r");
if (!fd) {
G_warning(_("unable to open datum table file: %s"), file);
G_initialize_done(&table.initialized);
return;
}
for (line = 1; G_getl2(buf, sizeof(buf), fd); line++) {
char name[100], descr[100], ellps[100];
struct datum *t;
G_strip(buf);
if (*buf == '\0' || *buf == '#')
continue;
if (table.count >= table.size) {
table.size += 50;
table.datums =
G_realloc(table.datums, table.size * sizeof(struct datum));
}
t = &table.datums[table.count];
if (sscanf(buf, "%s \"%99[^\"]\" %s dx=%lf dy=%lf dz=%lf", name, descr,
ellps, &t->dx, &t->dy, &t->dz) != 6) {
G_warning(_("error in datum table file, line %d"), line);
continue;
}
t->name = G_store(name);
t->descr = G_store(descr);
t->ellps = G_store(ellps);
table.count++;
}
qsort(table.datums, table.count, sizeof(struct datum), compare_table_names);
G_initialize_done(&table.initialized);
fclose(fd);
}
static int compare_table_names(const void *aa, const void *bb)
{
const struct datum *a = aa;
const struct datum *b = bb;
return G_strcasecmp(a->name, b->name);
}
NULL
#define NULL
Definition ccmath.h:32
G_initialize_done
void G_initialize_done(int *p)
Definition counter.c:77
G_is_initialized
int G_is_initialized(int *p)
Definition counter.c:60
b
double b
Definition driver/set_window.c:5
t
double t
Definition driver/set_window.c:5
G_getl2
int G_getl2(char *buf, int n, FILE *fd)
Gets a line of text from a file of any pedigree.
Definition getl.c:60
G_datum_description
const char * G_datum_description(int n)
Definition gis/datum.c:66
G_get_datumparams_from_projinfo
int G_get_datumparams_from_projinfo(const struct Key_Value *projinfo, char *datumname, char *params)
Definition gis/datum.c:107
DATUMTABLE
#define DATUMTABLE
Definition gis/datum.c:17
G_get_datum_by_name
int G_get_datum_by_name(const char *name)
Definition gis/datum.c:43
G_read_datum_table
void G_read_datum_table(void)
Definition gis/datum.c:141
G_datum_ellipsoid
const char * G_datum_ellipsoid(int n)
Definition gis/datum.c:76
G_datum_name
const char * G_datum_name(int n)
Definition gis/datum.c:56
G_warning
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition gis/error.c:203
G_gisbase
const char * G_gisbase(void)
Get full path name of the top level module directory.
Definition gisbase.c:39
count
int count
G_find_key_value
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive).
Definition key_value1.c:85
file
#define file
name
const char * name
Definition named_colr.c:6
G_strcasecmp
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower).
Definition strings.c:47
G_store
char * G_store(const char *s)
Copy string to allocated memory.
Definition strings.c:87
G_strip
void G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition strings.c:300