File: parse_dist.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 (94 lines) | stat: -rw-r--r-- 2,528 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/****************************************************************************
 *
 * MODULE:       r.buffer
 *
 * AUTHOR(S):    Michael Shapiro - CERL
 *
 * PURPOSE:      This program creates distance zones from non-zero
 *               cells in a grid layer. Distances are specified in
 *               meters (on the command-line). Window does not have to
 *               have square cells. Works both for planimetric
 *               (UTM, State Plane) and lat-long.
 *
 * COPYRIGHT:    (C) 2005 by the GRASS Development Team
 *
 *               This program is free software under the GNU General Public
 *               License (>=v2). Read the file COPYING that comes with GRASS
 *               for details.
 *
 ****************************************************************************/

#include <stdlib.h>
#include "distance.h"
#include <grass/glocale.h>

static int cmp(const void *, const void *);
static int scan_dist(char *, double *);

int parse_distances(char **zone_list, double to_meters)
{
    double dist;
    double ew2 = 0.0;
    int i;
    int count;

    for (count = 0; zone_list[count]; count++)
        ;
    if (count <= 0)
        return 0;

    /* create an array to hold the distances */

    distances = (struct Distance *)G_calloc(count, sizeof(struct Distance));

    /* scan the command line for the distances */

    if (window.proj != PROJECTION_LL) {
        ew2 = window.ew_res * window.ew_res;
        ns_to_ew_squared = window.ns_res * window.ns_res / ew2;
    }

    for (i = 0; i < count; i++) {
        if (!scan_dist(zone_list[i], &dist)) {
            G_warning(_("%s: %s - illegal distance specification"), pgm_name,
                      zone_list[i]);
            return 0;
        }
        else {
            dist *= (to_meters * meters_to_grid);
            if (window.proj != PROJECTION_LL)
                dist = dist * dist / ew2;
            distances[i].dist = dist;
        }
        distances[i].label = zone_list[i];
    }

    /* sort the distances in increasing order */

    qsort(distances, count, sizeof(struct Distance), cmp);

    return count;
}

static int cmp(const void *aa, const void *bb)
{
    const struct Distance *a = aa, *b = bb;

    if (a->dist < b->dist)
        return -1;
    return a->dist > b->dist;
}

static int scan_dist(char *s, double *dist)
{
    char dummy[2];

    *dummy = 0;
    if (sscanf(s, "%lf%1s", dist, dummy) != 1)
        return 0;
    if (*dummy)
        return 0;
    if (*dist <= 0.0)
        return 0;
    return 1;
}