main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /****************************************************************************
  2. *
  3. * MODULE: r.li.shape
  4. * AUTHOR(S): Claudio Porta and Lucio Davide Spano (original contributors)
  5. * students of Computer Science University of Pisa (Italy)
  6. * Commission from Faunalia Pontedera (PI) www.faunalia.it
  7. * Fixes: Markus Neteler <neteler itc.it>
  8. * Rewrite: Markus Metz
  9. *
  10. * PURPOSE: calculates patch number index
  11. * COPYRIGHT: (C) 2007-2014 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20. #include <math.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/glocale.h>
  24. #include "../r.li.daemon/daemon.h"
  25. #include "../r.li.daemon/GenericCell.h"
  26. /* type, cell count and edge count of each patch */
  27. struct pst {
  28. generic_cell type;
  29. long cells;
  30. long edges;
  31. };
  32. rli_func shape_index;
  33. int calculate(int fd, struct area_entry *ad, double *result);
  34. int calculateD(int fd, struct area_entry *ad, double *result);
  35. int calculateF(int fd, struct area_entry *ad, double *result);
  36. int main(int argc, char *argv[])
  37. {
  38. struct Option *raster, *conf, *output;
  39. struct GModule *module;
  40. G_gisinit(argv[0]);
  41. module = G_define_module();
  42. module->description = _("Calculates shape index on a raster map");
  43. G_add_keyword(_("raster"));
  44. G_add_keyword(_("landscape structure analysis"));
  45. G_add_keyword(_("patch index"));
  46. /* define options */
  47. raster = G_define_standard_option(G_OPT_R_INPUT);
  48. conf = G_define_standard_option(G_OPT_F_INPUT);
  49. conf->key = "config";
  50. conf->description = _("Configuration file");
  51. conf->required = YES;
  52. output = G_define_standard_option(G_OPT_R_OUTPUT);
  53. /** add other options for index parameters here */
  54. if (G_parser(argc, argv))
  55. exit(EXIT_FAILURE);
  56. return calculateIndex(conf->answer, shape_index, NULL, raster->answer,
  57. output->answer);
  58. }
  59. int shape_index(int fd, char **par, struct area_entry *ad, double *result)
  60. {
  61. int ris = RLI_OK;
  62. double indice = 0;
  63. switch (ad->data_type) {
  64. case CELL_TYPE:
  65. {
  66. ris = calculate(fd, ad, &indice);
  67. break;
  68. }
  69. case DCELL_TYPE:
  70. {
  71. ris = calculateD(fd, ad, &indice);
  72. break;
  73. }
  74. case FCELL_TYPE:
  75. {
  76. ris = calculateF(fd, ad, &indice);
  77. break;
  78. }
  79. default:
  80. {
  81. G_fatal_error("data type unknown");
  82. return RLI_ERRORE;
  83. }
  84. }
  85. if (ris != RLI_OK) {
  86. return RLI_ERRORE;
  87. }
  88. *result = indice;
  89. return RLI_OK;
  90. }
  91. int calculate(int fd, struct area_entry *ad, double *result)
  92. {
  93. CELL *buf, *buf_sup, *buf_null;
  94. CELL corrCell, precCell, supCell;
  95. long npatch;
  96. long pid, old_pid, new_pid, *pid_corr, *pid_sup, *ltmp;
  97. struct pst *pst;
  98. long nalloc, incr;
  99. int i, j, k;
  100. int connected;
  101. int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked;
  102. buf_null = Rast_allocate_c_buf();
  103. Rast_set_c_null_value(buf_null, Rast_window_cols());
  104. buf_sup = buf_null;
  105. /* initialize patch ids */
  106. pid_corr = G_malloc(ad->cl * sizeof(long));
  107. pid_sup = G_malloc(ad->cl * sizeof(long));
  108. for (j = 0; j < ad->cl; j++) {
  109. pid_corr[j] = 0;
  110. pid_sup[j] = 0;
  111. }
  112. /* open mask if needed */
  113. mask_fd = -1;
  114. mask_buf = mask_sup = NULL;
  115. masked = FALSE;
  116. if (ad->mask == 1) {
  117. if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
  118. return RLI_ERRORE;
  119. mask_buf = G_malloc(ad->cl * sizeof(int));
  120. if (mask_buf == NULL) {
  121. G_fatal_error("malloc mask_buf failed");
  122. return RLI_ERRORE;
  123. }
  124. mask_sup = G_malloc(ad->cl * sizeof(int));
  125. if (mask_sup == NULL) {
  126. G_fatal_error("malloc mask_buf failed");
  127. return RLI_ERRORE;
  128. }
  129. for (j = 0; j < ad->cl; j++)
  130. mask_buf[j] = 0;
  131. masked = TRUE;
  132. }
  133. /* calculate number of patches */
  134. npatch = 0;
  135. pid = 0;
  136. /* patch size and type */
  137. incr = 1024;
  138. if (incr > ad->rl)
  139. incr = ad->rl;
  140. if (incr > ad->cl)
  141. incr = ad->cl;
  142. if (incr < 2)
  143. incr = 2;
  144. nalloc = incr;
  145. pst = G_malloc(nalloc * sizeof(struct pst));
  146. for (k = 0; k < nalloc; k++) {
  147. pst[k].cells = 0;
  148. pst[k].edges = 0;
  149. }
  150. for (i = 0; i < ad->rl; i++) {
  151. buf = RLI_get_cell_raster_row(fd, i + ad->y, ad);
  152. if (i > 0) {
  153. buf_sup = RLI_get_cell_raster_row(fd, i - 1 + ad->y, ad);
  154. }
  155. if (masked) {
  156. mask_tmp = mask_sup;
  157. mask_sup = mask_buf;
  158. mask_buf = mask_tmp;
  159. if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
  160. return 0;
  161. }
  162. ltmp = pid_sup;
  163. pid_sup = pid_corr;
  164. pid_corr = ltmp;
  165. Rast_set_c_null_value(&precCell, 1);
  166. connected = 0;
  167. for (j = 0; j < ad->cl; j++) {
  168. pid_corr[j] = 0;
  169. corrCell = buf[j + ad->x];
  170. if (masked && (mask_buf[j] == 0)) {
  171. Rast_set_c_null_value(&corrCell, 1);
  172. }
  173. supCell = buf_sup[j + ad->x];
  174. if (masked && (mask_sup[j] == 0)) {
  175. Rast_set_c_null_value(&supCell, 1);
  176. }
  177. if (Rast_is_c_null_value(&corrCell)) {
  178. if (!Rast_is_c_null_value(&precCell))
  179. pst[pid_corr[j - 1]].edges++;
  180. if (!Rast_is_c_null_value(&supCell))
  181. pst[pid_sup[j]].edges++;
  182. connected = 0;
  183. precCell = corrCell;
  184. continue;
  185. }
  186. if (!Rast_is_c_null_value(&precCell) && corrCell == precCell) {
  187. pid_corr[j] = pid_corr[j - 1];
  188. connected = 1;
  189. pst[pid_corr[j]].cells++;
  190. }
  191. else {
  192. connected = 0;
  193. }
  194. if (!Rast_is_c_null_value(&supCell) && corrCell == supCell) {
  195. if (pid_corr[j] != pid_sup[j]) {
  196. /* connect or merge */
  197. /* after r.clump */
  198. if (connected) {
  199. npatch--;
  200. if (npatch == 0) {
  201. G_fatal_error("npatch == 0 at row %d, col %d", i, j);
  202. }
  203. }
  204. old_pid = pid_corr[j];
  205. new_pid = pid_sup[j];
  206. pid_corr[j] = new_pid;
  207. if (old_pid > 0) {
  208. /* merge */
  209. /* update left side of the current row */
  210. for (k = 0; k < j; k++) {
  211. if (pid_corr[k] == old_pid)
  212. pid_corr[k] = new_pid;
  213. }
  214. /* update right side of the previous row */
  215. for (k = j + 1; k < ad->cl; k++) {
  216. if (pid_sup[k] == old_pid)
  217. pid_sup[k] = new_pid;
  218. }
  219. pst[new_pid].cells += pst[old_pid].cells;
  220. pst[old_pid].cells = 0;
  221. pst[new_pid].edges += pst[old_pid].edges;
  222. pst[old_pid].edges = 0;
  223. if (old_pid == pid)
  224. pid--;
  225. }
  226. else {
  227. pst[new_pid].cells++;
  228. }
  229. }
  230. connected = 1;
  231. }
  232. if (!connected) {
  233. /* start new patch */
  234. npatch++;
  235. pid++;
  236. pid_corr[j] = pid;
  237. if (pid >= nalloc) {
  238. pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
  239. for (k = nalloc; k < pid + incr; k++) {
  240. pst[k].cells = 0;
  241. pst[k].edges = 0;
  242. }
  243. nalloc = pid + incr;
  244. }
  245. pst[pid].cells = 1;
  246. pst[pid].type.t = CELL_TYPE;
  247. pst[pid].type.val.c = corrCell;
  248. }
  249. /* update edge count for corr */
  250. if (Rast_is_c_null_value(&precCell) || precCell != corrCell)
  251. pst[pid_corr[j]].edges++;
  252. if (Rast_is_c_null_value(&supCell) || supCell != corrCell)
  253. pst[pid_corr[j]].edges++;
  254. if (i == ad->rl - 1)
  255. pst[pid_corr[j]].edges++;
  256. if (j == ad->cl - 1)
  257. pst[pid_corr[j]].edges++;
  258. /* update edge count for prec */
  259. if (!Rast_is_c_null_value(&precCell) && precCell != corrCell)
  260. pst[pid_corr[j - 1]].edges++;
  261. /* update edge count for sup */
  262. if (!Rast_is_c_null_value(&supCell) && supCell != corrCell)
  263. pst[pid_sup[j]].edges++;
  264. precCell = corrCell;
  265. }
  266. }
  267. if (npatch > 0) {
  268. double edges, cells;
  269. edges = cells = 0;
  270. for (i = 1; i <= pid; i++) {
  271. cells += pst[i].cells;
  272. edges += pst[i].edges;
  273. }
  274. *result = 0.25 * edges / sqrt(cells);
  275. }
  276. else {
  277. Rast_set_d_null_value(result, 1);
  278. }
  279. if (masked) {
  280. close(mask_fd);
  281. G_free(mask_buf);
  282. G_free(mask_sup);
  283. }
  284. G_free(buf_null);
  285. G_free(pid_corr);
  286. G_free(pid_sup);
  287. G_free(pst);
  288. return RLI_OK;
  289. }
  290. int calculateD(int fd, struct area_entry *ad, double *result)
  291. {
  292. DCELL *buf, *buf_sup, *buf_null;
  293. DCELL corrCell, precCell, supCell;
  294. long npatch;
  295. long pid, old_pid, new_pid, *pid_corr, *pid_sup, *ltmp;
  296. struct pst *pst;
  297. long nalloc, incr;
  298. int i, j, k;
  299. int connected;
  300. int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked;
  301. buf_null = Rast_allocate_d_buf();
  302. Rast_set_d_null_value(buf_null, Rast_window_cols());
  303. buf_sup = buf_null;
  304. /* initialize patch ids */
  305. pid_corr = G_malloc(ad->cl * sizeof(long));
  306. pid_sup = G_malloc(ad->cl * sizeof(long));
  307. for (j = 0; j < ad->cl; j++) {
  308. pid_corr[j] = 0;
  309. pid_sup[j] = 0;
  310. }
  311. /* open mask if needed */
  312. mask_fd = -1;
  313. mask_buf = mask_sup = NULL;
  314. masked = FALSE;
  315. if (ad->mask == 1) {
  316. if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
  317. return RLI_ERRORE;
  318. mask_buf = G_malloc(ad->cl * sizeof(int));
  319. if (mask_buf == NULL) {
  320. G_fatal_error("malloc mask_buf failed");
  321. return RLI_ERRORE;
  322. }
  323. mask_sup = G_malloc(ad->cl * sizeof(int));
  324. if (mask_sup == NULL) {
  325. G_fatal_error("malloc mask_buf failed");
  326. return RLI_ERRORE;
  327. }
  328. for (j = 0; j < ad->cl; j++)
  329. mask_buf[j] = 0;
  330. masked = TRUE;
  331. }
  332. /* calculate number of patches */
  333. npatch = 0;
  334. pid = 0;
  335. /* patch size and type */
  336. incr = 1024;
  337. if (incr > ad->rl)
  338. incr = ad->rl;
  339. if (incr > ad->cl)
  340. incr = ad->cl;
  341. if (incr < 2)
  342. incr = 2;
  343. nalloc = incr;
  344. pst = G_malloc(nalloc * sizeof(struct pst));
  345. for (k = 0; k < nalloc; k++) {
  346. pst[k].cells = 0;
  347. pst[k].edges = 0;
  348. }
  349. for (i = 0; i < ad->rl; i++) {
  350. buf = RLI_get_dcell_raster_row(fd, i + ad->y, ad);
  351. if (i > 0) {
  352. buf_sup = RLI_get_dcell_raster_row(fd, i - 1 + ad->y, ad);
  353. }
  354. if (masked) {
  355. mask_tmp = mask_sup;
  356. mask_sup = mask_buf;
  357. mask_buf = mask_tmp;
  358. if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
  359. return 0;
  360. }
  361. ltmp = pid_sup;
  362. pid_sup = pid_corr;
  363. pid_corr = ltmp;
  364. Rast_set_d_null_value(&precCell, 1);
  365. connected = 0;
  366. for (j = 0; j < ad->cl; j++) {
  367. pid_corr[j] = 0;
  368. corrCell = buf[j + ad->x];
  369. if (masked && (mask_buf[j] == 0)) {
  370. Rast_set_d_null_value(&corrCell, 1);
  371. }
  372. supCell = buf_sup[j + ad->x];
  373. if (masked && (mask_sup[j] == 0)) {
  374. Rast_set_d_null_value(&supCell, 1);
  375. }
  376. if (Rast_is_d_null_value(&corrCell)) {
  377. if (!Rast_is_d_null_value(&precCell))
  378. pst[pid_corr[j - 1]].edges++;
  379. if (!Rast_is_d_null_value(&supCell))
  380. pst[pid_sup[j]].edges++;
  381. connected = 0;
  382. precCell = corrCell;
  383. continue;
  384. }
  385. if (!Rast_is_d_null_value(&precCell) && corrCell == precCell) {
  386. pid_corr[j] = pid_corr[j - 1];
  387. connected = 1;
  388. pst[pid_corr[j]].cells++;
  389. }
  390. else {
  391. connected = 0;
  392. }
  393. if (!Rast_is_d_null_value(&supCell) && corrCell == supCell) {
  394. if (pid_corr[j] != pid_sup[j]) {
  395. /* connect or merge */
  396. /* after r.clump */
  397. if (connected) {
  398. npatch--;
  399. if (npatch == 0) {
  400. G_fatal_error("npatch == 0 at row %d, col %d", i, j);
  401. }
  402. }
  403. old_pid = pid_corr[j];
  404. new_pid = pid_sup[j];
  405. pid_corr[j] = new_pid;
  406. if (old_pid > 0) {
  407. /* merge */
  408. /* update left side of the current row */
  409. for (k = 0; k < j; k++) {
  410. if (pid_corr[k] == old_pid)
  411. pid_corr[k] = new_pid;
  412. }
  413. /* update right side of the previous row */
  414. for (k = j + 1; k < ad->cl; k++) {
  415. if (pid_sup[k] == old_pid)
  416. pid_sup[k] = new_pid;
  417. }
  418. pst[new_pid].cells += pst[old_pid].cells;
  419. pst[old_pid].cells = 0;
  420. pst[new_pid].edges += pst[old_pid].edges;
  421. pst[old_pid].edges = 0;
  422. if (old_pid == pid)
  423. pid--;
  424. }
  425. else {
  426. pst[new_pid].cells++;
  427. }
  428. }
  429. connected = 1;
  430. }
  431. if (!connected) {
  432. /* start new patch */
  433. npatch++;
  434. pid++;
  435. pid_corr[j] = pid;
  436. if (pid >= nalloc) {
  437. pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
  438. for (k = nalloc; k < pid + incr; k++) {
  439. pst[k].cells = 0;
  440. pst[k].edges = 0;
  441. }
  442. nalloc = pid + incr;
  443. }
  444. pst[pid].cells = 1;
  445. pst[pid].type.t = CELL_TYPE;
  446. pst[pid].type.val.c = corrCell;
  447. }
  448. /* update edge count for corr */
  449. if (Rast_is_d_null_value(&precCell) || precCell != corrCell)
  450. pst[pid_corr[j]].edges++;
  451. if (Rast_is_d_null_value(&supCell) || supCell != corrCell)
  452. pst[pid_corr[j]].edges++;
  453. if (i == ad->rl - 1)
  454. pst[pid_corr[j]].edges++;
  455. if (j == ad->cl - 1)
  456. pst[pid_corr[j]].edges++;
  457. /* update edge count for prec */
  458. if (!Rast_is_d_null_value(&precCell) && precCell != corrCell)
  459. pst[pid_corr[j - 1]].edges++;
  460. /* update edge count for sup */
  461. if (!Rast_is_d_null_value(&supCell) && supCell != corrCell)
  462. pst[pid_sup[j]].edges++;
  463. precCell = corrCell;
  464. }
  465. }
  466. if (npatch > 0) {
  467. double edges, cells;
  468. edges = cells = 0;
  469. for (i = 1; i <= pid; i++) {
  470. cells += pst[i].cells;
  471. edges += pst[i].edges;
  472. }
  473. *result = 0.25 * edges / sqrt(cells);
  474. }
  475. else {
  476. Rast_set_d_null_value(result, 1);
  477. }
  478. if (masked) {
  479. close(mask_fd);
  480. G_free(mask_buf);
  481. G_free(mask_sup);
  482. }
  483. G_free(buf_null);
  484. G_free(pid_corr);
  485. G_free(pid_sup);
  486. G_free(pst);
  487. return RLI_OK;
  488. }
  489. int calculateF(int fd, struct area_entry *ad, double *result)
  490. {
  491. FCELL *buf, *buf_sup, *buf_null;
  492. FCELL corrCell, precCell, supCell;
  493. long npatch;
  494. long pid, old_pid, new_pid, *pid_corr, *pid_sup, *ltmp;
  495. struct pst *pst;
  496. long nalloc, incr;
  497. int i, j, k;
  498. int connected;
  499. int mask_fd, *mask_buf, *mask_sup, *mask_tmp, masked;
  500. buf_null = Rast_allocate_f_buf();
  501. Rast_set_f_null_value(buf_null, Rast_window_cols());
  502. buf_sup = buf_null;
  503. /* initialize patch ids */
  504. pid_corr = G_malloc(ad->cl * sizeof(long));
  505. pid_sup = G_malloc(ad->cl * sizeof(long));
  506. for (j = 0; j < ad->cl; j++) {
  507. pid_corr[j] = 0;
  508. pid_sup[j] = 0;
  509. }
  510. /* open mask if needed */
  511. mask_fd = -1;
  512. mask_buf = mask_sup = NULL;
  513. masked = FALSE;
  514. if (ad->mask == 1) {
  515. if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
  516. return RLI_ERRORE;
  517. mask_buf = G_malloc(ad->cl * sizeof(int));
  518. if (mask_buf == NULL) {
  519. G_fatal_error("malloc mask_buf failed");
  520. return RLI_ERRORE;
  521. }
  522. mask_sup = G_malloc(ad->cl * sizeof(int));
  523. if (mask_sup == NULL) {
  524. G_fatal_error("malloc mask_buf failed");
  525. return RLI_ERRORE;
  526. }
  527. for (j = 0; j < ad->cl; j++)
  528. mask_buf[j] = 0;
  529. masked = TRUE;
  530. }
  531. /* calculate number of patches */
  532. npatch = 0;
  533. pid = 0;
  534. /* patch size and type */
  535. incr = 1024;
  536. if (incr > ad->rl)
  537. incr = ad->rl;
  538. if (incr > ad->cl)
  539. incr = ad->cl;
  540. if (incr < 2)
  541. incr = 2;
  542. nalloc = incr;
  543. pst = G_malloc(nalloc * sizeof(struct pst));
  544. for (k = 0; k < nalloc; k++) {
  545. pst[k].cells = 0;
  546. pst[k].edges = 0;
  547. }
  548. for (i = 0; i < ad->rl; i++) {
  549. buf = RLI_get_fcell_raster_row(fd, i + ad->y, ad);
  550. if (i > 0) {
  551. buf_sup = RLI_get_fcell_raster_row(fd, i - 1 + ad->y, ad);
  552. }
  553. if (masked) {
  554. mask_tmp = mask_sup;
  555. mask_sup = mask_buf;
  556. mask_buf = mask_tmp;
  557. if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
  558. return 0;
  559. }
  560. ltmp = pid_sup;
  561. pid_sup = pid_corr;
  562. pid_corr = ltmp;
  563. Rast_set_f_null_value(&precCell, 1);
  564. connected = 0;
  565. for (j = 0; j < ad->cl; j++) {
  566. pid_corr[j] = 0;
  567. corrCell = buf[j + ad->x];
  568. if (masked && (mask_buf[j] == 0)) {
  569. Rast_set_f_null_value(&corrCell, 1);
  570. }
  571. supCell = buf_sup[j + ad->x];
  572. if (masked && (mask_sup[j] == 0)) {
  573. Rast_set_f_null_value(&supCell, 1);
  574. }
  575. if (Rast_is_f_null_value(&corrCell)) {
  576. if (!Rast_is_f_null_value(&precCell))
  577. pst[pid_corr[j - 1]].edges++;
  578. if (!Rast_is_f_null_value(&supCell))
  579. pst[pid_sup[j]].edges++;
  580. connected = 0;
  581. precCell = corrCell;
  582. continue;
  583. }
  584. if (!Rast_is_f_null_value(&precCell) && corrCell == precCell) {
  585. pid_corr[j] = pid_corr[j - 1];
  586. connected = 1;
  587. pst[pid_corr[j]].cells++;
  588. }
  589. else {
  590. connected = 0;
  591. }
  592. if (!Rast_is_f_null_value(&supCell) && corrCell == supCell) {
  593. if (pid_corr[j] != pid_sup[j]) {
  594. /* connect or merge */
  595. /* after r.clump */
  596. if (connected) {
  597. npatch--;
  598. if (npatch == 0) {
  599. G_fatal_error("npatch == 0 at row %d, col %d", i, j);
  600. }
  601. }
  602. old_pid = pid_corr[j];
  603. new_pid = pid_sup[j];
  604. pid_corr[j] = new_pid;
  605. if (old_pid > 0) {
  606. /* merge */
  607. /* update left side of the current row */
  608. for (k = 0; k < j; k++) {
  609. if (pid_corr[k] == old_pid)
  610. pid_corr[k] = new_pid;
  611. }
  612. /* update right side of the previous row */
  613. for (k = j + 1; k < ad->cl; k++) {
  614. if (pid_sup[k] == old_pid)
  615. pid_sup[k] = new_pid;
  616. }
  617. pst[new_pid].cells += pst[old_pid].cells;
  618. pst[old_pid].cells = 0;
  619. pst[new_pid].edges += pst[old_pid].edges;
  620. pst[old_pid].edges = 0;
  621. if (old_pid == pid)
  622. pid--;
  623. }
  624. else {
  625. pst[new_pid].cells++;
  626. }
  627. }
  628. connected = 1;
  629. }
  630. if (!connected) {
  631. /* start new patch */
  632. npatch++;
  633. pid++;
  634. pid_corr[j] = pid;
  635. if (pid >= nalloc) {
  636. pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
  637. for (k = nalloc; k < pid + incr; k++) {
  638. pst[k].cells = 0;
  639. pst[k].edges = 0;
  640. }
  641. nalloc = pid + incr;
  642. }
  643. pst[pid].cells = 1;
  644. pst[pid].type.t = CELL_TYPE;
  645. pst[pid].type.val.c = corrCell;
  646. }
  647. /* update edge count for corr */
  648. if (Rast_is_f_null_value(&precCell) || precCell != corrCell)
  649. pst[pid_corr[j]].edges++;
  650. if (Rast_is_f_null_value(&supCell) || supCell != corrCell)
  651. pst[pid_corr[j]].edges++;
  652. if (i == ad->rl - 1)
  653. pst[pid_corr[j]].edges++;
  654. if (j == ad->cl - 1)
  655. pst[pid_corr[j]].edges++;
  656. /* update edge count for prec */
  657. if (!Rast_is_f_null_value(&precCell) && precCell != corrCell)
  658. pst[pid_corr[j - 1]].edges++;
  659. /* update edge count for sup */
  660. if (!Rast_is_f_null_value(&supCell) && supCell != corrCell)
  661. pst[pid_sup[j]].edges++;
  662. precCell = corrCell;
  663. }
  664. }
  665. if (npatch > 0) {
  666. double edges, cells;
  667. edges = cells = 0;
  668. for (i = 1; i <= pid; i++) {
  669. cells += pst[i].cells;
  670. edges += pst[i].edges;
  671. }
  672. *result = 0.25 * edges / sqrt(cells);
  673. }
  674. else {
  675. Rast_set_d_null_value(result, 1);
  676. }
  677. if (masked) {
  678. close(mask_fd);
  679. G_free(mask_buf);
  680. G_free(mask_sup);
  681. }
  682. G_free(buf_null);
  683. G_free(pid_corr);
  684. G_free(pid_sup);
  685. G_free(pst);
  686. return RLI_OK;
  687. }