datum.c

Go to the documentation of this file.
00001 /*
00002  * $Id: datum.c,v 2.4 2006/03/31 09:10:04 paul Exp $
00003  *
00004  ****************************************************************************
00005  *
00006  * MODULE:       gis library
00007  * AUTHOR(S):    Andreas Lange - andreas.lange@rhein-main.de
00008  *               Paul Kelly - paul-grass@stjohnspoint.co.uk
00009  * PURPOSE:      provide functions for reading datum parameters from the
00010  *               location database.     
00011  * COPYRIGHT:    (C) 2000, 2003 by the GRASS Development Team
00012  *
00013  *               This program is free software under the GNU General Public
00014  *               License (>=v2). Read the file COPYING that comes with GRASS
00015  *               for details.
00016  *
00017  *****************************************************************************/
00018 
00019 #define DATUMTABLE "/etc/datum.table"
00020 
00021 #include <unistd.h>
00022 #include <string.h>
00023 #include <ctype.h>
00024 #include <stdlib.h>
00025 
00026 #include <grass/gis.h>
00027 #include <grass/glocale.h>
00028 
00029 static struct table
00030 {
00031     char *name;   /* Short Name / acronym of map datum */
00032     char *descr;  /* Long Name for map datum */
00033     char *ellps;  /* acronym for ellipsoid used with this datum */
00034     double dx;    /* delta x */
00035     double dy;    /* delta y */
00036     double dz;    /* delta z */
00037 } *table;
00038 
00039 static int size;
00040 static int count = -1;
00041 
00042 static int compare_table_names(const void *, const void *);
00043 static void read_datum_table(void);
00044 
00045 int 
00046 G_get_datum_by_name(const char *name)
00047 {
00048     int i;
00049 
00050     read_datum_table();
00051 
00052     for (i = 0; i < count; i++)
00053         if (G_strcasecmp(name, table[i].name) == 0)
00054             return i;
00055 
00056     return -1;
00057 }
00058 
00059 char *
00060 G_datum_name(int n)
00061 {
00062     read_datum_table();
00063 
00064     if (n < 0 || n >= count) 
00065         return NULL; 
00066 
00067     return table[n].name;
00068 }
00069 
00070 char *
00071 G_datum_description(int n)
00072 { 
00073     read_datum_table();
00074   
00075     if (n < 0 || n >= count)
00076         return NULL;
00077 
00078     return table[n].descr;
00079 }
00080 
00081 char *
00082 G_datum_ellipsoid(int n)
00083 {
00084     read_datum_table();
00085 
00086     if (n < 0 || n >= count)
00087         return NULL;
00088 
00089     return table[n].ellps;
00090 }
00091 
00092 /***********************************************************
00093  *  G_get_datumparams_from_projinfo(projinfo, datumname, params)
00094  *     struct Key_Value *projinfo Set of key_value pairs containing
00095  *                       projection information in PROJ_INFO file
00096  *                       format
00097  *     char *datumname   Pointer into which a string containing
00098  *                       the datum name (if present) will be
00099  *                       placed.
00100  *     char *params      Pointer into which a string containing
00101  *                       the datum parameters (if present) will
00102  *                       be placed.
00103  *
00104  *  Extract the datum transformation-related parameters from a 
00105  *  set of general PROJ_INFO parameters.
00106  *  This function can be used to test if a location set-up 
00107  *  supports datum transformation.
00108  *  
00109  *  returns: -1 error or no datum information found, 
00110  *           1 only datum name found, 2 params found
00111  ************************************************************/
00112 
00113 int G_get_datumparams_from_projinfo(struct Key_Value *projinfo, 
00114                                     char *datumname, char *params)
00115 {
00116     int returnval = -1;
00117    
00118     if( NULL != G_find_key_value("datum", projinfo) )
00119     {
00120         sprintf(datumname, G_find_key_value("datum", projinfo));
00121         returnval = 1;
00122     }
00123           
00124     if( G_find_key_value("datumparams", projinfo) != NULL )
00125     {
00126         sprintf(params, G_find_key_value("datumparams", projinfo));
00127         returnval = 2;
00128     }
00129     else if( G_find_key_value("nadgrids", projinfo) != NULL )
00130     {
00131         sprintf(params, "nadgrids=%s", G_find_key_value("nadgrids", projinfo));
00132         returnval = 2;
00133     }
00134     else if( G_find_key_value("towgs84", projinfo) != NULL )
00135     {
00136         sprintf(params, "towgs84=%s", G_find_key_value("towgs84", projinfo));
00137         returnval = 2;
00138     }
00139     else if( G_find_key_value("dx", projinfo) != NULL
00140           && G_find_key_value("dy", projinfo) != NULL
00141           && G_find_key_value("dz", projinfo) != NULL ) 
00142     {
00143         sprintf(params, "towgs84=%s,%s,%s",
00144                 G_find_key_value("dx", projinfo),
00145                 G_find_key_value("dy", projinfo),
00146                 G_find_key_value("dz", projinfo) );
00147         returnval = 2;
00148     }
00149 
00150     return returnval;
00151    
00152 }
00153 
00154 static void
00155 read_datum_table(void) 
00156 {
00157     FILE *fd;
00158     char file[1024];
00159     char buf[1024];
00160     int line;
00161 
00162     if (count >= 0)
00163         return;
00164 
00165     count = 0;
00166 
00167     sprintf(file, "%s%s", G_gisbase(), DATUMTABLE);
00168 
00169     fd = fopen(file, "r");
00170     if (!fd)
00171     {
00172         G_warning(_("unable to open datum table file: %s"), file);
00173         return;
00174     }
00175 
00176     for (line = 1; G_getl(buf, sizeof(buf), fd); line++)
00177     {
00178         char name[100], descr[100], ellps[100];
00179         struct table *t;
00180 
00181         G_strip(buf);
00182         if (*buf == '\0' || *buf == '#')
00183             continue;
00184 
00185         if (count >= size)
00186         {
00187             size += 50;
00188             table = G_realloc(table, size * sizeof(struct table));
00189         }
00190 
00191         t = &table[count];
00192 
00193         if (sscanf(buf, "%s \"%99[^\"]\" %s dx=%lf dy=%lf dz=%lf",
00194                    name, descr, ellps, &t->dx, &t->dy, &t->dz) != 6)
00195         {
00196             G_warning(_("error in datum table file, line %d"), line);
00197             continue;
00198         }
00199 
00200         t->name  = G_store (name);
00201         t->descr = G_store (descr);
00202         t->ellps = G_store (ellps);
00203 
00204         count++;
00205     }
00206  
00207     qsort(table, count, sizeof(struct table), compare_table_names);
00208 }
00209 
00210 static int
00211 compare_table_names(const void *aa, const void *bb)
00212 {
00213     const struct table *a = aa;
00214     const struct table *b = bb;
00215 
00216     return G_strcasecmp(a->name, b->name);
00217 }

Generated on Fri Nov 21 11:02:17 2008 for GRASS by  doxygen 1.5.1