File: close.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (66 lines) | stat: -rw-r--r-- 1,717 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <string.h>
#include "global.h"
/*!
   \brief Close lines (boundaries)

   Using threshold distance (-1 for no limit)

   \param[in] Map vector map
   \param[in] ltype vector feature type (line | boundary)
   \param[in] thresh threshold distance

   \return number of modified features
 */
int close_lines(struct Map_info *Map, int ltype, double thresh)
{
    int nlines, line, type, nlines_modified, newline;
    int npoints;
    double *x, *y, *z;
    double dist;

    struct line_pnts *Points;
    struct line_cats *Cats;

    Points = Vect_new_line_struct();
    Cats = Vect_new_cats_struct();

    nlines_modified = 0;

    Vect_build_partial(Map, GV_BUILD_BASE);
    nlines = Vect_get_num_lines(Map);

    for (line = 1; line <= nlines; line++) {
        if (!Vect_line_alive(Map, line))
            continue;

        type = Vect_read_line(Map, Points, Cats, line);

        if (!(type & ltype))
            continue;

        npoints = Points->n_points - 1;
        x = Points->x;
        y = Points->y;
        z = Points->z;

        dist = Vect_points_distance(x[npoints], y[npoints], z[npoints], x[0],
                                    y[0], z[0], WITHOUT_Z);

        if (dist > 0 && (thresh < 0.0 || dist <= thresh)) {
            Vect_line_delete_point(Points, npoints);
            Vect_append_point(Points, x[0], y[0], z[0]);

            newline = Vect_rewrite_line(Map, line, type, Points, Cats);
            if (newline < 0) {
                G_warning(_("Unable to rewrite line %d"), line);
                return -1;
            }
            nlines_modified++;
        }
    }

    Vect_destroy_line_struct(Points);
    Vect_destroy_cats_struct(Cats);

    return nlines_modified;
}