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