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

« back to all changes in this revision

Viewing changes to imagery/i.points/analyze.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:
1
 
#include <string.h>
2
 
#include <unistd.h>
3
 
#include <math.h>
4
 
#include <stdlib.h>
5
 
#include <grass/gis.h>
6
 
#include <grass/raster.h>
7
 
#include "globals.h"
8
 
#include "local_proto.h"
9
 
 
10
 
#define NLINES 18
11
 
 
12
 
struct box
13
 
{
14
 
    int top, bottom, left, right;
15
 
};
16
 
 
17
 
static int uparrow(struct box *, int);
18
 
static int downarrow(struct box *, int);
19
 
static int pick(int, int);
20
 
static int done(void);
21
 
static int cancel_which(void);
22
 
static int inbox(struct box *, int, int);
23
 
static int dotext(char *, int, int, int, int, int, int);
24
 
static int compute_transformation(void);
25
 
static int to_file(void);
26
 
static int askfile(void);
27
 
static int to_printer(void);
28
 
static int do_report(FILE *);
29
 
static int printcentered(FILE *, char *, int);
30
 
static int show_point(int, int);
31
 
 
32
 
static int which;
33
 
static struct box more, less, report;
34
 
static int height, size, edge, nlines;
35
 
static int curp, first_point;
36
 
static double rms;
37
 
static double *xres, *yres, *gnd;
38
 
static int pager;
39
 
static int xmax, ymax, gmax;
40
 
static char buf[300];
41
 
 
42
 
#define FMT0(buf,n) \
43
 
        sprintf (buf, "%3d ", n)
44
 
#define FMT1(buf,xres,yres,gnd) \
45
 
        sprintf (buf, "%5.1f %5.1f %6.1f ", xres,yres,gnd)
46
 
#define LHEAD1 "        error          "
47
 
#define LHEAD2 "  #   col   row  target"
48
 
 
49
 
#define FMT2(buf,e1,n1,e2,n2) \
50
 
        sprintf (buf, "%9.1f %9.1f %9.1f %9.1f ", e1,n1,e2,n2)
51
 
#define RHEAD1 "         image              target"
52
 
#define RHEAD2 "    east     north      east     north"
53
 
 
54
 
#define BACKGROUND GREY
55
 
 
56
 
int analyze(void)
57
 
