~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to raster/r.li/r.li.simpson/simpson.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <math.h>
21
21
 
22
22
#include <grass/gis.h>
 
23
#include <grass/raster.h>
23
24
#include <grass/glocale.h>
24
25
 
25
26
#include "../r.li.daemon/daemon.h"
42
43
    module = G_define_module();
43
44
    module->description =
44
45
        _("Calculates Simpson's diversity index on a raster map");
45
 
    module->keywords =
46
 
        _("raster, landscape structure analysis, diversity index");
 
46
    G_add_keyword(_("raster"));
 
47
    G_add_keyword(_("landscape structure analysis"));
 
48
    G_add_keyword(_("diversity index"));
47
49
 
48
50
    /* define options */
49
51
 
50
 
    raster = G_define_standard_option(G_OPT_R_MAP);
 
52
    raster = G_define_standard_option(G_OPT_R_INPUT);
51
53
 
52
54
    conf = G_define_standard_option(G_OPT_F_INPUT);
53
 
    conf->key = "conf";
 
55
    conf->key = "config";
54
56
    conf->description = _("Configuration file");
55
57
    conf->required = YES;
56
58
 
121
123
    long area = 0;
122
124
 
123
125
    avl_tree albero = NULL;
124
 
    AVL_table *array;
 
126
    AVL_table array;
125
127
    generic_cell uc;
126
128
 
127
129
    uc.t = CELL_TYPE;
138
140
        masked = TRUE;
139
141
    }
140
142
 
141
 
    G_set_c_null_value(&precCell, 1);
 
143
    Rast_set_c_null_value(&precCell, 1);
