GRASS Programmer's Manual 6.4.1(2011)
proj3.c
Go to the documentation of this file.
00001 #include <string.h>
00002 #include <grass/gis.h>
00003 #include <grass/glocale.h>
00004 
00005 static int lookup(const char *, const char *, char *, int);
00006 static int equal(const char *, const char *);
00007 static int lower(char);
00008 
00009 
00021 char *G_database_unit_name(int plural)
00022 {
00023     int n;
00024     static char name[256];
00025 
00026     switch (n = G_projection()) {
00027     case PROJECTION_XY:
00028     case PROJECTION_UTM:
00029     case PROJECTION_LL:
00030     case PROJECTION_SP:
00031         return G__unit_name(G__projection_units(n), plural);
00032     }
00033 
00034     if (!lookup(UNIT_FILE, plural ? "units" : "unit", name, sizeof(name)))
00035         strcpy(name, plural ? "units" : "unit");
00036     return name;
00037 }
00038 
00039 
00051 char *G_database_projection_name(void)
00052 {
00053     int n;
00054     static char name[256];
00055 
00056     switch (n = G_projection()) {
00057     case PROJECTION_XY:
00058     case PROJECTION_UTM:
00059     case PROJECTION_LL:
00060     case PROJECTION_SP:
00061         return G__projection_name(n);
00062     }
00063     if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
00064         strcpy(name, _("Unknown projection"));
00065     return name;
00066 }
00067 
00068 
00080 double G_database_units_to_meters_factor(void)
00081 {
00082     char *unit;
00083     double factor;
00084     char buf[256];
00085     int n;
00086 
00087     static struct
00088     {
00089         char *unit;
00090         double factor;
00091     } table[] = {
00092         {"unit",  1.0},
00093         {"meter", 1.0},
00094         {"foot", .3048},
00095         {"inch", .0254},
00096         {NULL, 0.0}
00097     };
00098 
00099     factor = 0.0;
00100     if (lookup(UNIT_FILE, "meters", buf, sizeof(buf)))
00101         sscanf(buf, "%lf", &factor);
00102     if (factor <= 0.0) {
00103         unit = G_database_unit_name(0);
00104         for (n = 0; table[n].unit; n++)
00105             if (equal(unit, table[n].unit)) {
00106                 factor = table[n].factor;
00107                 break;
00108             }
00109     }
00110     return factor;
00111 }
00112 
00113 /***********************************************************************
00114  * G_database_datum_name(void)
00115  *
00116  * return name of datum of current database
00117  *
00118  * returns pointer to valid name if ok
00119  * NULL otherwise
00120  ***********************************************************************/
00121 
00122 
00133 char *G_database_datum_name(void)
00134 {
00135     static char name[256], params[256];
00136     struct Key_Value *projinfo;
00137     int datumstatus;
00138 
00139     if (lookup(PROJECTION_FILE, "datum", name, sizeof(name)))
00140         return name;
00141     else if ((projinfo = G_get_projinfo()) == NULL)
00142         return NULL;
00143     else
00144         datumstatus = G_get_datumparams_from_projinfo(projinfo, name, params);
00145 
00146     G_free_key_value(projinfo);
00147     if (datumstatus == 2)
00148         return params;
00149     else
00150         return NULL;
00151 }
00152 
00153 /***********************************************************************
00154  * G_database_ellipse_name(void)
00155  *
00156  * return name of ellipsoid of current database
00157  *
00158  * returns pointer to valid name if ok
00159  * NULL otherwise
00160  ***********************************************************************/
00161 
00162 char *G_database_ellipse_name(void)
00163 {
00164     static char name[256];
00165 
00166     if (!lookup(PROJECTION_FILE, "ellps", name, sizeof(name))) {
00167         double a, es;
00168 
00169         G_get_ellipsoid_parameters(&a, &es);
00170         sprintf(name, "a=%.16g es=%.16g", a, es);
00171     }
00172 
00173     /* strcpy (name, "Unknown ellipsoid"); */
00174     return name;
00175 }
00176 
00177 static int lookup(const char *file, const char *key, char *value, int len)
00178 {
00179     char path[GPATH_MAX];
00180 
00181     /*
00182        G__file_name (path, "", file, G_mapset());
00183        if (access(path,0) == 0)
00184        return G_lookup_key_value_from_file(path, key, value, len) == 1;
00185      */
00186     G__file_name(path, "", file, "PERMANENT");
00187     return G_lookup_key_value_from_file(path, key, value, len) == 1;
00188 }
00189 
00190 static int equal(const char *a, const char *b)
00191 {
00192     if (a == NULL || b == NULL)
00193         return a == b;
00194     while (*a && *b)
00195         if (lower(*a++) != lower(*b++))
00196             return 0;
00197     if (*a || *b)
00198         return 0;
00199     return 1;
00200 }
00201 
00202 static int lower(char c)
00203 {
00204     if (c >= 'A' && c <= 'Z')
00205         c += 'a' - 'A';
00206     return c;
00207 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines