/
read_sites.c
128 lines (103 loc) · 3.9 KB
/
read_sites.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <stdlib.h>
#include <grass/glocale.h>
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/vector.h>
#include "proto.h"
/* 1/2001 added field parameter MN
* update 12/99 to read multi-dim sites properly MN
* updated 28 June 1995 to use new sites API.
* Only uses floating point attributes.
* mccauley
*/
void read_sites(const char *name, const char *field_name, const char *col,
int noindex)
{
extern long npoints;
int nrec, ctype = 0, type, field, with_z;
struct Map_info Map;
struct field_info *Fi;
dbDriver *Driver;
dbCatValArray cvarr;
struct line_pnts *Points;
struct line_cats *Cats;
Vect_set_open_level(1); /* without topology */
if (Vect_open_old2(&Map, name, "", field_name) < 0)
G_fatal_error(_("Unable to open vector map <%s>"), name);
field = Vect_get_field_number(&Map, field_name);
with_z = col == NULL && Vect_is_3d(&Map); /* read z-coordinates
only when column is
not defined */
if (!col) {
if (!with_z)
G_important_message(_("Input vector map <%s> is 2D - using categories to interpolate"),
Vect_get_full_name(&Map));
else
G_important_message(_("Input vector map <%s> is 3D - using z-coordinates to interpolate"),
Vect_get_full_name(&Map));
}
if (col) {
db_CatValArray_init(&cvarr);
Fi = Vect_get_field(&Map, field);
if (Fi == NULL)
G_fatal_error(_("Database connection not defined for layer %s"),
field_name);
Driver = db_start_driver_open_database(Fi->driver, Fi->database);
if (Driver == NULL)
G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
Fi->database, Fi->driver);
nrec =
db_select_CatValArray(Driver, Fi->table, Fi->key, col, NULL,
&cvarr);
G_debug(3, "nrec = %d", nrec);
ctype = cvarr.ctype;
if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
G_fatal_error(_("Column type not supported"));
if (nrec < 0)
G_fatal_error(_("Unable to select data from table"));
G_verbose_message(n_
("One record selected from table",
"%d records selected from table", nrec), nrec);
db_close_database_shutdown_driver(Driver);
}
Points = Vect_new_line_struct();
Cats = Vect_new_cats_struct();
while ((type = Vect_read_next_line(&Map, Points, Cats)) >= 0) {
double dval;
if (!(type & GV_POINTS))
continue;
if (!with_z) {
int cat;
/* TODO: what to do with multiple cats */
Vect_cat_get(Cats, field, &cat);
if (cat < 0) /* skip features without category */
continue;
if (col) {
int ival, ret;
if (ctype == DB_C_TYPE_INT) {
ret = db_CatValArray_get_value_int(&cvarr, cat, &ival);
dval = ival;
}
else { /* DB_C_TYPE_DOUBLE */
ret = db_CatValArray_get_value_double(&cvarr, cat, &dval);
}
if (ret != DB_OK) {
G_warning(_("No record for point (cat = %d)"), cat);
continue;
}
}
else {
dval = cat;
}
}
else
dval = Points->z[0];
newpoint(dval, Points->x[0], Points->y[0], noindex);
}
if (col)
db_CatValArray_free(&cvarr);
Vect_set_release_support(&Map);
Vect_close(&Map);
G_message(n_("%ld point loaded", "%ld points loaded", npoints), npoints);
}