{
58
 
    static int use = 1;
59
 
    static Objects objects[] = {
60
 
        MENU("DONE", done, &use),
61
 
        MENU("PRINT", to_printer, &use),
62
 
        MENU("FILE", to_file, &use),
63
 
        INFO(" Double click on point to be included/excluded ", &use),
64
 
        OTHER(pick, &use),
65
 
        {0}
66
 
    };
67
 
 
68
 
    int color;
69
 
    int tsize;
70
 
    int cury;
71
 
    int len;
72
 
    int line;
73
 
    int top, bottom, left, right, width, middle, nums;
74
 
 
75
 
    /* to give user a response of some sort */
76
 
    Menu_msg("Preparing analysis ...");
77
 
 
78
 
    /*
79
 
     * build a popup window at center of the screen.
80
 
     * 35% the height and wide enough to hold the report
81
 
     *
82
 
     */
83
 
 
84
 
    /* height of 1 line, based on NLINES taking up 35% vertical space */
85
 
    height = (.35 * (SCREEN_BOTTOM - SCREEN_TOP)) / NLINES + 1;
86
 
 
87
 
    /* size of text, 80% of line height */
88
 
    tsize = .8 * height;
89
 
    size = tsize - 2;           /* fudge for computing pixels width of text */
90
 
 
91
 
    /* indent for the text */
92
 
    edge = .1 * height + 1;
93
 
 
94
 
    /* determine the length, in chars, of printed line */
95
 
    FMT0(buf, 0);
96
 
    nums = strlen(buf) * size;
97
 
    FMT1(buf, 0.0, 0.0, 0.0);
98
 
    len = strlen(buf);
99
 
    middle = len * size;
100
 
    FMT2(buf, 0.0, 0.0, 0.0, 0.0);
101
 
    len += strlen(buf);
102
 
 
103
 
    /* width is for max chars plus sidecar for more/less */
104
 
    width = len * size + nums + 2 * height;
105
 
    if ((SCREEN_RIGHT - SCREEN_LEFT) < width)
106
 
        width = SCREEN_RIGHT - SCREEN_LEFT;
107
 
 
108
 
 
109
 
    /* define the window */
110
 
    bottom = VIEW_MENU->top - 1;
111
 
    top = bottom - height * NLINES;
112
 
 
113
 
 
114
 
    left = SCREEN_LEFT;
115
 
    right = left + width;
116
 
    middle += left + nums;
117
 
    nums += left;
118
 
 
119
 
    /* save what is under this area, so it can be restored */
120
 
    R_panel_save(tempfile1, top, bottom + 1, left, right + 1);
121
 
 
122
 
 
123
 
    /* fill it with white */
124
 
    R_standard_color(BACKGROUND);
125
 
    R_box_abs(left, top, right, bottom);
126
 
 
127
 
    right -= 2 * height;        /* reduce it to exclude sidecar */
128
 
 
129
 
    /* print messages in message area */
130
 
    R_text_size(tsize, tsize);
131
 
 
132
 
 
133
 
    /* setup the more/less boxes in the sidecar */
134
 
    R_standard_color(BLACK);
135
 
    less.top = top;
136
 
    less.bottom = top + 2 * height;
137
 
    less.left = right;
138
 
    less.right = right + 2 * height;
139
 
    Outline_box(less.top, less.bottom, less.left, less.right);
140
 
 
141
 
    more.top = bottom - 2 * height;
142
 
    more.bottom = bottom;
143
 
    more.left = right;
144
 
    more.right = right + 2 * height;
145
 
    Outline_box(more.top, more.bottom, more.left, more.right);
146
 
 
147
 
    /*
148
 
     * top two lines are for column labels
149
 
     * last two line is for overall rms error.
150
 
     */
151
 
    nlines = NLINES - 3;
152
 
    first_point = 0;
153
 
 
154
 
    /* allocate predicted values */
155
 
    xres = (double *)G_calloc(group.points.count, sizeof(double));
156
 
    yres = (double *)G_calloc(group.points.count, sizeof(double));
157
 
    gnd = (double *)G_calloc(group.points.count, sizeof(double));
158
 
 
159
 
    /* compute transformation for the first time */
160
 
    compute_transformation();
161
 
 
162
 
 
163
 
    /* put head on the report */
164
 
    cury = top;
165
 
    dotext(LHEAD1, cury, cury + height, left, middle, 0, BLACK);
166
 
    dotext(RHEAD1, cury, cury + height, middle, right - 1, 0, BLACK);
167
 
    cury += height;
168
 
    dotext(LHEAD2, cury, cury + height, left, middle, 0, BLACK);
169
 
    dotext(RHEAD2, cury, cury + height, middle, right - 1, 0, BLACK);
170
 
    cury += height;
171
 
    R_move_abs(left, cury - 1);
172
 
    R_cont_abs(right, cury - 1);
173
 
 
174
 
    /* isolate the sidecar */
175
 
    R_move_abs(right, top);
176
 
    R_cont_abs(right, bottom);
177
 
 
178
 
    /* define report box */
179
 
    report.top = cury;
180
 
    report.left = left;
181
 
    report.right = right;
182
 
 
183
 
    /* lets do it */
184
 
    pager = 1;
185
 
    while (1) {
186
 
        R_text_size(tsize, tsize);
187
 
        line = 0;
188
 
        curp = first_point;
189
 
        cury = top + 2 * height;
190
 
        while (1) {
191
 
            if (line >= nlines || curp >= group.points.count)
192
 
                break;
193
 
            line++;
194
 
            if (group.equation_stat > 0 && group.points.status[curp] > 0) {
195
 
                color = BLACK;
196
 
                FMT1(buf, xres[curp], yres[curp], gnd[curp]);
197
 
                if (curp == xmax || curp == ymax || curp == gmax)
198
 
                    color = RED;
199
 
                dotext(buf, cury, cury + height, nums, middle, 0, color);
200
 
            }
201
 
            else if (group.points.status[curp] > 0)
202
 
                dotext("?", cury, cury + height, nums, middle, 1, BLACK);
203
 
            else
204
 
                dotext("not used", cury, cury + height, nums, middle, 1,
205
 
                       BLACK);
206
 
            if (pager) {
207
 
                FMT0(buf, curp + 1);
208
 
                dotext(buf, cury, cury + height, left, nums, 0, BLACK);
209
 
                FMT2(buf,
210
 
                     group.points.e1[curp],
211
 
                     group.points.n1[curp],
212
 
                     group.points.e2[curp], group.points.n2[curp]);
213
 
                dotext(buf, cury, cury + height, middle, right - 1, 0, BLACK);
214
 
            }
215
 
            cury += height;
216
 
            curp++;
217
 
        }
218
 
        report.bottom = cury;
219
 
        downarrow(&more, curp < group.points.count ? BLACK : BACKGROUND);
220
 
        uparrow(&less, first_point > 0 ? BLACK : BACKGROUND);
221
 
        R_standard_color(BACKGROUND);
222
 
        R_box_abs(left, cury, right - 1, bottom);
223
 
        if (group.equation_stat < 0) {
224
 
            color = RED;
225
 
            strcpy(buf, "Poorly placed control points");
226
 
        }
227
 
        else if (group.equation_stat == 0) {
228
 
            color = RED;
229
 
            strcpy(buf, "No active control points");
230
 
        }
231
 
        else {
232
 
            color = BLACK;
233
 
            sprintf(buf, "Overall rms error: %.2f", rms);
234
 
        }
235
 
        dotext(buf, bottom - height, bottom, left, right - 1, 0, color);
236
 
        R_standard_color(BLACK);
237
 
        R_move_abs(left, bottom - height);
238
 
        R_cont_abs(right - 1, bottom - height);
239
 
 
240
 
        pager = 0;
241
 
        which = -1;
242
 
        if (Input_pointer(objects) < 0)
243
 
            break;
244
 
    }
245
 
 
246
 
    /* all done. restore what was under the window */
247
 
    right += 2 * height;        /* move it back over the sidecar */
248
 
    R_standard_color(BACKGROUND);
249
 
    R_box_abs(left, top, right, bottom);
250
 
    R_panel_restore(tempfile1);
251
 
    R_panel_delete(tempfile1);
252
 
    R_flush();
253
 
 
254
 
    G_free(xres);
255
 
    G_free(yres);
256
 
    G_free(gnd);
257
 
    I_put_control_points(group.name, &group.points);
258
 
    display_points(1);
259
 
    return 0;                   /* return but don't QUIT */
260
 
}
261
 
 
262
 
 
263
 