142
144
    for (j = 0; j < ad->rl; j++) {      /* for each row */
143
145
        if (masked) {
144
146
            if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
153
155
            corrCell = buf[i + ad->x];
154
156
 
155
157
            if ((masked) && (mask_buf[i] == 0)) {
156
 
                G_set_c_null_value(&corrCell, 1);
 
158
                Rast_set_c_null_value(&corrCell, 1);
157
159
            }
158
160
 
159
 
            if (!(G_is_null_value(&corrCell, uc.t))) {
 
161
            if (!(Rast_is_null_value(&corrCell, uc.t))) {
160
162
                /* total patch area */
161
163
                area++;
162
164
            }
163
165
 
164
 
            if (!(G_is_null_value(&precCell, uc.t)) &&
 
166
            if (!(Rast_is_null_value(&precCell, uc.t)) &&
165
167
                corrCell == precCell) {
166
168
 
167
169
                totCorr++;
168
170
            }
169
 
            else if (!(G_is_null_value(&precCell, uc.t)) &&
 
171
            else if (!(Rast_is_null_value(&precCell, uc.t)) &&
170
172
                     corrCell != precCell) {
171
173
                
172
174
                /* add precCell to search tree */
211
213
    }
212
214
 
213
215
    /* last closing */
214
 
    if (area > 0 && !(G_is_null_value(&precCell, uc.t))) {
 
216
    if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
215
217
        if (albero == NULL) {
216
218
            uc.val.c = precCell;
217
219
            albero = avl_make(uc, totCorr);
267
269
        /* calculate simpson */
268
270
        simpson = 0;
269
271
        for (i = 0; i < m; i++) {
270
 
            t = (double)(array[i]->tot);
 
272
            t = (double)(array[i].tot);
271
273
            p = t / area;
272
274
            simpson += (p * p);
273
275
        }
276
278
        *result = 1 - simpson;
277
279
    }
278
280
    else
279
 
        G_set_d_null_value(result, 1);
 
281
        Rast_set_d_null_value(result, 1);
280
282
 
281
283
    avl_destroy(albero);
282
284
    if (masked) {
307
309
    long area = 0;
308
310
 
309
311
    avl_tree albero = NULL;
310
 
    AVL_table *array;
 
312
    AVL_table array;
311
313
    generic_cell uc;
312
314
 
313
315
    uc.t = DCELL_TYPE;
324
326
        masked = TRUE;
325
327
    }
326
328
 
327
 
    G_set_d_null_value(&precCell, 1);
 
329
    Rast_set_d_null_value(&precCell, 1);
328
330
    for (j = 0; j < ad->rl; j++) {      /* for each row */
329
331
        if (masked) {
330
332
            if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
339
341
            corrCell = buf[i + ad->x];
340
342
 
341
343
            if ((masked) && (mask_buf[i] == 0)) {
342
 
                G_set_d_null_value(&corrCell, 1);
 
344
                Rast_set_d_null_value(&corrCell, 1);
343
345
            }
344
346
 
345
 
            if (!(G_is_null_value(&corrCell, uc.t))) {
 
347
            if (!(Rast_is_null_value(&corrCell, uc.t))) {
346
348
                /* total patch area */
347
349
                area++;
348
350
            }
349
351
 
350
 
            if (!(G_is_null_value(&precCell, uc.t)) &&
 
352
            if (!(Rast_is_null_value(&precCell, uc.t)) &&
351
353
                corrCell == precCell) {
352
354
 
353
355
                totCorr++;
354
356
            }
355
 
            else if (!(G_is_null_value(&precCell, uc.t)) &&
 
357
            else if (!(Rast_is_null_value(&precCell, uc.t)) &&
356
358
                     corrCell != precCell) {
357
359
                
358
360
                /* add precCell to search tree */
397
399
    }
398
400
 
399
401
    /* last closing */
400
 
    if (area > 0 && !(G_is_null_value(&precCell, uc.t))) {
 
402
    if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
401
403
        if (albero == NULL) {
402
404
            uc.val.dc = precCell;
403
405
            albero = avl_make(uc, totCorr);
453
455
        /* calculate simpson */
454
456
        simpson = 0;
455
457
        for (i = 0; i < m; i++) {
456
 
            t = (double)(array[i]->tot);
 
458
            t = (double)(array[i].tot);
457
459
            p = t / area;
458
460
            simpson += (p * p);
459
461
        }
462
464
        *result = 1 - simpson;
463
465
    }
464
466
    else
465
 
        G_set_d_null_value(result, 1);
 
467
        Rast_set_d_null_value(result, 1);
466
468
 
467
469
    avl_destroy(albero);
468
470
    if (masked) {
493
495
    long area = 0;
494
496
 
495
497
    avl_tree albero = NULL;
496
 
    AVL_table *array;
 
498
    AVL_table array;
497
499
    generic_cell uc;
498
500
 
499
501
    uc.t = FCELL_TYPE;
510
512
        masked = TRUE;
511
513
    }
512
514
 
513
 
    G_set_f_null_value(&precCell, 1);
 
515
    Rast_set_f_null_value(&precCell, 1);
514
516
    for (j = 0; j < ad->rl; j++) {      /* for each row */
515
517
        if (masked) {
516
518
            if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
525
527
            corrCell = buf[i + ad->x];
526
528
 
527
529
            if ((masked) && (mask_buf[i] == 0)) {
528
 
                G_set_f_null_value(&corrCell, 1);
 
530
                Rast_set_f_null_value(&corrCell, 1);
529
531
            }
530
532
 
531
 
            if (!(G_is_null_value(&corrCell, uc.t))) {
 
533
            if (!(Rast_is_null_value(&corrCell, uc.t))) {
532
534
                /* total patch area */
533
535
                area++;
534
536
            }
535
537
 
536
 
            if (!(G_is_null_value(&precCell, uc.t)) &&
 
538
            if (!(Rast_is_null_value(&precCell, uc.t)) &&
537
539
                corrCell == precCell) {
538
540
 
539
541
                totCorr++;
540
542
            }
541
 
            else if (!(G_is_null_value(&precCell, uc.t)) &&
 
543
            else if (!(Rast_is_null_value(&precCell, uc.t)) &&
542
544
                     corrCell != precCell) {
543
545
                
544
546
                /* add precCell to search tree */
583
585
    }
584
586
 
585
587
    /* last closing */
586
 
    if (area > 0 && !(G_is_null_value(&precCell, uc.t))) {
 
588
    if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
587
589
        if (albero == NULL) {
588
590
            uc.val.fc = precCell;
589
591
            albero = avl_make(uc, totCorr);
639
641
        /* calculate simpson */
640
642
        simpson = 0;
641
643
        for (i = 0; i < m; i++) {
642
 
            t = (double)(array[i]->tot);
 
644
            t = (double)(array[i].tot);
643
645
            p = t / area;
644
646
            simpson += (p * p);
645
647
        }
648
650
        *result = 1 - simpson;
649
651
    }
650
652
    else
651
 
        G_set_d_null_value(result, 1);
 
653
        Rast_set_d_null_value(result, 1);
652
654
 
653
655
    avl_destroy(albero);
654
656
    if (masked) {