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

Generated on Mon Jan 1 19:49:25 2007 for GRASS by  doxygen 1.5.1