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(char *, char *, char *, int);
00006 static int equal(char *, 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     char *G__unit_name();
00026 
00027     switch(n=G_projection())
00028     {
00029     case PROJECTION_XY:
00030     case PROJECTION_UTM:
00031     case PROJECTION_LL:
00032     case PROJECTION_SP:
00033         return G__unit_name(G__projection_units(n), plural);
00034     }
00035 
00036     if(!lookup (UNIT_FILE, plural?"units":"unit", name, sizeof(name)))
00037         strcpy (name, plural ? "units" : "unit");
00038     return name;
00039 }
00040 
00041 
00053 char *G_database_projection_name()
00054 {
00055     int n;
00056     static char name[256];
00057     char *G__projection_name();
00058 
00059     switch(n=G_projection())
00060     {
00061     case PROJECTION_XY:
00062     case PROJECTION_UTM:
00063     case PROJECTION_LL:
00064     case PROJECTION_SP:
00065         return G__projection_name(n);
00066     }
00067     if(!lookup (PROJECTION_FILE, "name", name, sizeof(name)))
00068         strcpy (name, _("Unknown projection"));
00069     return name;
00070 }
00071 
00072 
00084 double G_database_units_to_meters_factor()
00085 {
00086     char *unit;
00087     double factor;
00088     char buf[256];
00089     int n;
00090 
00091     static struct
00092     {
00093         char *unit;
00094         double factor;
00095     } table[] =
00096     {
00097         {"unit", 1.0},
00098         {"meter", 1.0},
00099         {"foot", .3048},
00100         {"inch", .0254},
00101         {NULL, 0.0}
00102     };
00103 
00104     factor = 0.0;
00105     if (lookup(UNIT_FILE, "meters", buf, sizeof(buf)))
00106         sscanf (buf, "%lf", &factor);
00107     if (factor <= 0.0)
00108     {
00109         unit = G_database_unit_name(0);
00110         for (n=0; table[n].unit; n++)
00111             if (equal(unit, table[n].unit))
00112             {
00113                 factor = table[n].factor;
00114                 break;
00115             }
00116     }
00117     return factor;
00118 }
00119 
00120 /***********************************************************************
00121  * G_database_datum_name()
00122  *
00123  * return name of datum of current database
00124  *
00125  * returns pointer to valid name if ok
00126  * NULL otherwise
00127  ***********************************************************************/
00128  
00129 
00140 char *G_database_datum_name()
00141 {
00142   static char name[256], params[256];
00143   struct Key_Value *projinfo;
00144   int datumstatus;
00145    
00146   if(lookup (PROJECTION_FILE, "datum", name, sizeof(name)))
00147     return name;
00148   else if( (projinfo = G_get_projinfo()) == NULL )
00149     return NULL;
00150   else
00151     datumstatus = G_get_datumparams_from_projinfo(projinfo, name, params);
00152    
00153   G_free_key_value( projinfo );
00154   if( datumstatus == 2)
00155     return params;
00156   else  
00157     return NULL;
00158 }
00159 
00160 /***********************************************************************
00161  * G_database_ellipse_name()
00162  *
00163  * return name of ellipsoid of current database
00164  *
00165  * returns pointer to valid name if ok
00166  * NULL otherwise
00167  ***********************************************************************/
00168  
00169 char *G_database_ellipse_name()
00170 {
00171   static char name[256];
00172   
00173   if(!lookup (PROJECTION_FILE, "ellps", name, sizeof(name)))
00174   {
00175     double a, es;     
00176     G_get_ellipsoid_parameters(&a, &es);
00177     sprintf(name, "a=%.16g es=%.16g", a, es);
00178   }
00179 
00180   /* strcpy (name, "Unknown ellipsoid"); */
00181   return name;
00182 }
00183 
00184 static int lookup(char *file, char *key, char *value, int len)
00185 {
00186     char path[1024];
00187 
00188 /*
00189     G__file_name (path, "", file, G_mapset());
00190     if (access(path,0) == 0)
00191         return G_lookup_key_value_from_file(path, key, value, len) == 1;
00192 */
00193     G__file_name (path, "", file, "PERMANENT");
00194     return G_lookup_key_value_from_file(path, key, value, len) == 1;
00195 }
00196 
00197 static int equal(char *a, char *b)
00198 {
00199     if (a == NULL || b == NULL)
00200         return a==b;
00201     while (*a && *b)
00202         if (lower(*a++) != lower(*b++))
00203             return 0;
00204     if (*a || *b)
00205         return 0;
00206     return 1;
00207 }
00208 
00209 static int lower(char c)
00210 {
00211     if (c >= 'A' && c <= 'Z')
00212         c += 'a' - 'A';
00213     return c;
00214 }

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