rd_cellhd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* read cell header, or window.
  2. returns NULL if ok, error message otherwise
  3. note: the error message can be freed using G_free ().
  4. */
  5. #include <string.h>
  6. #include <grass/gis.h>
  7. #include <grass/glocale.h>
  8. static int scan_item(const char *, char *, char *);
  9. static int scan_int(const char *, int *);
  10. static double scan_double(const char *, double *);
  11. #define F_PROJ 1
  12. #define F_ZONE 2
  13. #define F_NORTH 3
  14. #define F_SOUTH 4
  15. #define F_EAST 5
  16. #define F_WEST 6
  17. #define F_EWRES 7
  18. #define F_NSRES 8
  19. #define F_FORMAT 9
  20. #define F_COMP 10
  21. #define F_COLS 11
  22. #define F_ROWS 12
  23. #define F_EWRES3 13
  24. #define F_NSRES3 14
  25. #define F_COLS3 15
  26. #define F_ROWS3 16
  27. #define F_TOP 17
  28. #define F_BOTTOM 18
  29. #define F_TBRES 19
  30. #define F_DEPTHS 20
  31. #define SET(x) flags|=(1<<x)
  32. #define TEST(x) (flags&(1<<x))
  33. void G__read_Cell_head(FILE * fd, struct Cell_head *cellhd, int is_cellhd)
  34. {
  35. int count;
  36. char **array;
  37. char buf[1024];
  38. G_debug(2, "G__read_Cell_head");
  39. /* Count lines */
  40. count = 0;
  41. G_fseek(fd, 0L, 0);
  42. while (G_getl(buf, sizeof(buf), fd))
  43. count++;
  44. array = (char **)G_calloc(count + 1, sizeof(char **));
  45. count = 0;
  46. G_fseek(fd, 0L, 0);
  47. while (G_getl(buf, sizeof(buf), fd)) {
  48. array[count] = G_store(buf);
  49. count++;
  50. }
  51. G__read_Cell_head_array(array, cellhd, is_cellhd);
  52. count = 0;
  53. while (array[count]) {
  54. G_free(array[count]);
  55. count++;
  56. }
  57. G_free(array);
  58. }
  59. /* Read window from NULL terminated array of strings */
  60. void G__read_Cell_head_array(char **array,
  61. struct Cell_head *cellhd, int is_cellhd)
  62. {
  63. char *buf;
  64. char label[200];
  65. char value[200];
  66. int i, line;
  67. int flags;
  68. G_debug(2, "G__read_Cell_head_array");
  69. flags = 0;
  70. /* initialize the cell header */
  71. cellhd->format = 0;
  72. cellhd->rows = 0;
  73. cellhd->rows3 = 0;
  74. cellhd->cols = 0;
  75. cellhd->cols3 = 0;
  76. cellhd->depths = 1;
  77. cellhd->proj = -1;
  78. cellhd->zone = -1;
  79. cellhd->compressed = -1;
  80. cellhd->ew_res = 0.0;
  81. cellhd->ew_res3 = 1.0;
  82. cellhd->ns_res = 0.0;
  83. cellhd->ns_res3 = 1.0;
  84. cellhd->tb_res = 1.0;
  85. cellhd->north = 0.0;
  86. cellhd->south = 0.0;
  87. cellhd->east = 0.0;
  88. cellhd->west = 0.0;
  89. cellhd->top = 1.0;
  90. cellhd->bottom = 0.0;
  91. /* determine projection, zone first */
  92. i = 0;
  93. for (line = 1; (buf = array[i++]); line++) {
  94. if (TEST(F_PROJ) && TEST(F_ZONE))
  95. break;
  96. switch (scan_item(buf, label, value)) {
  97. case -1:
  98. G_fatal_error(_("Syntax error"));
  99. case 0:
  100. continue;
  101. case 1:
  102. break;
  103. }
  104. if (strncmp(label, "proj", 4) == 0) {
  105. if (TEST(F_PROJ))
  106. G_fatal_error(_("duplicate projection field"));
  107. if (!scan_int(value, &cellhd->proj))
  108. G_fatal_error(_("Syntax error"));
  109. SET(F_PROJ);
  110. continue;
  111. }
  112. if (strncmp(label, "zone", 4) == 0) {
  113. if (TEST(F_ZONE))
  114. G_fatal_error(_("duplicate zone field"));
  115. if (!scan_int(value, &cellhd->zone))
  116. G_fatal_error(_("Syntax error"));
  117. SET(F_ZONE);
  118. continue;
  119. }
  120. }
  121. if (!TEST(F_PROJ))
  122. G_fatal_error(_("projection field missing"));
  123. if (!TEST(F_ZONE))
  124. G_fatal_error(_("zone field missing"));
  125. /* read the other info */
  126. i = 0;
  127. for (line = 1; (buf = array[i++]); line++) {
  128. G_debug(3, "region item: %s", buf);
  129. switch (scan_item(buf, label, value)) {
  130. case -1:
  131. G_fatal_error(_("Syntax error"));
  132. case 0:
  133. continue;
  134. case 1:
  135. break;
  136. }
  137. if (strncmp(label, "proj", 4) == 0)
  138. continue;
  139. if (strncmp(label, "zone", 4) == 0)
  140. continue;
  141. if (strncmp(label, "nort", 4) == 0) {
  142. if (TEST(F_NORTH))
  143. G_fatal_error(_("duplicate north field"));
  144. if (!G_scan_northing(value, &cellhd->north, cellhd->proj))
  145. G_fatal_error(_("Syntax error"));
  146. SET(F_NORTH);
  147. continue;
  148. }
  149. if (strncmp(label, "sout", 4) == 0) {
  150. if (TEST(F_SOUTH))
  151. G_fatal_error(_("duplicate south field"));
  152. if (!G_scan_northing(value, &cellhd->south, cellhd->proj))
  153. G_fatal_error(_("Syntax error"));
  154. SET(F_SOUTH);
  155. continue;
  156. }
  157. if (strncmp(label, "east", 4) == 0) {
  158. if (TEST(F_EAST))
  159. G_fatal_error(_("duplicate east field"));
  160. if (!G_scan_easting(value, &cellhd->east, cellhd->proj))
  161. G_fatal_error(_("Syntax error"));
  162. SET(F_EAST);
  163. continue;
  164. }
  165. if (strncmp(label, "west", 4) == 0) {
  166. if (TEST(F_WEST))
  167. G_fatal_error(_("duplicate west field"));
  168. if (!G_scan_easting(value, &cellhd->west, cellhd->proj))
  169. G_fatal_error(_("Syntax error"));
  170. SET(F_WEST);
  171. continue;
  172. }
  173. if (strncmp(label, "top", 3) == 0) {
  174. if (TEST(F_TOP))
  175. G_fatal_error(_("duplicate top field"));
  176. if (!scan_double(value, &cellhd->top))
  177. G_fatal_error(_("Syntax error"));
  178. SET(F_TOP);
  179. continue;
  180. }
  181. if (strncmp(label, "bottom", 6) == 0) {
  182. if (TEST(F_BOTTOM))
  183. G_fatal_error(_("duplicate bottom field"));
  184. if (!scan_double(value, &cellhd->bottom))
  185. G_fatal_error(_("Syntax error"));
  186. SET(F_BOTTOM);
  187. continue;
  188. }
  189. if (strncmp(label, "e-w ", 4) == 0 && strlen(label) == 9) {
  190. if (TEST(F_EWRES))
  191. G_fatal_error(_("duplicate e-w resolution field"));
  192. if (!G_scan_resolution(value, &cellhd->ew_res, cellhd->proj))
  193. G_fatal_error(_("Syntax error"));
  194. if (cellhd->ew_res <= 0.0)
  195. G_fatal_error(_("Syntax error"));
  196. SET(F_EWRES);
  197. continue;
  198. }
  199. if (strncmp(label, "e-w resol3", 10) == 0) {
  200. if (TEST(F_EWRES3))
  201. G_fatal_error(_("duplicate 3D e-w resolution field"));
  202. if (!G_scan_resolution(value, &cellhd->ew_res3, cellhd->proj))
  203. G_fatal_error(_("Syntax error"));
  204. if (cellhd->ew_res3 <= 0.0)
  205. G_fatal_error(_("Syntax error"));
  206. SET(F_EWRES3);
  207. continue;
  208. }
  209. if (strncmp(label, "n-s ", 4) == 0 && strlen(label) == 9) {
  210. if (TEST(F_NSRES))
  211. G_fatal_error(_("duplicate n-s resolution field"));
  212. if (!G_scan_resolution(value, &cellhd->ns_res, cellhd->proj))
  213. G_fatal_error(_("Syntax error"));
  214. if (cellhd->ns_res <= 0.0)
  215. G_fatal_error(_("Syntax error"));
  216. SET(F_NSRES);
  217. continue;
  218. }
  219. if (strncmp(label, "n-s resol3", 10) == 0) {
  220. if (TEST(F_NSRES3))
  221. G_fatal_error(_("duplicate 3D n-s resolution field"));
  222. if (!G_scan_resolution(value, &cellhd->ns_res3, cellhd->proj))
  223. G_fatal_error(_("Syntax error"));
  224. if (cellhd->ns_res3 <= 0.0)
  225. G_fatal_error(_("Syntax error"));
  226. SET(F_NSRES3);
  227. continue;
  228. }
  229. if (strncmp(label, "t-b ", 4) == 0) {
  230. if (TEST(F_TBRES))
  231. G_fatal_error(_("duplicate t-b resolution field"));
  232. if (!scan_double(value, &cellhd->tb_res))
  233. G_fatal_error(_("Syntax error"));
  234. if (cellhd->tb_res <= 0.0)
  235. G_fatal_error(_("Syntax error"));
  236. SET(F_TBRES);
  237. continue;
  238. }
  239. if (strncmp(label, "rows", 4) == 0 && strlen(label) == 4) {
  240. if (TEST(F_ROWS))
  241. G_fatal_error(_("duplicate rows field"));
  242. if (!scan_int(value, &cellhd->rows))
  243. G_fatal_error(_("Syntax error"));
  244. if (cellhd->rows <= 0)
  245. G_fatal_error(_("Syntax error"));
  246. SET(F_ROWS);
  247. continue;
  248. }
  249. if (strncmp(label, "rows3", 5) == 0) {
  250. if (TEST(F_ROWS3))
  251. G_fatal_error(_("duplicate 3D rows field"));
  252. if (!scan_int(value, &cellhd->rows3))
  253. G_fatal_error(_("Syntax error"));
  254. if (cellhd->rows3 <= 0)
  255. G_fatal_error(_("Syntax error"));
  256. SET(F_ROWS3);
  257. continue;
  258. }
  259. if (strncmp(label, "cols", 4) == 0 && strlen(label) == 4) {
  260. if (TEST(F_COLS))
  261. G_fatal_error(_("duplicate cols field"));
  262. if (!scan_int(value, &cellhd->cols))
  263. G_fatal_error(_("Syntax error"));
  264. if (cellhd->cols <= 0)
  265. G_fatal_error(_("Syntax error"));
  266. SET(F_COLS);
  267. continue;
  268. }
  269. if (strncmp(label, "cols3", 5) == 0) {
  270. if (TEST(F_COLS3))
  271. G_fatal_error(_("duplicate 3D cols field"));
  272. if (!scan_int(value, &cellhd->cols3))
  273. G_fatal_error(_("Syntax error"));
  274. if (cellhd->cols3 <= 0)
  275. G_fatal_error(_("Syntax error"));
  276. SET(F_COLS3);
  277. continue;
  278. }
  279. if (strncmp(label, "depths", 6) == 0) {
  280. if (TEST(F_DEPTHS))
  281. G_fatal_error(_("duplicate depths field"));
  282. if (!scan_int(value, &cellhd->depths))
  283. G_fatal_error(_("Syntax error"));
  284. if (cellhd->depths <= 0)
  285. G_fatal_error(_("Syntax error"));
  286. SET(F_DEPTHS);
  287. continue;
  288. }
  289. if (strncmp(label, "form", 4) == 0) {
  290. if (TEST(F_FORMAT))
  291. G_fatal_error(_("duplicate format field"));
  292. if (!scan_int(value, &cellhd->format))
  293. G_fatal_error(_("Syntax error"));
  294. SET(F_FORMAT);
  295. continue;
  296. }
  297. if (strncmp(label, "comp", 4) == 0) {
  298. if (TEST(F_COMP))
  299. G_fatal_error(_("duplicate compressed field"));
  300. if (!scan_int(value, &cellhd->compressed))
  301. G_fatal_error(_("Syntax error"));
  302. SET(F_COMP);
  303. continue;
  304. }
  305. G_fatal_error(_("Syntax error"));
  306. }
  307. /* check some of the fields */
  308. if (!TEST(F_NORTH))
  309. G_fatal_error(_("north field missing"));
  310. if (!TEST(F_SOUTH))
  311. G_fatal_error(_("south field missing"));
  312. if (!TEST(F_WEST))
  313. G_fatal_error(_("west field missing"));
  314. if (!TEST(F_EAST))
  315. G_fatal_error(_("east field missing"));
  316. if (!TEST(F_EWRES) && !TEST(F_COLS))
  317. G_fatal_error(_("cols field missing"));
  318. if (!TEST(F_NSRES) && !TEST(F_ROWS))
  319. G_fatal_error(_("rows field missing"));
  320. /* This next stmt is commented out to allow wr_cellhd.c to write
  321. * headers that will be readable by GRASS 3.1
  322. if ((TEST(F_ROWS) && TEST(F_NSRES))
  323. || (TEST(F_COLS) && TEST(F_EWRES)))
  324. ERROR ("row/col and resolution information can not both appear ",0);
  325. */
  326. /* 3D defined? */
  327. if (TEST(F_EWRES3) || TEST(F_NSRES3) || TEST(F_COLS3) || TEST(F_ROWS3)) {
  328. if (!TEST(F_EWRES3))
  329. G_fatal_error(_("ewres3 field missing"));
  330. if (!TEST(F_NSRES3))
  331. G_fatal_error(_("nsres3 field missing"));
  332. if (!TEST(F_COLS3))
  333. G_fatal_error(_("cols3 field missing"));
  334. if (!TEST(F_ROWS3))
  335. G_fatal_error(_("rows3 field missing"));
  336. }
  337. else { /* use 2D */
  338. cellhd->ew_res3 = cellhd->ew_res;
  339. cellhd->ns_res3 = cellhd->ns_res;
  340. cellhd->cols3 = cellhd->cols;
  341. cellhd->rows3 = cellhd->rows;
  342. }
  343. /* Adjust and complete the cell header */
  344. G_adjust_Cell_head(cellhd, TEST(F_ROWS), TEST(F_COLS));
  345. }
  346. static int scan_item(const char *buf, char *label, char *value)
  347. {
  348. /* skip blank lines */
  349. if (sscanf(buf, "%1s", label) != 1)
  350. return 0;
  351. /* skip comment lines */
  352. if (*label == '#')
  353. return 0;
  354. /* must be label: value */
  355. if (sscanf(buf, "%[^:]:%[^\n]", label, value) != 2)
  356. return -1;
  357. G_strip(label);
  358. G_strip(value);
  359. return 1;
  360. }
  361. static int scan_int(const char *buf, int *n)
  362. {
  363. char dummy[3];
  364. *dummy = 0;
  365. return (sscanf(buf, "%d%1s", n, dummy) == 1 && *dummy == 0);
  366. }
  367. static double scan_double(const char *buf, double *n)
  368. {
  369. char dummy[3];
  370. *dummy = 0;
  371. return (sscanf(buf, "%lf%1s", n, dummy) == 1 && *dummy == 0);
  372. }