static int uparrow(struct box *box, int color)
264
 
{
265
 
    R_standard_color(color);
266
 
    Uparrow(box->top + edge, box->bottom - edge, box->left + edge,
267
 
            box->right - edge);
268
 
 
269
 
    return 0;
270
 
}
271
 
 
272
 
static int downarrow(struct box *box, int color)
273
 
{
274
 
    R_standard_color(color);
275
 
    Downarrow(box->top + edge, box->bottom - edge, box->left + edge,
276
 
              box->right - edge);
277
 
 
278
 
    return 0;
279
 
}
280
 
 
281
 
static int pick(int x, int y)
282
 
{
283
 
    int n;
284
 
    int cur;
285
 
 
286
 
    cur = which;
287
 
    cancel_which();
288
 
    if (inbox(&more, x, y)) {
289
 
        if (curp >= group.points.count)
290
 
            return 0;
291
 
        first_point = curp;
292
 
        pager = 1;
293
 
        return 1;
294
 
    }
295
 
    if (inbox(&less, x, y)) {
296
 
        if (first_point == 0)
297
 
            return 0;
298
 
        first_point -= nlines;
299
 
        if (first_point < 0)
300
 
            first_point = 0;
301
 
        pager = 1;
302
 
        return 1;
303
 
    }
304
 
    if (!inbox(&report, x, y)) {
305
 
        return 0;
306
 
    }
307
 
 
308
 
    n = (y - report.top) / height;
309
 
    if (n == cur) {             /* second click! */
310
 
        group.points.status[first_point + n] =
311
 
            !group.points.status[first_point + n];
312
 
        compute_transformation();
313
 
        show_point(first_point + n, 1);
314
 
        return 1;
315
 
    }
316
 
 
317
 
    /* first click */
318
 
    which = n;
319
 
    show_point(first_point + n, 0);
320
 
    R_standard_color(RED);
321
 
    Outline_box((report.top + n * height) + 1, report.top + (n + 1) * height,
322
 
                report.left, report.right - 1);
323
 
    R_flush();
324
 
 
325
 
    return 0;                   /* ignore first click */
326
 
 
327
 
}
328
 
 
329
 
static int done(void)
330
 
{
331
 
    cancel_which();
332
 
    return -1;
333
 
}
334
 
 
335
 
static int cancel_which(void)
336
 
