padcv.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /*
  2. * \brief calculates coefficient of variation of patch area
  3. *
  4. * \AUTHOR: Serena Pallecchi student of Computer Science University of Pisa (Italy)
  5. * Commission from Faunalia Pontedera (PI) www.faunalia.it
  6. *
  7. * This program is free software under the GPL (>=v2)
  8. * Read the COPYING file that comes with GRASS for details.
  9. *
  10. */
  11. #include <grass/gis.h>
  12. #include <grass/raster.h>
  13. #include <grass/glocale.h>
  14. #include <stdlib.h>
  15. #include <fcntl.h>
  16. #include <math.h>
  17. #include "../r.li.daemon/defs.h"
  18. #include "../r.li.daemon/avlDefs.h"
  19. #include "../r.li.daemon/avlID.h"
  20. #include "../r.li.daemon/GenericCell.h"
  21. #include "../r.li.daemon/daemon.h"
  22. int calculate(int fd, struct area_entry *ad, double *result);
  23. int calculateD(int fd, struct area_entry *ad, double *result);
  24. int calculateF(int fd, struct area_entry *ad, double *result);
  25. int main(int argc, char *argv[])
  26. {
  27. struct Option *raster, *conf, *output;
  28. struct GModule *module;
  29. G_gisinit(argv[0]);
  30. module = G_define_module();
  31. module->description =
  32. _("Calculates coefficient of variation of patch area on a raster map");
  33. G_add_keyword(_("raster"));
  34. G_add_keyword(_("landscape structure analysis"));
  35. G_add_keyword(_("patch index"));
  36. /* define options */
  37. raster = G_define_standard_option(G_OPT_R_INPUT);
  38. conf = G_define_standard_option(G_OPT_F_INPUT);
  39. conf->key = "config";
  40. conf->description = _("Configuration file");
  41. conf->required = YES;
  42. output = G_define_standard_option(G_OPT_R_OUTPUT);
  43. if (G_parser(argc, argv))
  44. exit(EXIT_FAILURE);
  45. return calculateIndex(conf->answer, patchAreaDistributionCV, NULL,
  46. raster->answer, output->answer);
  47. }
  48. int patchAreaDistributionCV(int fd, char **par, struct area_entry *ad, double *result)
  49. {
  50. double indice = 0;
  51. struct Cell_head hd;
  52. int ris = RLI_OK;
  53. Rast_get_cellhd(ad->raster, "", &hd);
  54. switch (ad->data_type) {
  55. case CELL_TYPE:
  56. {
  57. ris = calculate(fd, ad, &indice);
  58. break;
  59. }
  60. case DCELL_TYPE:
  61. {
  62. ris = calculateD(fd, ad, &indice);
  63. break;
  64. }
  65. case FCELL_TYPE:
  66. {
  67. ris = calculateF(fd, ad, &indice);
  68. break;
  69. }
  70. default:
  71. {
  72. G_fatal_error("data type unknown");
  73. return RLI_ERRORE;
  74. }
  75. }
  76. if (ris != RLI_OK) {
  77. *result = -1;
  78. return RLI_ERRORE;
  79. }
  80. *result = indice;
  81. return RLI_OK;
  82. }
  83. int calculate(int fd, struct area_entry *ad, double *result)
  84. {
  85. CELL *buf;
  86. CELL *buf_sup;
  87. CELL corrCell;
  88. CELL precCell;
  89. CELL supCell;
  90. int i, j;
  91. int mask_fd = -1, *mask_buf;
  92. int ris = 0;
  93. int masked = FALSE;
  94. int areaPatch = 0; /*if all cells are null areaPatch=0 */
  95. long npatch = 0;
  96. long tot = 0;
  97. long zero = 0;
  98. long totCorr = 0;
  99. long idCorr = 0;
  100. long lastId = 0;
  101. long doppi = 0;
  102. long np = 0;
  103. long *mask_patch_sup;
  104. long *mask_patch_corr;
  105. double indice = 0;
  106. double somma = 0;
  107. double area = 0;
  108. double mn = 0;
  109. double sd = 0;
  110. double cv = 0;
  111. avlID_tree albero = NULL;
  112. avlID_table *array = NULL;
  113. generic_cell gc;
  114. gc.t = CELL_TYPE;
  115. /* open mask if needed */
  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. masked = TRUE;
  125. }
  126. mask_patch_sup = G_malloc(ad->cl * sizeof(long));
  127. if (mask_patch_sup == NULL) {
  128. G_fatal_error("malloc mask_patch_sup failed");
  129. return RLI_ERRORE;
  130. }
  131. mask_patch_corr = G_malloc(ad->cl * sizeof(long));
  132. if (mask_patch_corr == NULL) {
  133. G_fatal_error("malloc mask_patch_corr failed");
  134. return RLI_ERRORE;
  135. }
  136. buf_sup = Rast_allocate_c_buf();
  137. if (buf_sup == NULL) {
  138. G_fatal_error("malloc buf_sup failed");
  139. return RLI_ERRORE;
  140. }
  141. buf = Rast_allocate_c_buf();
  142. if (buf == NULL) {
  143. G_fatal_error("malloc buf failed");
  144. return RLI_ERRORE;
  145. }
  146. Rast_set_c_null_value(buf_sup + ad->x, ad->cl); /*the first time buf_sup is all null */
  147. for (i = 0; i < ad->cl; i++) {
  148. mask_patch_sup[i] = 0;
  149. mask_patch_corr[i] = 0;
  150. }
  151. for (j = 0; j < ad->rl; j++)
  152. /*for each raster row */
  153. {
  154. if (j > 0) {
  155. buf_sup = RLI_get_cell_raster_row(fd, j - 1 + ad->y, ad);
  156. }
  157. buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
  158. if (masked) {
  159. if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
  160. G_fatal_error("mask read failed");
  161. return RLI_ERRORE;
  162. }
  163. }
  164. Rast_set_c_null_value(&precCell, 1);
  165. for (i = 0; i < ad->cl; i++)
  166. /* for each cell in the row */
  167. {
  168. area++;
  169. corrCell = buf[i + ad->x];
  170. if (masked && mask_buf[i + ad->x] == 0) {
  171. Rast_set_c_null_value(&corrCell, 1);
  172. area--;
  173. }
  174. if (!(Rast_is_null_value(&corrCell, gc.t))) {
  175. areaPatch++;
  176. if (i > 0)
  177. precCell = buf[i - 1 + ad->x];
  178. if (j == 0)
  179. Rast_set_c_null_value(&supCell, 1);
  180. else
  181. supCell = buf_sup[i + ad->x];
  182. if (corrCell != precCell)
  183. /* ?
  184. * 1 2
  185. * */
  186. {
  187. if (corrCell != supCell) {
  188. /* 3
  189. * 1 2
  190. * */
  191. /*new patch */
  192. if (idCorr == 0) { /*first found patch */
  193. lastId = 1;
  194. idCorr = 1;
  195. totCorr = 1;
  196. mask_patch_corr[i] = idCorr;
  197. }
  198. else
  199. /*not first patch */
  200. /* put in the tree the previous value */
  201. {
  202. if (albero == NULL) {
  203. albero = avlID_make(idCorr, totCorr);
  204. if (albero == NULL) {
  205. G_fatal_error("avlID_make error");
  206. return RLI_ERRORE;
  207. }
  208. npatch++;
  209. }
  210. else
  211. /*tree not empty */
  212. {
  213. ris = avlID_add(&albero, idCorr, totCorr);
  214. switch (ris) {
  215. case AVL_ERR:
  216. {
  217. G_fatal_error("avlID_add error");
  218. return RLI_ERRORE;
  219. }
  220. case AVL_ADD:
  221. {
  222. npatch++;
  223. break;
  224. }
  225. case AVL_PRES:
  226. {
  227. break;
  228. }
  229. default:
  230. {
  231. G_fatal_error
  232. ("avlID_add unknown error");
  233. return RLI_ERRORE;
  234. }
  235. }
  236. }
  237. totCorr = 1;
  238. lastId++;
  239. idCorr = lastId;
  240. mask_patch_corr[i] = idCorr;
  241. }
  242. }
  243. else
  244. /* current cell and upper cell are equal */
  245. /* 2
  246. * 1 2
  247. * */
  248. {
  249. if (albero == NULL) {
  250. albero = avlID_make(idCorr, totCorr);
  251. if (albero == NULL) {
  252. G_fatal_error("avlID_make error");
  253. return RLI_ERRORE;
  254. }
  255. npatch++;
  256. }
  257. else { /*tree not null */
  258. ris = avlID_add(&albero, idCorr, totCorr);
  259. switch (ris) {
  260. case AVL_ERR:
  261. {
  262. G_fatal_error("avlID_add error");
  263. return RLI_ERRORE;
  264. }
  265. case AVL_ADD:
  266. {
  267. npatch++;
  268. break;
  269. }
  270. case AVL_PRES:
  271. {
  272. break;
  273. }
  274. default:
  275. {
  276. G_fatal_error("avlID_add unknown error");
  277. return RLI_ERRORE;
  278. }
  279. }
  280. }
  281. idCorr = mask_patch_sup[i];
  282. mask_patch_corr[i] = idCorr;
  283. totCorr = 1;
  284. }
  285. }
  286. else { /*current cell and previuos cell are equal */
  287. /* ?
  288. * 1 1
  289. */
  290. if (corrCell == supCell) { /*current cell and upper cell are equal */
  291. /* 1
  292. * 1 1
  293. */
  294. if (mask_patch_sup[i] != mask_patch_corr[i - 1]) {
  295. long r = 0;
  296. long del = mask_patch_sup[i];
  297. r = avlID_sub(&albero, del); /*r=number of cell of patch removed */
  298. if (r == 0) {
  299. G_fatal_error("avlID_sub error");
  300. return RLI_ERRORE;
  301. }
  302. /*Remove one patch because it makes part of a patch already found */
  303. ris = avlID_add(&albero, idCorr, r);
  304. switch (ris) {
  305. case AVL_ERR:
  306. {
  307. G_fatal_error("avlID_add error");
  308. return RLI_ERRORE;
  309. }
  310. case AVL_ADD:
  311. {
  312. npatch++;
  313. break;
  314. }
  315. case AVL_PRES:
  316. {
  317. break;
  318. }
  319. default:
  320. {
  321. G_fatal_error("avlID_add unknown error");
  322. return RLI_ERRORE;
  323. }
  324. }
  325. r = i;
  326. while (r < ad->cl) {
  327. if (mask_patch_sup[r] == del) {
  328. mask_patch_sup[r] = idCorr;
  329. }
  330. r++;
  331. }
  332. mask_patch_corr[i] = idCorr;
  333. }
  334. else {
  335. mask_patch_corr[i] = idCorr;
  336. }
  337. }
  338. else { /*current cell and upper cell are not equal */
  339. /* 2
  340. * 1 1
  341. */
  342. mask_patch_corr[i] = idCorr;
  343. }
  344. totCorr++;
  345. }
  346. }
  347. else { /*cell is null or is not to consider */
  348. mask_patch_corr[i] = 0;
  349. }
  350. }
  351. {
  352. int ii;
  353. long c;
  354. for (ii = 0; ii < ad->cl; ii++) {
  355. c = mask_patch_corr[ii];
  356. mask_patch_sup[ii] = c;
  357. mask_patch_corr[ii] = 0;
  358. }
  359. }
  360. }
  361. if (areaPatch != 0) {
  362. if (albero == NULL) {
  363. albero = avlID_make(idCorr, totCorr);
  364. if (albero == NULL) {
  365. G_fatal_error("avlID_make error");
  366. return RLI_ERRORE;
  367. }
  368. npatch++;
  369. }
  370. else {
  371. ris = avlID_add(&albero, idCorr, totCorr);
  372. switch (ris) {
  373. case AVL_ERR:
  374. {
  375. G_fatal_error("avlID_add error");
  376. return RLI_ERRORE;
  377. }
  378. case AVL_ADD:
  379. {
  380. npatch++;
  381. break;
  382. }
  383. case AVL_PRES:
  384. {
  385. break;
  386. }
  387. default:
  388. {
  389. G_fatal_error("avlID_add unknown error");
  390. return RLI_ERRORE;
  391. }
  392. }
  393. }
  394. array = G_malloc(npatch * sizeof(avlID_tableRow));
  395. if (array == NULL) {
  396. G_fatal_error("malloc array failed");
  397. return RLI_ERRORE;
  398. }
  399. tot = avlID_to_array(albero, zero, array);
  400. if (tot != npatch) {
  401. G_warning
  402. ("avlID_to_array unaspected value. the result could be wrong");
  403. return RLI_ERRORE;
  404. }
  405. for (i = 0; i < npatch; i++) {
  406. if (array[i]->tot == 0) {
  407. doppi++;
  408. }
  409. }
  410. np = npatch;
  411. npatch = npatch - doppi;
  412. mn = areaPatch / npatch;
  413. /* calculate summary */
  414. for (i = 0; i < np; i++) {
  415. long areaPi = 0;
  416. double diff;
  417. if (array[i]->tot != 0) {
  418. ris = ris + array[i]->tot;
  419. areaPi = (double)array[i]->tot;
  420. diff = areaPi - mn;
  421. somma = somma + (diff * diff);
  422. }
  423. }
  424. sd = sqrt(somma / npatch);
  425. cv = sd * 100 / mn;
  426. indice = cv;
  427. G_free(array);
  428. }
  429. else
  430. indice = (double)(-1);
  431. if (masked)
  432. G_free(mask_buf);
  433. G_free(mask_patch_sup);
  434. *result = indice;
  435. G_free(buf_sup);
  436. return RLI_OK;
  437. }
  438. int calculateD(int fd, struct area_entry *ad, double *result)
  439. {
  440. DCELL *buf;
  441. DCELL *buf_sup;
  442. DCELL corrCell;
  443. DCELL precCell;
  444. DCELL supCell;
  445. int i, j;
  446. int mask_fd = -1, *mask_buf;
  447. int ris = 0;
  448. int masked = FALSE;
  449. int areaPatch = 0; /*if all cells are null areaPatch=0 */
  450. long npatch = 0;
  451. long tot = 0;
  452. long zero = 0;
  453. long totCorr = 0;
  454. long idCorr = 0;
  455. long lastId = 0;
  456. long doppi = 0;
  457. long np = 0;
  458. long *mask_patch_sup;
  459. long *mask_patch_corr;
  460. double indice = 0;
  461. double somma = 0;
  462. double area = 0;
  463. double mn = 0;
  464. double sd = 0;
  465. double cv = 0;
  466. avlID_tree albero = NULL;
  467. avlID_table *array = NULL;
  468. generic_cell gc;
  469. gc.t = DCELL_TYPE;
  470. /* open mask if needed */
  471. if (ad->mask == 1) {
  472. if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
  473. return RLI_ERRORE;
  474. mask_buf = G_malloc(ad->cl * sizeof(int));
  475. if (mask_buf == NULL) {
  476. G_fatal_error("malloc mask_buf failed");
  477. return RLI_ERRORE;
  478. }
  479. masked = TRUE;
  480. }
  481. mask_patch_sup = G_malloc(ad->cl * sizeof(long));
  482. if (mask_patch_sup == NULL) {
  483. G_fatal_error("malloc mask_patch_sup failed");
  484. return RLI_ERRORE;
  485. }
  486. mask_patch_corr = G_malloc(ad->cl * sizeof(long));
  487. if (mask_patch_corr == NULL) {
  488. G_fatal_error("malloc mask_patch_corr failed");
  489. return RLI_ERRORE;
  490. }
  491. buf_sup = Rast_allocate_d_buf();
  492. if (buf_sup == NULL) {
  493. G_fatal_error("malloc buf_sup failed");
  494. return RLI_ERRORE;
  495. }
  496. buf = Rast_allocate_d_buf();
  497. if (buf == NULL) {
  498. G_fatal_error("malloc buf failed");
  499. return RLI_ERRORE;
  500. }
  501. Rast_set_d_null_value(buf_sup + ad->x, ad->cl); /*the first time buf_sup is all null */
  502. for (i = 0; i < ad->cl; i++) {
  503. mask_patch_sup[i] = 0;
  504. mask_patch_corr[i] = 0;
  505. }
  506. for (j = 0; j < ad->rl; j++)
  507. /*for each raster row */
  508. {
  509. if (j > 0) {
  510. buf_sup = RLI_get_dcell_raster_row(fd, j - 1 + ad->y, ad);
  511. }
  512. buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
  513. if (masked) {
  514. if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
  515. G_fatal_error("mask read failed");
  516. return RLI_ERRORE;
  517. }
  518. }
  519. Rast_set_d_null_value(&precCell, 1);
  520. for (i = 0; i < ad->cl; i++) {
  521. /* for each dcell in the row */
  522. area++;
  523. corrCell = buf[i + ad->x];
  524. if (masked && mask_buf[i + ad->x] == 0) {
  525. Rast_set_d_null_value(&corrCell, 1);
  526. area--;
  527. }
  528. if (!(Rast_is_null_value(&corrCell, gc.t))) {
  529. areaPatch++;
  530. if (i > 0)
  531. precCell = buf[i - 1 + ad->x];
  532. if (j == 0)
  533. Rast_set_d_null_value(&supCell, 1);
  534. else
  535. supCell = buf_sup[i + ad->x];
  536. if (corrCell != precCell)
  537. /* ?
  538. * 1 2
  539. * */
  540. {
  541. if (corrCell != supCell) {
  542. /* 3
  543. * 1 2
  544. * */
  545. /*new patch */
  546. if (idCorr == 0) { /*first patch */
  547. lastId = 1;
  548. idCorr = 1;
  549. totCorr = 1;
  550. mask_patch_corr[i] = idCorr;
  551. }
  552. else
  553. /*not first patch */
  554. /* put in the tree the previous value */
  555. {
  556. if (albero == NULL) {
  557. albero = avlID_make(idCorr, totCorr);
  558. if (albero == NULL) {
  559. G_fatal_error("avlID_make error");
  560. return RLI_ERRORE;
  561. }
  562. npatch++;
  563. }
  564. else
  565. /*tree not empty */
  566. {
  567. ris = avlID_add(&albero, idCorr, totCorr);
  568. switch (ris) {
  569. case AVL_ERR:
  570. {
  571. G_fatal_error("avlID_add error");
  572. return RLI_ERRORE;
  573. }
  574. case AVL_ADD:
  575. {
  576. npatch++;
  577. break;
  578. }
  579. case AVL_PRES:
  580. {
  581. break;
  582. }
  583. default:
  584. {
  585. G_fatal_error
  586. ("avlID_add unknown error");
  587. return RLI_ERRORE;
  588. }
  589. }
  590. }
  591. totCorr = 1;
  592. lastId++;
  593. idCorr = lastId;
  594. mask_patch_corr[i] = idCorr;
  595. }
  596. }
  597. else
  598. /*current cell and upper cell are equal */
  599. /* 2
  600. * 1 2
  601. * */
  602. {
  603. if (albero == NULL) {
  604. albero = avlID_make(idCorr, totCorr);
  605. if (albero == NULL) {
  606. G_fatal_error("avlID_make error");
  607. return RLI_ERRORE;
  608. }
  609. npatch++;
  610. }
  611. else { /*tree not null */
  612. ris = avlID_add(&albero, idCorr, totCorr);
  613. switch (ris) {
  614. case AVL_ERR:
  615. {
  616. G_fatal_error("avlID_add error");
  617. return RLI_ERRORE;
  618. }
  619. case AVL_ADD:
  620. {
  621. npatch++;
  622. break;
  623. }
  624. case AVL_PRES:
  625. {
  626. break;
  627. }
  628. default:
  629. {
  630. G_fatal_error("avlID_add unknown error");
  631. return RLI_ERRORE;
  632. }
  633. }
  634. }
  635. idCorr = mask_patch_sup[i];
  636. mask_patch_corr[i] = idCorr;
  637. totCorr = 1;
  638. }
  639. }
  640. else { /*current cell and previuos cell are equal */
  641. /* ?
  642. * 1 1
  643. */
  644. if (corrCell == supCell) { /*current cell and upper cell are equal */
  645. /* 1
  646. * 1 1
  647. */
  648. if (mask_patch_sup[i] != mask_patch_corr[i - 1]) {
  649. long r = 0;
  650. long del = mask_patch_sup[i];
  651. r = avlID_sub(&albero, del); /*r=number of cell of patch removed */
  652. if (r == 0) {
  653. G_fatal_error("avlID_sub error");
  654. return RLI_ERRORE;
  655. }
  656. /*Remove one patch because it makes part of a patch already found */
  657. ris = avlID_add(&albero, idCorr, r);
  658. switch (ris) {
  659. case AVL_ERR:
  660. {
  661. G_fatal_error("avlID_add error");
  662. return RLI_ERRORE;
  663. }
  664. case AVL_ADD:
  665. {
  666. npatch++;
  667. break;
  668. }
  669. case AVL_PRES:
  670. {
  671. break;
  672. }
  673. default:
  674. {
  675. G_fatal_error("avlID_add unknown error");
  676. return RLI_ERRORE;
  677. }
  678. }
  679. r = i;
  680. while (r < ad->cl) {
  681. if (mask_patch_sup[r] == del) {
  682. mask_patch_sup[r] = idCorr;
  683. }
  684. r++;
  685. }
  686. mask_patch_corr[i] = idCorr;
  687. }
  688. else {
  689. mask_patch_corr[i] = idCorr;
  690. }
  691. }
  692. else { /*current cell and upper cell are not equal */
  693. /* 2
  694. * 1 1
  695. */
  696. mask_patch_corr[i] = idCorr;
  697. }
  698. totCorr++;
  699. }
  700. }
  701. else { /*cell is null or is not to consider */
  702. mask_patch_corr[i] = 0;
  703. }
  704. }
  705. {
  706. int ii;
  707. long c;
  708. for (ii = 0; ii < ad->cl; ii++) {
  709. c = mask_patch_corr[ii];
  710. mask_patch_sup[ii] = c;
  711. mask_patch_corr[ii] = 0;
  712. }
  713. }
  714. }
  715. if (areaPatch != 0) {
  716. if (albero == NULL) {
  717. albero = avlID_make(idCorr, totCorr);
  718. if (albero == NULL) {
  719. G_fatal_error("avlID_make error");
  720. return RLI_ERRORE;
  721. }
  722. npatch++;
  723. }
  724. else {
  725. ris = avlID_add(&albero, idCorr, totCorr);
  726. switch (ris) {
  727. case AVL_ERR:
  728. {
  729. G_fatal_error("avlID_add error");
  730. return RLI_ERRORE;
  731. }
  732. case AVL_ADD:
  733. {
  734. npatch++;
  735. break;
  736. }
  737. case AVL_PRES:
  738. {
  739. break;
  740. }
  741. default:
  742. {
  743. G_fatal_error("avlID_add unknown error");
  744. return RLI_ERRORE;
  745. }
  746. }
  747. }
  748. array = G_malloc(npatch * sizeof(avlID_tableRow));
  749. if (array == NULL) {
  750. G_fatal_error("malloc array failed");
  751. return RLI_ERRORE;
  752. }
  753. tot = avlID_to_array(albero, zero, array);
  754. if (tot != npatch) {
  755. G_warning
  756. ("avlID_to_array unaspected value. the result could be wrong");
  757. return RLI_ERRORE;
  758. }
  759. for (i = 0; i < npatch; i++) {
  760. if (array[i]->tot == 0) {
  761. doppi++;
  762. }
  763. }
  764. np = npatch;
  765. npatch = npatch - doppi;
  766. mn = areaPatch / npatch;
  767. /* calculate summary */
  768. for (i = 0; i < np; i++) {
  769. long areaPi = 0;
  770. double diff;
  771. if (array[i]->tot != 0) {
  772. ris = ris + array[i]->tot;
  773. areaPi = (double)array[i]->tot;
  774. diff = areaPi - mn;
  775. somma = somma + (diff * diff);
  776. }
  777. }
  778. sd = sqrt(somma / npatch);
  779. cv = sd * 100 / mn;
  780. indice = cv;
  781. G_free(array);
  782. }
  783. else
  784. indice = (double)(-1);
  785. if (masked)
  786. G_free(mask_buf);
  787. G_free(mask_patch_sup);
  788. *result = indice;
  789. return RLI_OK;
  790. }
  791. int calculateF(int fd, struct area_entry *ad, double *result)
  792. {
  793. FCELL *buf;
  794. FCELL *buf_sup;
  795. FCELL corrCell;
  796. FCELL precCell;
  797. FCELL supCell;
  798. int i, j;
  799. int mask_fd = -1, *mask_buf;
  800. int ris = 0;
  801. int masked = FALSE;
  802. int areaPatch = 0; /*if all cells are null areaPatch=0 */
  803. long npatch = 0;
  804. long np = 0;
  805. long tot = 0;
  806. long zero = 0;
  807. long totCorr = 0;
  808. long idCorr = 0;
  809. long lastId = 0;
  810. long doppi = 0;
  811. long *mask_patch_sup;
  812. long *mask_patch_corr;
  813. double indice = 0;
  814. double somma = 0;
  815. double area = 0;
  816. double mn = 0;
  817. double sd = 0;
  818. double cv = 0;
  819. avlID_tree albero = NULL;
  820. avlID_table *array = NULL;
  821. generic_cell gc;
  822. gc.t = FCELL_TYPE;
  823. /* open mask if needed */
  824. if (ad->mask == 1) {
  825. if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
  826. return RLI_ERRORE;
  827. mask_buf = G_malloc(ad->cl * sizeof(int));
  828. if (mask_buf == NULL) {
  829. G_fatal_error("malloc mask_buf failed");
  830. return RLI_ERRORE;
  831. }
  832. masked = TRUE;
  833. }
  834. mask_patch_sup = G_malloc(ad->cl * sizeof(long));
  835. if (mask_patch_sup == NULL) {
  836. G_fatal_error("malloc mask_patch_sup failed");
  837. return RLI_ERRORE;
  838. }
  839. mask_patch_corr = G_malloc(ad->cl * sizeof(long));
  840. if (mask_patch_corr == NULL) {
  841. G_fatal_error("malloc mask_patch_corr failed");
  842. return RLI_ERRORE;
  843. }
  844. buf_sup = Rast_allocate_f_buf();
  845. if (buf_sup == NULL) {
  846. G_fatal_error("malloc buf_sup failed");
  847. return RLI_ERRORE;
  848. }
  849. buf = Rast_allocate_f_buf();
  850. if (buf == NULL) {
  851. G_fatal_error("malloc buf failed");
  852. return RLI_ERRORE;
  853. }
  854. Rast_set_f_null_value(buf_sup + ad->x, ad->cl); /*the first time buf_sup is all null */
  855. for (i = 0; i < ad->cl; i++) {
  856. mask_patch_sup[i] = 0;
  857. mask_patch_corr[i] = 0;
  858. }
  859. for (j = 0; j < ad->rl; j++)
  860. /*for each raster row */
  861. {
  862. if (j > 0) {
  863. buf_sup = RLI_get_fcell_raster_row(fd, j - 1 + ad->y, ad);
  864. }
  865. buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
  866. if (masked) {
  867. if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
  868. G_fatal_error("mask read failed");
  869. return RLI_ERRORE;
  870. }
  871. }
  872. Rast_set_f_null_value(&precCell, 1);
  873. for (i = 0; i < ad->cl; i++) {
  874. /* for each fcell in the row */
  875. area++;
  876. corrCell = buf[i + ad->x];
  877. if (masked && mask_buf[i + ad->x] == 0) {
  878. Rast_set_f_null_value(&corrCell, 1);
  879. area--;
  880. }
  881. if (!(Rast_is_null_value(&corrCell, gc.t))) {
  882. areaPatch++;
  883. if (i > 0)
  884. precCell = buf[i - 1 + ad->x];
  885. if (j == 0)
  886. Rast_set_f_null_value(&supCell, 1);
  887. else
  888. supCell = buf_sup[i + ad->x];
  889. if (corrCell != precCell)
  890. /* ?
  891. * 1 2
  892. * */
  893. {
  894. if (corrCell != supCell)
  895. /* 3
  896. * 1 2
  897. * */
  898. {
  899. /*new patch */
  900. if (idCorr == 0) { /*first patch */
  901. lastId = 1;
  902. idCorr = 1;
  903. totCorr = 1;
  904. mask_patch_corr[i] = idCorr;
  905. }
  906. else
  907. /*not first patch */
  908. /* put in the tree the previous value */
  909. {
  910. if (albero == NULL) {
  911. albero = avlID_make(idCorr, totCorr);
  912. if (albero == NULL) {
  913. G_fatal_error("avlID_make error");
  914. return RLI_ERRORE;
  915. }
  916. npatch++;
  917. }
  918. else
  919. /*tree not empty */
  920. {
  921. ris = avlID_add(&albero, idCorr, totCorr);
  922. switch (ris) {
  923. case AVL_ERR:
  924. {
  925. G_fatal_error("avlID_add error");
  926. return RLI_ERRORE;
  927. }
  928. case AVL_ADD:
  929. {
  930. npatch++;
  931. break;
  932. }
  933. case AVL_PRES:
  934. {
  935. break;
  936. }
  937. default:
  938. {
  939. G_fatal_error
  940. ("avlID_add unknown error");
  941. return RLI_ERRORE;
  942. }
  943. }
  944. }
  945. totCorr = 1;
  946. lastId++;
  947. idCorr = lastId;
  948. mask_patch_corr[i] = idCorr;
  949. }
  950. }
  951. else
  952. /*current cell and upper cell are equal */
  953. /* 2
  954. * 1 2
  955. * */
  956. {
  957. if (albero == NULL) {
  958. albero = avlID_make(idCorr, totCorr);
  959. if (albero == NULL) {
  960. G_fatal_error("avlID_make error");
  961. return RLI_ERRORE;
  962. }
  963. npatch++;
  964. }
  965. else { /*tree not null */
  966. ris = avlID_add(&albero, idCorr, totCorr);
  967. switch (ris) {
  968. case AVL_ERR:
  969. {
  970. G_fatal_error("avlID_add error");
  971. return RLI_ERRORE;
  972. }
  973. case AVL_ADD:
  974. {
  975. npatch++;
  976. break;
  977. }
  978. case AVL_PRES:
  979. {
  980. break;
  981. }
  982. default:
  983. {
  984. G_fatal_error("avlID_add unknown error");
  985. return RLI_ERRORE;
  986. }
  987. }
  988. }
  989. idCorr = mask_patch_sup[i];
  990. mask_patch_corr[i] = idCorr;
  991. totCorr = 1;
  992. }
  993. }
  994. else { /*current cell and previuos cell are equal */
  995. /* ?
  996. * 1 1
  997. */
  998. if (corrCell == supCell) { /*current cell and upper cell are equal */
  999. /* 1
  1000. * 1 1
  1001. */
  1002. if (mask_patch_sup[i] != mask_patch_corr[i - 1]) {
  1003. long r = 0;
  1004. long del = mask_patch_sup[i];
  1005. r = avlID_sub(&albero, del); /*r=number of cell of patch removed */
  1006. if (r == 0) {
  1007. G_fatal_error("avlID_sub error");
  1008. return RLI_ERRORE;
  1009. }
  1010. /*Remove one patch because it makes part of a patch already found */
  1011. ris = avlID_add(&albero, idCorr, r);
  1012. switch (ris) {
  1013. case AVL_ERR:
  1014. {
  1015. G_fatal_error("avlID_add error");
  1016. return RLI_ERRORE;
  1017. }
  1018. case AVL_ADD:
  1019. {
  1020. npatch++;
  1021. break;
  1022. }
  1023. case AVL_PRES:
  1024. {
  1025. break;
  1026. }
  1027. default:
  1028. {
  1029. G_fatal_error("avlID_add unknown error");
  1030. return RLI_ERRORE;
  1031. }
  1032. }
  1033. r = i;
  1034. while (r < ad->cl) {
  1035. if (mask_patch_sup[r] == del) {
  1036. mask_patch_sup[r] = idCorr;
  1037. }
  1038. r++;
  1039. }
  1040. mask_patch_corr[i] = idCorr;
  1041. }
  1042. else {
  1043. mask_patch_corr[i] = idCorr;
  1044. }
  1045. }
  1046. else { /*current cell and upper cell are not equal */
  1047. /* 2
  1048. * 1 1
  1049. */
  1050. mask_patch_corr[i] = idCorr;
  1051. }
  1052. totCorr++;
  1053. }
  1054. }
  1055. else { /*cell is null or is not to consider */
  1056. mask_patch_corr[i] = 0;
  1057. }
  1058. }
  1059. {
  1060. int ii;
  1061. long c;
  1062. for (ii = 0; ii < ad->cl; ii++) {
  1063. c = mask_patch_corr[ii];
  1064. mask_patch_sup[ii] = c;
  1065. mask_patch_corr[ii] = 0;
  1066. }
  1067. }
  1068. }
  1069. if (areaPatch != 0) {
  1070. if (albero == NULL) {
  1071. albero = avlID_make(idCorr, totCorr);
  1072. if (albero == NULL) {
  1073. G_fatal_error("avlID_make error");
  1074. return RLI_ERRORE;
  1075. }
  1076. npatch++;
  1077. }
  1078. else {
  1079. ris = avlID_add(&albero, idCorr, totCorr);
  1080. switch (ris) {
  1081. case AVL_ERR:
  1082. {
  1083. G_fatal_error("avlID_add error");
  1084. return RLI_ERRORE;
  1085. }
  1086. case AVL_ADD:
  1087. {
  1088. npatch++;
  1089. break;
  1090. }
  1091. case AVL_PRES:
  1092. {
  1093. break;
  1094. }
  1095. default:
  1096. {
  1097. G_fatal_error("avlID_add unknown error");
  1098. return RLI_ERRORE;
  1099. }
  1100. }
  1101. }
  1102. array = G_malloc(npatch * sizeof(avlID_tableRow));
  1103. if (array == NULL) {
  1104. G_fatal_error("malloc array failed");
  1105. return RLI_ERRORE;
  1106. }
  1107. tot = avlID_to_array(albero, zero, array);
  1108. if (tot != npatch) {
  1109. G_warning
  1110. ("avlID_to_array unaspected value. the result could be wrong");
  1111. return RLI_ERRORE;
  1112. }
  1113. for (i = 0; i < npatch; i++) {
  1114. if (array[i]->tot == 0) {
  1115. doppi++;
  1116. }
  1117. }
  1118. np = npatch;
  1119. npatch = npatch - doppi;
  1120. mn = areaPatch / npatch;
  1121. /* calculate summary */
  1122. for (i = 0; i < np; i++) {
  1123. long areaPi = 0;
  1124. double diff;
  1125. if (array[i]->tot != 0) {
  1126. ris = ris + array[i]->tot;
  1127. areaPi = (double)array[i]->tot;
  1128. diff = areaPi - mn;
  1129. somma = somma + (diff * diff);
  1130. }
  1131. }
  1132. sd = sqrt(somma / npatch);
  1133. cv = sd * 100 / mn;
  1134. indice = cv;
  1135. G_free(array);
  1136. }
  1137. else
  1138. indice = (double)(-1);
  1139. if (masked)
  1140. G_free(mask_buf);
  1141. G_free(mask_patch_sup);
  1142. *result = indice;
  1143. return RLI_OK;
  1144. }