open_files.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* PURPOSE: opening input rasters and creating segmentation files */
  2. #include <stdlib.h>
  3. #include <grass/gis.h>
  4. #include <grass/glocale.h>
  5. #include <grass/imagery.h>
  6. #include <grass/segment.h> /* segmentation library */
  7. #include "iseg.h"
  8. static int load_seeds(struct globals *, int, int, int);
  9. static int read_seed(struct globals *, SEGMENT *, struct rc *, int);
  10. static int manage_memory(int, int, struct globals *);
  11. int open_files(struct globals *globals)
  12. {
  13. int *in_fd, bounds_fd, is_null;
  14. int n, row, col, srows, scols, inlen, outlen, nseg;
  15. DCELL **inbuf; /* buffers to store lines from each of the imagery group rasters */
  16. CELL *boundsbuf, bounds_val;
  17. int have_bounds = 0;
  18. CELL id;
  19. struct Range range; /* min/max values of bounds map */
  20. struct FPRange *fp_range; /* min/max values of each input raster */
  21. DCELL *min, *max;
  22. struct ngbr_stats Ri, Rk;
  23. /*allocate memory for flags */
  24. globals->null_flag = flag_create(globals->nrows, globals->ncols);
  25. globals->candidate_flag = flag_create(globals->nrows, globals->ncols);
  26. flag_clear_all(globals->null_flag);
  27. flag_clear_all(globals->candidate_flag);
  28. in_fd = G_malloc(globals->Ref.nfiles * sizeof(int));
  29. inbuf = (DCELL **) G_malloc(globals->Ref.nfiles * sizeof(DCELL *));
  30. fp_range = G_malloc(globals->Ref.nfiles * sizeof(struct FPRange));
  31. min = G_malloc(globals->Ref.nfiles * sizeof(DCELL));
  32. max = G_malloc(globals->Ref.nfiles * sizeof(DCELL));
  33. globals->min = min;
  34. globals->max = max;
  35. G_debug(1, "Opening input rasters...");
  36. for (n = 0; n < globals->Ref.nfiles; n++) {
  37. inbuf[n] = Rast_allocate_d_buf();
  38. in_fd[n] = Rast_open_old(globals->Ref.file[n].name, globals->Ref.file[n].mapset);
  39. }
  40. /* Get min/max values of each input raster for scaling */
  41. globals->max_diff = 0.;
  42. globals->nbands = globals->Ref.nfiles;
  43. for (n = 0; n < globals->Ref.nfiles; n++) {
  44. /* returns -1 on error, 2 on empty range, quitting either way. */
  45. if (Rast_read_fp_range(globals->Ref.file[n].name, globals->Ref.file[n].mapset, &fp_range[n]) != 1)
  46. G_fatal_error(_("No min/max found in raster map <%s>"),
  47. globals->Ref.file[n].name);
  48. Rast_get_fp_range_min_max(&(fp_range[n]), &min[n], &max[n]);
  49. if (Rast_is_d_null_value(&min[n])) {
  50. G_fatal_error(_("Input map <%s> is all NULL"),
  51. globals->Ref.file[n].name);
  52. }
  53. if (min[n] == max[n]) {
  54. G_fatal_error(_("Input map <%s> is a constant of value %g"),
  55. globals->Ref.file[n].name, min[n]);
  56. }
  57. G_debug(1, "Range for layer %d: min = %f, max = %f",
  58. n, min[n], max[n]);
  59. }
  60. if (globals->weighted == FALSE)
  61. globals->max_diff = globals->Ref.nfiles;
  62. else {
  63. /* max difference with selected similarity method */
  64. Ri.mean = max;
  65. Rk.mean = min;
  66. globals->max_diff = 1;
  67. globals->max_diff = (*globals->calculate_similarity) (&Ri, &Rk, globals);
  68. }
  69. /* ********** find out file segmentation size ************ */
  70. G_debug(1, "Calculate temp file sizes...");
  71. /* size of each element to be stored */
  72. inlen = sizeof(DCELL) * globals->Ref.nfiles;
  73. outlen = sizeof(CELL);
  74. G_debug(1, "data element size, in: %d , out: %d ", inlen, outlen);
  75. globals->datasize = sizeof(double) * globals->nbands;
  76. /* count non-null cells */
  77. globals->notnullcells = (LARGEINT)globals->nrows * globals->ncols;
  78. for (row = 0; row < globals->nrows; row++) {
  79. for (n = 0; n < globals->Ref.nfiles; n++) {
  80. Rast_get_d_row(in_fd[n], inbuf[n], row);
  81. }
  82. for (col = 0; col < globals->ncols; col++) {
  83. is_null = 0; /*Assume there is data */
  84. for (n = 0; n < globals->Ref.nfiles; n++) {
  85. if (Rast_is_d_null_value(&inbuf[n][col])) {
  86. is_null = 1;
  87. }
  88. }
  89. if (is_null) {
  90. globals->notnullcells--;
  91. FLAG_SET(globals->null_flag, row, col);
  92. }
  93. }
  94. }
  95. if (globals->notnullcells < 2)
  96. G_fatal_error(_("Insufficient number of non-NULL cells in current region"));
  97. /* segment lib segment size */
  98. srows = 64;
  99. scols = 64;
  100. nseg = manage_memory(srows, scols, globals);
  101. /* create segment structures */
  102. if (Segment_open
  103. (&globals->bands_seg, G_tempfile(), globals->nrows, globals->ncols, srows,
  104. scols, inlen, nseg) != 1)
  105. G_fatal_error("Unable to create input temporary files");
  106. if (globals->method == ORM_MS) {
  107. if (Segment_open
  108. (&globals->bands_seg2, G_tempfile(), globals->nrows, globals->ncols, srows,
  109. scols, inlen, nseg) != 1)
  110. G_fatal_error("Unable to create input temporary files");
  111. globals->bands_in = &globals->bands_seg;
  112. globals->bands_out = &globals->bands_seg2;
  113. }
  114. if (Segment_open
  115. (&globals->rid_seg, G_tempfile(), globals->nrows, globals->ncols, srows,
  116. scols, outlen, nseg * 2) != 1)
  117. G_fatal_error("Unable to create input temporary files");
  118. /* load input bands to segment structure */
  119. if (globals->Ref.nfiles > 1)
  120. G_message(_("Loading input bands..."));
  121. else
  122. G_message(_("Loading input band..."));
  123. globals->bands_val = (double *)G_malloc(inlen);
  124. globals->second_val = (double *)G_malloc(inlen);
  125. globals->max_rid = 0;
  126. globals->row_min = globals->nrows;
  127. globals->row_max = 0;
  128. globals->col_min = globals->ncols;
  129. globals->col_max = 0;
  130. for (row = 0; row < globals->nrows; row++) {
  131. G_percent(row, globals->nrows, 4);
  132. for (n = 0; n < globals->Ref.nfiles; n++) {
  133. Rast_get_d_row(in_fd[n], inbuf[n], row);
  134. }
  135. for (col = 0; col < globals->ncols; col++) {
  136. is_null = 0; /*Assume there is data */
  137. for (n = 0; n < globals->Ref.nfiles; n++) {
  138. globals->bands_val[n] = inbuf[n][col];
  139. if (Rast_is_d_null_value(&inbuf[n][col])) {
  140. is_null = 1;
  141. }
  142. else {
  143. if (globals->weighted == FALSE)
  144. /* scaled version */
  145. globals->bands_val[n] = (inbuf[n][col] - min[n]) / (max[n] - min[n]);
  146. }
  147. }
  148. if (Segment_put(&globals->bands_seg,
  149. (void *)globals->bands_val, row, col) != 1)
  150. G_fatal_error(_("Unable to write to temporary file"));
  151. if (globals->method == ORM_MS) {
  152. if (Segment_put(&globals->bands_seg2,
  153. (void *)globals->bands_val, row, col) != 1)
  154. G_fatal_error(_("Unable to write to temporary file"));
  155. }
  156. id = 0;
  157. if (!is_null) {
  158. /* get min/max row/col to narrow the processing window */
  159. if (globals->row_min > row)
  160. globals->row_min = row;
  161. if (globals->row_max < row)
  162. globals->row_max = row;
  163. if (globals->col_min > col)
  164. globals->col_min = col;
  165. if (globals->col_max < col)
  166. globals->col_max = col;
  167. }
  168. else {
  169. /* all input bands NULL */
  170. Rast_set_c_null_value(&id, 1);
  171. FLAG_SET(globals->null_flag, row, col);
  172. }
  173. if (Segment_put(&globals->rid_seg,
  174. (void *)&id, row, col) != 1)
  175. G_fatal_error(_("Unable to write to temporary file"));
  176. }
  177. }
  178. G_percent(1, 1, 1);
  179. G_debug(1, "nrows: %d, min row: %d, max row %d",
  180. globals->nrows, globals->row_min, globals->row_max);
  181. G_debug(1, "ncols: %d, min col: %d, max col %d",
  182. globals->ncols, globals->col_min, globals->col_max);
  183. globals->row_max++;
  184. globals->col_max++;
  185. globals->ncells = (LARGEINT)(globals->row_max - globals->row_min) *
  186. (globals->col_max - globals->col_min);
  187. /* bounds/constraints */
  188. Rast_set_c_null_value(&globals->upper_bound, 1);
  189. Rast_set_c_null_value(&globals->lower_bound, 1);
  190. if (globals->bounds_map != NULL) {
  191. if (Segment_open
  192. (&globals->bounds_seg, G_tempfile(), globals->nrows, globals->ncols,
  193. srows, scols, sizeof(CELL), nseg) != TRUE)
  194. G_fatal_error("Unable to create bounds temporary files");
  195. if (Rast_read_range(globals->bounds_map, globals->bounds_mapset, &range) != 1)
  196. G_fatal_error(_("No min/max found in raster map <%s>"),
  197. globals->bounds_map);
  198. Rast_get_range_min_max(&range, &globals->upper_bound,
  199. &globals->lower_bound);
  200. if (Rast_is_c_null_value(&globals->upper_bound) ||
  201. Rast_is_c_null_value(&globals->lower_bound)) {
  202. G_fatal_error(_("No min/max found in raster map <%s>"),
  203. globals->bounds_map);
  204. }
  205. bounds_fd = Rast_open_old(globals->bounds_map, globals->bounds_mapset);
  206. boundsbuf = Rast_allocate_c_buf();
  207. for (row = 0; row < globals->nrows; row++) {
  208. Rast_get_c_row(bounds_fd, boundsbuf, row);
  209. for (col = 0; col < globals->ncols; col++) {
  210. bounds_val = boundsbuf[col];
  211. if (FLAG_GET(globals->null_flag, row, col)) {
  212. Rast_set_c_null_value(&bounds_val, 1);
  213. }
  214. else {
  215. if (!Rast_is_c_null_value(&bounds_val)) {
  216. have_bounds = 1;
  217. if (globals->lower_bound > bounds_val)
  218. globals->lower_bound = bounds_val;
  219. if (globals->upper_bound < bounds_val)
  220. globals->upper_bound = bounds_val;
  221. }
  222. }
  223. if (Segment_put(&globals->bounds_seg, &bounds_val, row, col) != 1)
  224. G_fatal_error(_("Unable to write to temporary file"));
  225. }
  226. }
  227. Rast_close(bounds_fd);
  228. G_free(boundsbuf);
  229. if (!have_bounds) {
  230. G_warning(_("There are no boundary constraints in '%s'"), globals->bounds_map);
  231. Rast_set_c_null_value(&globals->upper_bound, 1);
  232. Rast_set_c_null_value(&globals->lower_bound, 1);
  233. Segment_close(&globals->bounds_seg);
  234. globals->bounds_map = NULL;
  235. globals->bounds_mapset = NULL;
  236. }
  237. }
  238. else {
  239. G_debug(1, "no boundary constraint supplied.");
  240. }
  241. /* other info */
  242. globals->candidate_count = 0; /* counter for remaining candidate pixels */
  243. /* Free memory */
  244. for (n = 0; n < globals->Ref.nfiles; n++) {
  245. G_free(inbuf[n]);
  246. Rast_close(in_fd[n]);
  247. }
  248. globals->rs.sum = G_malloc(globals->datasize);
  249. globals->rs.mean = G_malloc(globals->datasize);
  250. globals->reg_tree = rgtree_create(globals->nbands, globals->datasize);
  251. if (globals->method == ORM_RG && globals->seeds) {
  252. load_seeds(globals, srows, scols, nseg);
  253. G_debug(1, "Number of initial regions: %d", globals->max_rid);
  254. }
  255. G_free(inbuf);
  256. G_free(in_fd);
  257. G_free(fp_range);
  258. return TRUE;
  259. }
  260. static int load_seeds(struct globals *globals, int srows, int scols, int nseg)
  261. {
  262. int row, col;
  263. SEGMENT seeds_seg;
  264. CELL *seeds_buf, seeds_val;
  265. int seeds_fd, have_seeds;
  266. CELL new_id, cellmax, noid;
  267. struct rc Ri;
  268. G_debug(1, "load_seeds()");
  269. cellmax = (1 << (sizeof(CELL) * 8 - 2)) - 1;
  270. cellmax += (1 << (sizeof(CELL) * 8 - 2));
  271. noid = 0;
  272. G_message(_("Loading seeds from raster map <%s>..."), globals->seeds);
  273. if (Segment_open
  274. (&seeds_seg, G_tempfile(), globals->nrows, globals->ncols,
  275. srows, scols, sizeof(CELL), nseg) != TRUE)
  276. G_fatal_error("Unable to create bounds temporary files");
  277. seeds_fd = Rast_open_old(globals->seeds, "");
  278. seeds_buf = Rast_allocate_c_buf();
  279. have_seeds = 0;
  280. /* load seeds map to segment structure */
  281. for (row = 0; row < globals->nrows; row++) {
  282. Rast_get_c_row(seeds_fd, seeds_buf, row);
  283. for (col = 0; col < globals->ncols; col++) {
  284. if (FLAG_GET(globals->null_flag, row, col)) {
  285. Rast_set_c_null_value(&seeds_val, 1);
  286. }
  287. else {
  288. seeds_val = seeds_buf[col];
  289. if (!Rast_is_c_null_value(&seeds_val))
  290. have_seeds = 1;
  291. }
  292. if (Segment_put(&seeds_seg, &seeds_val, row, col) != 1)
  293. G_fatal_error(_("Unable to write to temporary file"));
  294. }
  295. }
  296. if (!have_seeds) {
  297. G_warning(_("No seeds found in '%s'!"), globals->seeds);
  298. G_free(seeds_buf);
  299. Rast_close(seeds_fd);
  300. Segment_close(&seeds_seg);
  301. return 0;
  302. }
  303. new_id = 0;
  304. /* convert seeds to regions */
  305. G_debug(1, "convert seeds to regions");
  306. Rast_set_c_null_value(&seeds_val, 1);
  307. for (row = 0; row < globals->nrows; row++) {
  308. Rast_get_c_row(seeds_fd, seeds_buf, row);
  309. for (col = 0; col < globals->ncols; col++) {
  310. if (!(FLAG_GET(globals->null_flag, row, col)) &&
  311. !(FLAG_GET(globals->candidate_flag, row, col))) {
  312. if (!Rast_is_c_null_value(&(seeds_buf[col]))) {
  313. if (new_id == cellmax)
  314. G_fatal_error(_("Too many seeds: integer overflow"));
  315. new_id++;
  316. Ri.row = row;
  317. Ri.col = col;
  318. if (!read_seed(globals, &seeds_seg, &Ri, new_id)) {
  319. new_id--;
  320. Segment_put(&globals->rid_seg, (void *)&noid, Ri.row, Ri.col);
  321. }
  322. }
  323. }
  324. }
  325. }
  326. G_free(seeds_buf);
  327. Rast_close(seeds_fd);
  328. Segment_close(&seeds_seg);
  329. globals->max_rid = new_id;
  330. flag_clear_all(globals->candidate_flag);
  331. return 1;
  332. }
  333. static int read_seed(struct globals *globals, SEGMENT *seeds_seg, struct rc *Ri, int new_id)
  334. {
  335. int n, i, Ri_id, Rk_id;
  336. struct rc ngbr_rc, next;
  337. struct rclist rilist;
  338. int neighbors[8][2];
  339. G_debug(4, "read_seed()");
  340. /* get Ri's segment ID from input seeds */
  341. Segment_get(seeds_seg, &Ri_id, Ri->row, Ri->col);
  342. /* set new segment id */
  343. if (Segment_put(&globals->rid_seg, &new_id, Ri->row, Ri->col) != 1)
  344. G_fatal_error(_("Unable to write to temporary file"));
  345. /* set candidate flag */
  346. FLAG_SET(globals->candidate_flag, Ri->row, Ri->col);
  347. /* initialize region stats */
  348. globals->rs.count = 1;
  349. globals->rs.id = new_id;
  350. Segment_get(&globals->bands_seg, (void *)globals->bands_val,
  351. Ri->row, Ri->col);
  352. for (i = 0; i < globals->nbands; i++) {
  353. globals->rs.sum[i] = globals->bands_val[i];
  354. globals->rs.mean[i] = globals->bands_val[i];
  355. }
  356. /* go through seed, spreading outwards from head */
  357. rclist_init(&rilist);
  358. rclist_add(&rilist, Ri->row, Ri->col);
  359. while (rclist_drop(&rilist, &next)) {
  360. G_debug(5, "find_pixel_neighbors for row: %d , col %d",
  361. next.row, next.col);
  362. globals->find_neighbors(next.row, next.col, neighbors);
  363. for (n = 0; n < globals->nn; n++) {
  364. ngbr_rc.row = neighbors[n][0];
  365. ngbr_rc.col = neighbors[n][1];
  366. if (ngbr_rc.row < 0 || ngbr_rc.row >= globals->nrows ||
  367. ngbr_rc.col < 0 || ngbr_rc.col >= globals->ncols) {
  368. continue;
  369. }
  370. if (FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col)) {
  371. continue;
  372. }
  373. if (FLAG_GET(globals->candidate_flag, ngbr_rc.row, ngbr_rc.col)) {
  374. continue;
  375. }
  376. Segment_get(seeds_seg, (void *) &Rk_id, ngbr_rc.row, ngbr_rc.col);
  377. G_debug(5, "Rk ID = %d Ri ID = %d", Rk_id, Ri_id);
  378. if (Rk_id != Ri_id) {
  379. continue;
  380. }
  381. /* set segment id */
  382. if (Segment_put(&globals->rid_seg,
  383. &new_id, ngbr_rc.row, ngbr_rc.col) != 1)
  384. G_fatal_error(_("Unable to write to temporary file"));
  385. /* set candidate flag */
  386. FLAG_SET(globals->candidate_flag, ngbr_rc.row, ngbr_rc.col);
  387. /* add to list of cells to check */
  388. rclist_add(&rilist, ngbr_rc.row, ngbr_rc.col);
  389. /* update region stats */
  390. Segment_get(&globals->bands_seg, (void *)globals->bands_val,
  391. ngbr_rc.row, ngbr_rc.col);
  392. for (i = 0; i < globals->nbands; i++) {
  393. globals->rs.sum[i] += globals->bands_val[i];
  394. }
  395. globals->rs.count++;
  396. }
  397. }
  398. if (rgtree_find(globals->reg_tree, &(globals->rs)) != NULL) {
  399. G_fatal_error(_("Segment %d is already registered!"), new_id);
  400. }
  401. /* insert into region tree */
  402. if (globals->rs.count >= globals->min_reg_size) {
  403. for (i = 0; i < globals->nbands; i++)
  404. globals->rs.mean[i] = globals->rs.sum[i] / globals->rs.count;
  405. rgtree_insert(globals->reg_tree, &(globals->rs));
  406. }
  407. else {
  408. if (globals->rs.count > 1)
  409. update_band_vals(Ri->row, Ri->col, &(globals->rs), globals);
  410. else if (globals->rs.count == 1) {
  411. return 0;
  412. }
  413. }
  414. return 1;
  415. }
  416. static int manage_memory(int srows, int scols, struct globals *globals)
  417. {
  418. double reg_size_mb, segs_mb;
  419. LARGEINT reg_size_count;
  420. int nseg, nseg_total;
  421. segs_mb = globals->mb;
  422. if (globals->method == ORM_RG) {
  423. /* minimum region size to store in search tree */
  424. reg_size_mb = 2 * globals->datasize + /* mean, sum */
  425. 2 * sizeof(int) + /* id, count */
  426. sizeof(unsigned char) +
  427. 2 * sizeof(struct REG_NODE *);
  428. reg_size_mb /= (1024. * 1024.);
  429. /* put aside some memory for segment structures */
  430. segs_mb = globals->mb * 0.1;
  431. if (segs_mb > 10)
  432. segs_mb = 10;
  433. /* calculate number of region stats that can be kept in memory */
  434. reg_size_count = (globals->mb - segs_mb) / reg_size_mb;
  435. if (reg_size_count < 1)
  436. reg_size_count = 1;
  437. globals->min_reg_size = 3;
  438. if (reg_size_count < (double) globals->notnullcells / globals->min_reg_size) {
  439. globals->min_reg_size = (double) globals->notnullcells / reg_size_count;
  440. }
  441. else {
  442. reg_size_count = (double) globals->notnullcells / globals->min_reg_size;
  443. /* recalculate segs_mb */
  444. segs_mb = globals->mb - reg_size_count * reg_size_mb;
  445. if (segs_mb < 10)
  446. segs_mb = 10;
  447. }
  448. G_verbose_message(_("Regions with at least %d cells are stored in memory"),
  449. globals->min_reg_size);
  450. }
  451. /* calculate number of segments in memory */
  452. /* nseg: integer overflow possible with large segs_mb */
  453. if (globals->bounds_map != NULL) {
  454. /* input bands, segment ids, bounds map */
  455. if (globals->method == ORM_MS) {
  456. nseg = (1024. * 1024. * segs_mb) /
  457. (sizeof(DCELL) * 2 * globals->nbands * srows * scols +
  458. sizeof(CELL) * 4 * srows * scols);
  459. }
  460. else {
  461. nseg = (1024. * 1024. * segs_mb) /
  462. (sizeof(DCELL) * globals->nbands * srows * scols +
  463. sizeof(CELL) * 4 * srows * scols);
  464. }
  465. }
  466. else {
  467. /* input bands, segment ids */
  468. if (globals->method == ORM_MS) {
  469. nseg = (1024. * 1024. * segs_mb) /
  470. (sizeof(DCELL) * 2 * globals->nbands * srows * scols +
  471. sizeof(CELL) * 2 * srows * scols);
  472. }
  473. else {
  474. nseg = (1024. * 1024. * segs_mb) /
  475. (sizeof(DCELL) * globals->nbands * srows * scols +
  476. sizeof(CELL) * 2 * srows * scols);
  477. }
  478. }
  479. nseg_total = (globals->nrows / srows + (globals->nrows % srows > 0)) *
  480. (globals->ncols / scols + (globals->ncols % scols > 0));
  481. if (nseg > nseg_total)
  482. nseg = nseg_total;
  483. G_debug(1, "current region: %d rows, %d cols", globals->nrows, globals->ncols);
  484. G_debug(1, "segmented to tiles with size: %d rows, %d cols", srows,
  485. scols);
  486. G_verbose_message(_("Number of segments in memory: %d of %d total"),
  487. nseg, nseg_total);
  488. return nseg;
  489. }