{
337
 
    if (which >= 0) {
338
 
        R_standard_color(BACKGROUND);
339
 
        Outline_box((report.top + which * height) + 1,
340
 
                    report.top + (which + 1) * height, report.left,
341
 
                    report.right - 1);
342
 
        show_point(first_point + which, 1);
343
 
    }
344
 
    which = -1;
345
 
 
346
 
    return 0;
347
 
}
348
 
 
349
 
static int inbox(struct box *box, int x, int y)
350
 
{
351
 
    return (x > box->left && x < box->right && y > box->top &&
352
 
            y < box->bottom);
353
 
}
354
 
 
355
 
static int dotext(char *text,
356
 
                  int top, int bottom, int left, int right, int centered,
357
 
                  int color)
358
 
{
359
 
    R_standard_color(BACKGROUND);
360
 
    R_box_abs(left, top, right, bottom);
361
 
    R_standard_color(color);
362
 
    R_move_abs(left + 1 + edge, bottom - 1 - edge);
363
 
    if (centered)
364
 
        R_move_rel((right - left - strlen(text) * size) / 2, 0);
365
 
    R_set_window(top, bottom, left, right);     /* for text clipping */
366
 
    R_text(text);
367
 
    R_set_window(SCREEN_TOP, SCREEN_BOTTOM, SCREEN_LEFT, SCREEN_RIGHT);
368
 
 
369
 
    return 0;
370
 
}
371
 
 
372
 
static int compute_transformation(void)
373
 
{
374
 
    int n, count;
375
 
    double d, d1, d2, sum;
376
 
    double e1, e2, n1, n2;
377
 
    double xval, yval, gval;
378
 
 
379
 
    xmax = ymax = gmax = 0;
380
 
    xval = yval = gval = 0.0;
381
 
 
382
 
    Compute_equation();
383
 
    if (group.equation_stat <= 0)
384
 
        return 1;
385
 
 
386
 
    /* compute the row,col error plus ground error 
387
 
     * keep track of largest and second largest error
388
 
     */
389
 
    sum = 0.0;
390
 
    rms = 0.0;
391
 
    count = 0;
392
 
    for (n = 0; n < group.points.count; n++) {
393
 
        if (group.points.status[n] <= 0)
394
 
            continue;
395
 
        count++;
396
 
        I_georef(group.points.e2[n], group.points.n2[n], &e1, &n1, group.E21,
397
 
                 group.N21);
398
 
        I_georef(group.points.e1[n], group.points.n1[n], &e2, &n2, group.E12,
399
 
                 group.N12);
400
 
 
401
 
        if ((d = xres[n] = e1 - group.points.e1[n]) < 0)
402
 
            d = -d;
403
 
        if (d > xval) {
404
 
            xmax = n;
405
 
            xval = d;
406
 
        }
407
 
 
408
 
        if ((d = yres[n] = n1 - group.points.n1[n]) < 0)
409
 
            d = -d;
410
 
        if (d > yval) {
411
 
            ymax = n;
412
 
            yval = d;
413
 
        }
414
 
 
415
 
        /* compute ground error (ie along diagonal) */
416
 
        d1 = e2 - group.points.e2[n];
417
 
        d2 = n2 - group.points.n2[n];
418
 
        d = d1 * d1 + d2 * d2;
419
 
        sum += d;               /* add it to rms sum, before taking sqrt */
420
 
        d = sqrt(d);
421
 
        gnd[n] = d;
422
 
        if (d > gval) {         /* is this one the max? */
423
 
            gmax = n;
424
 
            gval = d;
425
 
        }
426
 
    }
427
 
 
428
 
    /* compute overall rms error */
429
 
    if (count)
430
 
        rms = sqrt(sum / count);
431
 
 
432
 
    return 0;
433
 
}
434
 
 
435
 
static int to_file(void)
436
 
{
437
 
    FILE *fd;
438
 
    char msg[1024];
439
 
 
440
 
    cancel_which();
441
 
    if (Input_other(askfile, "Keyboard") < 0) {
442
 
        return 0;
443
 
    }
444
 
 
445
 
    fd = fopen(buf, "w");
446
 
    if (fd == NULL) {
447
 
        sprintf(msg, "** Unable to create file %s\n", buf);
448
 
        Beep();
449
 
        Curses_write_window(PROMPT_WINDOW, 2, 1, msg);
450
 
    }
451
 
    else {
452
 
        do_report(fd);
453
 
        fclose(fd);
454
 
        sprintf(msg, "Report saved in file %s\n", buf);
455
 
        Curses_write_window(PROMPT_WINDOW, 2, 1, msg);
456
 
    }
457
 
    return 0;
458
 
}
459
 
 
460
 
