rd_cellhd.c

Go to the documentation of this file.
00001 /* read cell header, or window.
00002    returns NULL if ok, error message otherwise
00003    note:  the error message can be freed using G_free ().
00004 */
00005 
00006 #include <grass/gis.h>
00007 #include <grass/glocale.h>
00008 #include <string.h>
00009 
00010 #define ERROR(x,line) return error(x,line)
00011 static int scan_item(char *,char *,char *);
00012 static int scan_int(char *,int *);
00013 static double scan_double(char *,double *);
00014 static char *error(char *,int);
00015 
00016 #define F_PROJ   1
00017 #define F_ZONE   2
00018 #define F_NORTH  3
00019 #define F_SOUTH  4
00020 #define F_EAST   5
00021 #define F_WEST   6
00022 #define F_EWRES  7
00023 #define F_NSRES  8
00024 #define F_FORMAT 9
00025 #define F_COMP   10
00026 #define F_COLS   11
00027 #define F_ROWS   12
00028 
00029 #define F_EWRES3 13
00030 #define F_NSRES3 14
00031 #define F_COLS3  15
00032 #define F_ROWS3  16
00033 #define F_TOP    17
00034 #define F_BOTTOM 18
00035 #define F_TBRES  19
00036 #define F_DEPTHS 20
00037 
00038 #define SET(x) flags|=(1<<x)
00039 #define TEST(x) (flags&(1<<x))
00040 
00041 char *G__read_Cell_head_array ( char **array,
00042     struct Cell_head *cellhd,int is_cellhd);
00043 
00044 char *G__read_Cell_head ( FILE *fd,
00045     struct Cell_head *cellhd,int is_cellhd)
00046 {
00047     int count;
00048     char *result, **array;
00049     char buf[1024];
00050 
00051     G_debug ( 2, "G__read_Cell_head" );
00052 
00053     /* Count lines */
00054     count = 0;
00055     fseek (fd, 0L, 0);
00056     while ( G_getl (buf, sizeof (buf), fd) ) count++;
00057 
00058     array = (char**) G_calloc ( count+1, sizeof(char**) );
00059 
00060     count = 0;
00061     fseek (fd, 0L, 0);
00062     while ( G_getl (buf, sizeof (buf), fd) )
00063     {
00064         array[count] = G_store(buf );
00065         count++;
00066     }
00067 
00068     result = G__read_Cell_head_array ( array, cellhd, is_cellhd);
00069 
00070     count = 0;
00071     while ( array[count] )
00072     {
00073         G_free ( array[count] );
00074         count++;
00075     }
00076     G_free ( array );
00077     
00078     return result;
00079 }
00080 
00081 /* Read window from NULL terminated array of strings */ 
00082 char *G__read_Cell_head_array ( char **array,
00083     struct Cell_head *cellhd,int is_cellhd)
00084 {
00085     char *buf;
00086     char label[200];
00087     char value[200];
00088     int i, line;
00089     int flags;
00090     char *G_adjust_Cell_head();
00091     char *err;
00092 
00093     G_debug ( 2, "G__read_Cell_head_array" );
00094 
00095     flags = 0;
00096 
00097 /* initialize the cell header */
00098     cellhd->format  = 0 ;
00099     cellhd->rows    = 0 ;
00100     cellhd->rows3   = 0 ;
00101     cellhd->cols    = 0 ;
00102     cellhd->cols3   = 0 ;
00103     cellhd->depths  = 1 ;
00104     cellhd->proj    = -1 ;
00105     cellhd->zone    = -1 ;
00106     cellhd->compressed = -1;
00107     cellhd->ew_res  = 0.0 ;
00108     cellhd->ew_res3 = 1.0 ;
00109     cellhd->ns_res  = 0.0 ;
00110     cellhd->ns_res3 = 1.0 ;
00111     cellhd->tb_res = 1.0 ;
00112     cellhd->north   = 0.0 ;
00113     cellhd->south   = 0.0 ;
00114     cellhd->east    = 0.0 ;
00115     cellhd->west    = 0.0 ;
00116     cellhd->top     = 1.0 ;
00117     cellhd->bottom  = 0.0 ;
00118 
00119 /* determine projection, zone first */
00120 
00121     i = 0;
00122     for (line = 1; (buf = array[i++]); line++)
00123     {
00124         if (TEST(F_PROJ) && TEST(F_ZONE))
00125             break;
00126 
00127         switch (scan_item (buf, label, value))
00128         {
00129         case -1: ERROR(buf,line);
00130         case 0:  continue;
00131         case 1:  break;
00132         }
00133         if (strncmp (label, "proj", 4) == 0)
00134         {
00135             if (TEST(F_PROJ))
00136                 ERROR (_("duplicate projection field"), line);
00137 
00138             if (!scan_int (value, &cellhd->proj))
00139                 ERROR(buf,line);
00140 
00141             SET(F_PROJ);
00142             continue;
00143         }
00144         if (strncmp (label, "zone", 4) == 0)
00145         {
00146             if (TEST(F_ZONE))
00147                 ERROR (_("duplicate zone field"), line);
00148 
00149             if (!scan_int (value, &cellhd->zone))
00150                 ERROR(buf,line);
00151 
00152             SET(F_ZONE);
00153             continue;
00154         }
00155     }
00156     if (!TEST(F_PROJ))
00157         ERROR (_("projection field missing"),0);
00158     if (!TEST(F_ZONE))
00159         ERROR (_("zone field missing"),0);
00160 
00161 /* read the other info */
00162     i = 0;
00163     for (line = 1; (buf = array[i++]); line++)
00164     {
00165         G_debug ( 3, "region item: %s", buf );
00166         switch (scan_item (buf, label, value))
00167         {
00168         case -1: ERROR(buf,line);
00169         case 0:  continue;
00170         case 1:  break;
00171         }
00172             
00173         if (strncmp (label, "proj", 4) == 0)
00174             continue;
00175         if (strncmp (label, "zone", 4) == 0)
00176             continue;
00177 
00178         if (strncmp (label, "nort", 4) == 0)
00179         {
00180             if (TEST(F_NORTH))
00181                 ERROR(_("duplicate north field"), line);
00182             if (!G_scan_northing (value, &cellhd->north, cellhd->proj))
00183                 ERROR(buf,line);
00184             SET(F_NORTH);
00185             continue;
00186         }
00187         if (strncmp (label, "sout", 4) == 0)
00188         {
00189             if (TEST(F_SOUTH))
00190                 ERROR(_("duplicate south field"), line);
00191             if (!G_scan_northing (value, &cellhd->south, cellhd->proj))
00192                 ERROR(buf,line);
00193             SET(F_SOUTH);
00194             continue;
00195         }
00196         if (strncmp (label, "east", 4) == 0)
00197         {
00198             if (TEST(F_EAST))
00199                 ERROR(_("duplicate east field"), line);
00200             if (!G_scan_easting (value, &cellhd->east, cellhd->proj))
00201                 ERROR(buf,line);
00202             SET(F_EAST);
00203             continue;
00204         }
00205         if (strncmp (label, "west", 4) == 0)
00206         {
00207             if (TEST(F_WEST))
00208                 ERROR(_("duplicate west field"), line);
00209             if (!G_scan_easting (value, &cellhd->west, cellhd->proj))
00210                 ERROR(buf,line);
00211             SET(F_WEST);
00212             continue;
00213         }
00214         if (strncmp (label, "top", 3) == 0)
00215         {
00216             if (TEST(F_TOP))
00217                 ERROR(_("duplicate top field"), line);
00218             if (!scan_double (value, &cellhd->top))
00219                 ERROR(buf,line);
00220             SET(F_TOP);
00221             continue;
00222         }
00223         if (strncmp (label, "bottom", 6) == 0)
00224         {
00225             if (TEST(F_BOTTOM))
00226                 ERROR(_("duplicate bottom field"), line);
00227             if (!scan_double (value, &cellhd->bottom))
00228                 ERROR(buf,line);
00229             SET(F_BOTTOM);
00230             continue;
00231         }
00232         if (strncmp (label, "e-w ", 4) == 0 && strlen(label) == 9 )
00233         {
00234             if (TEST(F_EWRES))
00235                 ERROR(_("duplicate e-w resolution field"), line);
00236             if (!G_scan_resolution (value, &cellhd->ew_res, cellhd->proj))
00237                 ERROR(buf,line);
00238             if (cellhd->ew_res <= 0.0)
00239                 ERROR(buf,line);
00240             SET(F_EWRES);
00241             continue;
00242         }
00243         if (strncmp (label, "e-w resol3", 10) == 0)
00244         {
00245             if (TEST(F_EWRES3))
00246                 ERROR(_("duplicate 3D e-w resolution field"), line);
00247             if (!G_scan_resolution (value, &cellhd->ew_res3, cellhd->proj))
00248                 ERROR(buf,line);
00249             if (cellhd->ew_res3 <= 0.0)
00250                 ERROR(buf,line);
00251             SET(F_EWRES3);
00252             continue;
00253         }
00254         if (strncmp (label, "n-s ", 4) == 0 && strlen(label) == 9 )
00255         {
00256             if (TEST(F_NSRES))
00257                 ERROR(_("duplicate n-s resolution field"), line);
00258             if (!G_scan_resolution (value, &cellhd->ns_res, cellhd->proj))
00259                 ERROR(buf,line);
00260             if (cellhd->ns_res <= 0.0)
00261                 ERROR(buf,line);
00262             SET(F_NSRES);
00263             continue;
00264         }
00265         if (strncmp (label, "n-s resol3", 10) == 0 )
00266         {
00267             if (TEST(F_NSRES3))
00268                 ERROR(_("duplicate 3D n-s resolution field"), line);
00269             if (!G_scan_resolution (value, &cellhd->ns_res3, cellhd->proj))
00270                 ERROR(buf,line);
00271             if (cellhd->ns_res3 <= 0.0)
00272                 ERROR(buf,line);
00273             SET(F_NSRES3);
00274             continue;
00275         }
00276         if (strncmp (label, "t-b ", 4) == 0 )
00277         {
00278             if (TEST(F_TBRES))
00279                 ERROR(_("duplicate t-b resolution field"), line);
00280             if (!scan_double (value, &cellhd->tb_res))
00281                 ERROR(buf,line);
00282             if (cellhd->tb_res <= 0.0)
00283                 ERROR(buf,line);
00284             SET(F_TBRES);
00285             continue;
00286         }
00287         if (strncmp (label, "rows", 4) == 0 && strlen(label) == 4 )
00288         {
00289             if (TEST(F_ROWS))
00290                 ERROR(_("duplicate rows field"), line);
00291             if (!scan_int (value, &cellhd->rows))
00292                 ERROR (buf, line);
00293             if (cellhd->rows <= 0)
00294                 ERROR (buf, line);
00295             SET(F_ROWS);
00296             continue;
00297         }
00298         if (strncmp (label, "rows3", 5) == 0)
00299         {
00300             if (TEST(F_ROWS3))
00301                 ERROR(_("duplicate 3D rows field"), line);
00302             if (!scan_int (value, &cellhd->rows3))
00303                 ERROR (buf, line);
00304             if (cellhd->rows3 <= 0)
00305                 ERROR (buf, line);
00306             SET(F_ROWS3);
00307             continue;
00308         }
00309         if (strncmp (label, "cols", 4) == 0 && strlen(label) == 4 )
00310         {
00311             if (TEST(F_COLS))
00312                 ERROR(_("duplicate cols field"), line);
00313             if (!scan_int (value, &cellhd->cols))
00314                 ERROR (buf, line);
00315             if (cellhd->cols <= 0)
00316                 ERROR (buf, line);
00317             SET(F_COLS);
00318             continue;
00319         }
00320         if (strncmp (label, "cols3", 5) == 0)
00321         {
00322             if (TEST(F_COLS3))
00323                 ERROR(_("duplicate 3D cols field"), line);
00324             if (!scan_int (value, &cellhd->cols3))
00325                 ERROR (buf, line);
00326             if (cellhd->cols3 <= 0)
00327                 ERROR (buf, line);
00328             SET(F_COLS3);
00329             continue;
00330         }
00331         if (strncmp (label, "depths", 6) == 0)
00332         {
00333             if (TEST(F_DEPTHS))
00334                 ERROR(_("duplicate depths field"), line);
00335             if (!scan_int (value, &cellhd->depths))
00336                 ERROR (buf, line);
00337             if (cellhd->depths <= 0)
00338                 ERROR (buf, line);
00339             SET(F_DEPTHS);
00340             continue;
00341         }
00342         if (strncmp (label, "form", 4) == 0)
00343         {
00344             if (TEST(F_FORMAT))
00345                 ERROR(_("duplicate format field"), line);
00346             if (!scan_int (value, &cellhd->format))
00347                 ERROR(buf,line);
00348             SET(F_FORMAT);
00349             continue;
00350         }
00351         if (strncmp (label, "comp", 4) == 0)
00352         {
00353             if (TEST(F_COMP))
00354                 ERROR(_("duplicate compressed field"), line);
00355             if (!scan_int (value, &cellhd->compressed))
00356                 ERROR(buf,line);
00357             SET(F_COMP);
00358             continue;
00359         }
00360         ERROR(buf,line);
00361     }
00362 
00363 /* check some of the fields */
00364     if (!TEST(F_NORTH))
00365         ERROR (_("north field missing"),0);
00366     if (!TEST(F_SOUTH))
00367         ERROR (_("south field missing"),0);
00368     if (!TEST(F_WEST))
00369         ERROR (_("west field missing"),0);
00370     if (!TEST(F_EAST))
00371         ERROR (_("east field missing"),0);
00372     if (!TEST(F_EWRES) && !TEST(F_COLS))
00373         ERROR (_("cols field missing"),0);
00374     if (!TEST(F_NSRES) && !TEST(F_ROWS))
00375         ERROR (_("rows field missing"),0);
00376 /* This next stmt is commented out to allow wr_cellhd.c to write
00377  * headers that will be readable by GRASS 3.1
00378     if ((TEST(F_ROWS) && TEST(F_NSRES))
00379     ||  (TEST(F_COLS) && TEST(F_EWRES)))
00380         ERROR ("row/col and resolution information can not both appear ",0);
00381  */
00382 
00383     /* 3D defined? */
00384     if ( TEST(F_EWRES3) || TEST(F_NSRES3) || TEST(F_COLS3) || TEST(F_ROWS3) ) {
00385         if (!TEST(F_EWRES3))
00386             ERROR (_("ewres3 field missing"),0);
00387         if (!TEST(F_NSRES3))
00388             ERROR (_("nsres3 field missing"),0);
00389         if (!TEST(F_COLS3))
00390             ERROR (_("cols3 field missing"),0);
00391         if (!TEST(F_ROWS3))
00392             ERROR (_("rows3 field missing"),0);
00393     } else { /* use 2D */
00394             cellhd->ew_res3 = cellhd->ew_res;
00395             cellhd->ns_res3 = cellhd->ns_res;
00396             cellhd->cols3 = cellhd->cols;
00397             cellhd->rows3 = cellhd->rows;
00398     }
00399 
00400 /* Adjust and complete the cell header  */
00401     if((err = G_adjust_Cell_head(cellhd, TEST(F_ROWS), TEST(F_COLS))))
00402         ERROR (err,0);
00403 
00404 
00405     return NULL;
00406 }
00407 
00408 static int scan_item(char *buf, char *label, char *value)
00409 {
00410 /* skip blank lines */
00411     if (sscanf (buf, "%1s", label) != 1)
00412         return 0;
00413 
00414 /* skip comment lines */
00415     if (*label == '#')
00416         return 0;
00417 
00418 /* must be label: value */
00419     if (sscanf (buf, "%[^:]:%[^\n]", label, value) != 2)
00420         return -1;
00421             
00422     G_strip (label);
00423     G_strip (value);
00424     return 1;
00425 }
00426 
00427 static int scan_int(char *buf, int *n)
00428 {
00429     char dummy[3];
00430 
00431     *dummy = 0;
00432     return (sscanf (buf, "%d%1s", n, dummy) == 1 && *dummy == 0);
00433 }
00434 
00435 static double scan_double(char *buf, double *n)
00436 {
00437     char dummy[3];
00438 
00439     *dummy = 0;
00440     return (sscanf (buf, "%lf%1s", n, dummy) == 1 && *dummy == 0);
00441 }
00442 
00443 static char *error( char *msg, int line)
00444 {
00445     char buf[1024];
00446 
00447     if (line)
00448         sprintf (buf, _("line %d: <%s>"), line, msg);
00449     else
00450         sprintf (buf, "<%s>", msg);
00451 
00452     return G_store(buf);
00453 }

Generated on Wed Dec 19 14:59:06 2007 for GRASS by  doxygen 1.5.4