rd_cellhd.c 12 KB

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