static int askfile(void)
461
 
{
462
 
    char file[GNAME_MAX];
463
 
 
464
 
    while (1) {
465
 
        Curses_prompt_gets("Enter file to hold report: ", file);
466
 
        G_strip(file);
467
 
        if (*file == 0)
468
 
            return -1;
469
 
        if (G_index(file, '/'))
470
 
            strcpy(buf, file);
471
 
        else
472
 
            sprintf(buf, "%s/%s", G_home(), file);
473
 
        if (access(buf, 0) != 0)
474
 
            return 1;
475
 
        sprintf(buf, "** %s already exists. choose another file", file);
476
 
        Beep();
477
 
        Curses_write_window(PROMPT_WINDOW, 2, 1, buf);
478
 
    }
479
 
 
480
 
    return 0;
481
 
}
482
 
 
483
 
static int to_printer(void)
484
 
{
485
 
    FILE *fd;
486
 
 
487
 
    cancel_which();
488
 
    Menu_msg("sending report to printer ...");
489
 
 
490
 
    fd = popen("lpr", "w");
491
 
    do_report(fd);
492
 
    pclose(fd);
493
 
    return 0;
494
 
}
495
 
 
496
 
static int do_report(FILE * fd)
497
 
{
498
 
    char buf[GNAME_MAX];
499
 
    int n;
500
 
    int width;
501
 
 
502
 
    fprintf(fd, "LOCATION: %-20s GROUP: %-20s MAPSET: %s\n\n",
503
 
            G_location(), group.name, G_mapset());
504
 
    fprintf(fd, "%15sAnalysis of control point registration\n\n", "");
505
 
    fprintf(fd, "%s   %s\n", LHEAD1, RHEAD1);
506
 
    fprintf(fd, "%s   %s\n", LHEAD2, RHEAD2);
507
 
 
508
 
    FMT1(buf, 0.0, 0.0, 0.0);
509
 
    width = strlen(buf);
510
 
 
511
 
    for (n = 0; n < group.points.count; n++) {
512
 
        FMT0(buf, n + 1);
513
 
        fprintf(fd, "%s", buf);
514
 
        if (group.equation_stat > 0 && group.points.status[n] > 0) {
515
 
            FMT1(buf, xres[n], yres[n], gnd[n]);
516
 
            fprintf(fd, "%s", buf);
517
 
        }
518
 
        else if (group.points.status[n] > 0)
519
 
            printcentered(fd, "?", width);
520
 
        else
521
 
            printcentered(fd, "not used", width);
522
 
        FMT2(buf,
523
 
             group.points.e1[n],
524
 
             group.points.n1[n], group.points.e2[n], group.points.n2[n]);
525
 
        fprintf(fd, "   %s\n", buf);
526
 
    }
527
 
    fprintf(fd, "\n");
528
 
    if (group.equation_stat < 0)
529
 
        fprintf(fd, "Poorly place control points\n");
530
 
    else if (group.equation_stat == 0)
531
 
        fprintf(fd, "No active control points\n");
532
 
    else
533
 
        fprintf(fd, "Overall rms error: %.2f\n", rms);
534
 
 
535
 
    return 0;
536
 
}
537
 
 
538
 
static int printcentered(FILE * fd, char *buf, int width)
539
 
{
540
 
    int len;
541
 
    int n;
542
 
    int i;
543
 
 
544
 
    len = strlen(buf);
545
 
    n = (width - len) / 2;
546
 
 
547
 
    for (i = 0; i < n; i++)
548
 
        fprintf(fd, " ");
549
 
    fprintf(fd, "%s", buf);
550
 
    i += len;
551
 
    while (i++ < width)
552
 
        fprintf(fd, " ");
553
 
 
554
 
    return 0;
555
 
}
556
 
 
557
 
static int show_point(int n, int true_color)
558
 
{
559
 
    if (!true_color)
560
 
        R_standard_color(ORANGE);
561
 
    else if (group.points.status[n])
562
 
        R_standard_color(GREEN);
563
 
    else
564
 
        R_standard_color(RED);
565
 
    display_one_point(VIEW_MAP1, group.points.e1[n], group.points.n1[n]);
566
 
 
567
 
    return 0;
568
 
}