grass2str.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #ifndef _gras2str_H
  19. #define _gras2str_H
  20. #include <grass/iostream/ami.h>
  21. #include <grass/glocale.h>
  22. #include "option.h"
  23. #include "types.h"
  24. #include "common.h"
  25. #include "nodata.h" /* for TERRAFLOW_INTERNAL_NODATA_VALUE */
  26. /* ---------------------------------------------------------------------- */
  27. /* create and return a STREAM containing the given cell; nodata_count
  28. is set to the number of cells that contain nodata */
  29. template<class T>
  30. AMI_STREAM<T>*
  31. cell2stream(char* cellname, elevation_type T_max_value, long* nodata_count) {
  32. Rtimer rt;
  33. AMI_err ae;
  34. elevation_type T_min_value = -T_max_value;
  35. rt_start(rt);
  36. assert(cellname && nodata_count);
  37. *nodata_count = 0;
  38. /* create output stream */
  39. AMI_STREAM<T>* str = new AMI_STREAM<T>();
  40. {
  41. char * foo;
  42. str->name(&foo);
  43. if (stats)
  44. *stats << "Reading raster map <" << cellname
  45. << "> to stream <" << foo << ">." << endl;
  46. G_verbose_message(_("Reading data from <%s> to stream <%s>"), cellname, foo);
  47. }
  48. /* open map */
  49. int infd;
  50. infd = Rast_open_old (cellname, "");
  51. /* determine map type (CELL/FCELL/DCELL) */
  52. RASTER_MAP_TYPE data_type;
  53. data_type = Rast_map_type(cellname, "");
  54. /* Allocate input buffer */
  55. void *inrast;
  56. inrast = Rast_allocate_buf(data_type);
  57. CELL c;
  58. FCELL f;
  59. DCELL d;
  60. T x;
  61. int isnull = 0;
  62. G_important_message(_("Reading input data..."));
  63. for (int i = 0; i< nrows; i++) {
  64. /* read input map */
  65. Rast_get_row (infd, inrast, i, data_type);
  66. for (int j=0; j<ncols; j++) {
  67. switch (data_type) {
  68. case CELL_TYPE:
  69. c = ((CELL *) inrast)[j];
  70. isnull = Rast_is_c_null_value(&c);
  71. if (!isnull) {
  72. x = (T)c; d = (DCELL)c;
  73. }
  74. break;
  75. case FCELL_TYPE:
  76. f = ((FCELL *) inrast)[j];
  77. isnull = Rast_is_f_null_value(&f);
  78. if (!isnull) {
  79. x = (T)f; d = (DCELL)f;
  80. }
  81. break;
  82. case DCELL_TYPE:
  83. d = ((DCELL *) inrast)[j];
  84. isnull = Rast_is_d_null_value(&d);
  85. if (!isnull) {
  86. x = (T)d;
  87. }
  88. break;
  89. default:
  90. G_fatal_error("Raster type not implemented");
  91. }
  92. /* cout << form("(i=%d,j=%d): (%d, %f)\n",i,j,x,d); cout.flush(); */
  93. /* handle null values */
  94. if (isnull) {
  95. x = TERRAFLOW_INTERNAL_NODATA_VALUE;
  96. (*nodata_count)++;
  97. } else {
  98. /* check range */
  99. if ((d > (DCELL)T_max_value) || (d < (DCELL)T_min_value)) {
  100. G_fatal_error("Value out of range, reading raster map <%s> "
  101. "at (i=%d, j=%d) value=%.1f",
  102. cellname, i, j, d);
  103. }
  104. }
  105. /* write x to stream */
  106. ae = str->write_item(x);
  107. assert(ae == AMI_ERROR_NO_ERROR);
  108. } /* for j */
  109. G_percent(i, nrows, 2);
  110. }/* for i */
  111. G_percent(1, 1, 1); /* finish it */
  112. /* delete buffers */
  113. G_free(inrast);
  114. /* close map files */
  115. Rast_close (infd);
  116. G_debug(3, "nrows=%d ncols=%d stream_len()=%" PRI_OFF_T, nrows, ncols,
  117. str->stream_len());
  118. assert((off_t) nrows * ncols == str->stream_len());
  119. rt_stop(rt);
  120. if (stats)
  121. stats->recordTime("reading raster map", rt);
  122. return str;
  123. }
  124. /* ---------------------------------------------------------------------- */
  125. template<class T>
  126. void
  127. stream2_CELL(AMI_STREAM<T>* str, dimension_type nrows, dimension_type ncols,
  128. char* cellname, bool usefcell=false) {
  129. Rtimer rt;
  130. AMI_err ae;
  131. RASTER_MAP_TYPE mtype= (usefcell ? FCELL_TYPE : CELL_TYPE);
  132. rt_start(rt);
  133. assert(str);
  134. assert(str->stream_len() == nrows*ncols);
  135. str->seek(0);
  136. {
  137. char * foo;
  138. str->name(&foo);
  139. if (stats)
  140. *stats << "Writing stream <" << foo << "> to raster map <" << cellname << ">.\n";
  141. }
  142. /* open output raster map */
  143. int outfd;
  144. outfd = Rast_open_new (cellname, mtype);
  145. /* Allocate output buffer */
  146. unsigned char *outrast;
  147. outrast = (unsigned char *)Rast_allocate_buf(mtype);
  148. assert(outrast);
  149. T* elt;
  150. G_important_message(_("Writing to raster map <%s>..."), cellname);
  151. for (int i=0; i< nrows; i++) {
  152. for (int j=0; j< ncols; j++) {
  153. /* READ VALUE */
  154. ae = str->read_item(&elt);
  155. if (ae != AMI_ERROR_NO_ERROR) {
  156. str->sprint();
  157. G_fatal_error(_("stream2cell: Reading stream failed at (%d,%d)"),
  158. i, j);
  159. }
  160. /* WRITE VALUE */
  161. if(usefcell){
  162. if (is_nodata(*elt)) {
  163. Rast_set_f_null_value( &( ((FCELL *) outrast)[j]), 1);
  164. } else {
  165. ((FCELL *) outrast)[j] = (FCELL)(*elt);
  166. }
  167. }else{
  168. if (is_nodata(*elt)) {
  169. Rast_set_c_null_value( &( ((CELL *) outrast)[j]), 1);
  170. } else {
  171. ((CELL *) outrast)[j] = (CELL)(*elt);
  172. }
  173. }
  174. } /* for j*/
  175. Rast_put_row (outfd, outrast, mtype);
  176. G_percent(i, nrows, 2);
  177. }/* for i */
  178. G_percent(1, 1, 2); /* finish it */
  179. G_free(outrast);
  180. Rast_close (outfd);
  181. rt_stop(rt);
  182. if (stats)
  183. stats->recordTime("writing raster map", rt);
  184. str->seek(0);
  185. return;
  186. }
  187. /* laura: this is identical with stream2_FCELL; did not know how to
  188. template ona type --- is that possible? */
  189. /* ---------------------------------------------------------------------- */
  190. template<class T, class FUN>
  191. void
  192. stream2_CELL(AMI_STREAM<T> *str, dimension_type nrows, dimension_type ncols,
  193. FUN fmt, char* cellname) {
  194. Rtimer rt;
  195. AMI_err ae;
  196. assert(str && cellname);
  197. /* assert(str->stream_len() == nrows*ncols); */
  198. rt_start(rt);
  199. str->seek(0);
  200. {
  201. char * foo;
  202. str->name(&foo);
  203. if (stats)
  204. *stats << "Writing stream <" << foo << "> to raster map <" << cellname << ">." << endl;
  205. }
  206. /* open output raster map */
  207. int outfd;
  208. outfd = Rast_open_new (cellname, CELL_TYPE);
  209. /* Allocate output buffer */
  210. unsigned char *outrast;
  211. outrast = (unsigned char *)Rast_allocate_buf(CELL_TYPE);
  212. assert(outrast);
  213. T* elt;
  214. ae = str->read_item(&elt);
  215. assert(ae == AMI_ERROR_NO_ERROR || ae == AMI_ERROR_END_OF_STREAM);
  216. G_important_message(_("Writing to raster map <%s>..."), cellname);
  217. for (int i=0; i< nrows; i++) {
  218. for (int j=0; j< ncols; j++) {
  219. if(ae == AMI_ERROR_NO_ERROR && elt->i == i && elt->j == j) {
  220. /* WRITE VALUE */
  221. if (is_nodata ( fmt(*elt) )) {
  222. Rast_set_c_null_value( &( ((CELL *) outrast)[j]), 1);
  223. } else {
  224. ((CELL *) outrast)[j] = (CELL)(fmt(*elt));
  225. }
  226. ae = str->read_item(&elt);
  227. assert(ae == AMI_ERROR_NO_ERROR || ae == AMI_ERROR_END_OF_STREAM);
  228. } else {
  229. /* WRITE NODATA */
  230. Rast_set_c_null_value( &( ((CELL *) outrast)[j]), 1);
  231. }
  232. } /* for j*/
  233. Rast_put_row (outfd, outrast, CELL_TYPE);
  234. G_percent(i, nrows, 2);
  235. }/* for i */
  236. G_percent(1, 1, 1); /* finish it */
  237. G_free(outrast);
  238. Rast_close (outfd);
  239. rt_stop(rt);
  240. if (stats)
  241. stats->recordTime("writing raster map", rt);
  242. str->seek(0);
  243. return;
  244. }
  245. /* ---------------------------------------------------------------------- */
  246. template<class T, class FUN>
  247. void
  248. stream2_FCELL(AMI_STREAM<T> *str, dimension_type nrows, dimension_type ncols,
  249. FUN fmt, char* cellname) {
  250. Rtimer rt;
  251. AMI_err ae;
  252. assert(str && cellname);
  253. /* assert(str->stream_len() == nrows*ncols); */
  254. rt_start(rt);
  255. str->seek(0);
  256. {
  257. char * foo;
  258. str->name(&foo);
  259. if (stats)
  260. *stats << "Writing stream <" << foo << "> to raster map <" << cellname << ">." << endl;
  261. }
  262. /* open output raster map */
  263. int outfd;
  264. outfd = Rast_open_new(cellname, FCELL_TYPE);
  265. /* Allocate output buffer */
  266. unsigned char *outrast;
  267. outrast = (unsigned char *)Rast_allocate_buf(FCELL_TYPE);
  268. assert(outrast);
  269. T* elt;
  270. ae = str->read_item(&elt);
  271. assert(ae == AMI_ERROR_NO_ERROR || ae == AMI_ERROR_END_OF_STREAM);
  272. G_important_message(_("Writing to raster map <%s>..."), cellname);
  273. for (int i=0; i< nrows; i++) {
  274. for (int j=0; j< ncols; j++) {
  275. if(ae == AMI_ERROR_NO_ERROR && elt->i == i && elt->j == j) {
  276. /* WRITE VALUE */
  277. if (is_nodata ( fmt(*elt) )) {
  278. Rast_set_f_null_value( &( ((FCELL *) outrast)[j]), 1);
  279. } else {
  280. ((FCELL *) outrast)[j] = (FCELL)(fmt(*elt));
  281. }
  282. ae = str->read_item(&elt);
  283. assert(ae == AMI_ERROR_NO_ERROR || ae == AMI_ERROR_END_OF_STREAM);
  284. } else {
  285. /* WRITE NODATA */
  286. Rast_set_f_null_value( &( ((FCELL *) outrast)[j]), 1);
  287. }
  288. } /* for j*/
  289. Rast_put_row (outfd, outrast, FCELL_TYPE);
  290. G_percent(i, nrows, 2);
  291. }/* for i */
  292. G_percent(1, 1, 1); /* finish it */
  293. G_free(outrast);
  294. Rast_close (outfd);
  295. rt_stop(rt);
  296. if (stats)
  297. stats->recordTime("writing raster map", rt);
  298. str->seek(0);
  299. return;
  300. }
  301. /* ---------------------------------------------------------------------- */
  302. /* outstr is sorted by (i,j);
  303. class sweepOutput {
  304. public:
  305. dimension_type i,j;
  306. flowaccumulation_type accu;
  307. #ifdef OUTPUT_TCI
  308. tci_type tci;
  309. #endif
  310. };
  311. create an accu raster map, and a tci raster map if OUTPUT_TCI is defined
  312. */
  313. template<class T, class FUN1, class FUN2>
  314. void
  315. stream2_FCELL(AMI_STREAM<T>* str, dimension_type nrows, dimension_type ncols,
  316. FUN1 fmt1, FUN2 fmt2,
  317. char* cellname1, char* cellname2) {
  318. Rtimer rt;
  319. AMI_err ae;
  320. assert(str);
  321. #ifndef OUTPUT_TCI
  322. /* this function should be used only if tci is wanted as output */
  323. G_warning("Use this function only if tci is wanted as output");
  324. exit(1);
  325. #else
  326. rt_start(rt);
  327. str->seek(0);
  328. {
  329. char * foo;
  330. str->name(&foo);
  331. if (stats) {
  332. *stats << "Writing stream <" << foo << "> to raster maps <"
  333. << cellname1 << "> and <" << cellname2 << ">." << endl;
  334. }
  335. }
  336. /* open raster maps */
  337. int fd1;
  338. if ( (fd1 = Rast_open_new (cellname1, FCELL_TYPE)) < 0) {
  339. G_fatal_error(_("Could not open <%s>"), cellname1);
  340. }
  341. int fd2;
  342. fd2 = Rast_open_new (cellname2, FCELL_TYPE);
  343. /* Allocate output buffers */
  344. FCELL *rast1;
  345. rast1 = (FCELL*)Rast_allocate_buf(FCELL_TYPE);
  346. assert(rast1);
  347. FCELL *rast2;
  348. rast2 = (FCELL*)Rast_allocate_buf(FCELL_TYPE);
  349. assert(rast2);
  350. T* elt;
  351. ae = str->read_item(&elt);
  352. assert(ae == AMI_ERROR_NO_ERROR || ae == AMI_ERROR_END_OF_STREAM);
  353. G_important_message(_("Writing to raster maps <%s,%s>..."),
  354. cellname1, cellname2);
  355. for (int i=0; i< nrows; i++) {
  356. for (int j=0; j< ncols; j++) {
  357. if(ae == AMI_ERROR_NO_ERROR && elt->i == i && elt->j == j) {
  358. /* WRITE VALUE */
  359. if (is_nodata(fmt1(*elt))) {
  360. Rast_set_f_null_value(&(rast1[j]), 1);
  361. } else {
  362. rast1[j] = fmt1(*elt);
  363. };
  364. if (is_nodata( fmt2(*elt))) {
  365. Rast_set_f_null_value(&(rast2[j]), 1);
  366. } else {
  367. rast2[j] = fmt2(*elt);
  368. }
  369. /* read next value */
  370. ae = str->read_item(&elt);
  371. assert(ae == AMI_ERROR_NO_ERROR || ae == AMI_ERROR_END_OF_STREAM);
  372. } else {
  373. /* WRITE NODATA */
  374. Rast_set_f_null_value(&(rast1[j]), 1);
  375. Rast_set_f_null_value(&(rast2[j]), 1);
  376. }
  377. } /* for j*/
  378. Rast_put_row (fd1, rast1, FCELL_TYPE);
  379. Rast_put_row (fd2, rast2, FCELL_TYPE);
  380. G_percent(i, nrows, 2);
  381. }/* for i */
  382. G_percent(1, 1, 1); /* finish it */
  383. G_free(rast1);
  384. Rast_close (fd1);
  385. G_free(rast2);
  386. Rast_close (fd2);
  387. rt_stop(rt);
  388. if (stats)
  389. stats->recordTime("writing stream to raster maps", rt);
  390. str->seek(0);
  391. return;
  392. #endif
  393. }
  394. #endif