1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <grass/glocale.h>
4 #include <grass/gis.h>
5 #include <grass/dbmi.h>
6 #include <grass/vector.h>
7 #include "proto.h"
8 
9 /* 1/2001 added field parameter MN
10  * update 12/99 to read multi-dim sites properly MN
11  * updated 28 June 1995 to use new sites API.
12  * Only uses floating point attributes.
13  * mccauley
14  */
15 
read_sites(const char * name,const char * field_name,const char * col,int noindex)16 void read_sites(const char *name, const char *field_name, const char *col, int noindex)
17 {
18     extern long npoints;
19     int nrec, ctype = 0, type, field, with_z;
20     struct Map_info Map;
21     struct field_info *Fi;
22     dbDriver *Driver;
23     dbCatValArray cvarr;
24     struct line_pnts *Points;
25     struct line_cats *Cats;
26 
27     Vect_set_open_level(1);	/* without topology */
28     if (Vect_open_old2(&Map, name, "", field_name) < 0)
29 	G_fatal_error(_("Unable to open vector map <%s>"), name);
30 
31     field = Vect_get_field_number(&Map, field_name);
32     with_z = col == NULL && Vect_is_3d(&Map); /* read z-coordinates
33                                                  only when column is
34                                                  not defined */
35     if (!col) {
36         if (!with_z)
37             G_important_message(_("Input vector map <%s> is 2D - using categories to interpolate"),
38                                 Vect_get_full_name(&Map));
39         else
40             G_important_message(_("Input vector map <%s> is 3D - using z-coordinates to interpolate"),
41                                 Vect_get_full_name(&Map));
42 
43     }
44 
45     if (col) {
46 	db_CatValArray_init(&cvarr);
47 
48 	Fi = Vect_get_field(&Map, field);
49 	if (Fi == NULL)
50 	    G_fatal_error(_("Database connection not defined for layer %s"),
51 			  field_name);
52 
53 	Driver = db_start_driver_open_database(Fi->driver, Fi->database);
54 	if (Driver == NULL)
55 	    G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
56 			  Fi->database, Fi->driver);
57 
58 	nrec =
59 	    db_select_CatValArray(Driver, Fi->table, Fi->key, col, NULL,
60 				  &cvarr);
61 	G_debug(3, "nrec = %d", nrec);
62 
63 	ctype = cvarr.ctype;
64 	if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
65 	    G_fatal_error(_("Column type not supported"));
66 
67 	if (nrec < 0)
68 	    G_fatal_error(_("Unable to select data from table"));
69 
70 	G_verbose_message(n_("One record selected from table", "%d records selected from table", nrec), nrec);
71 
72 	db_close_database_shutdown_driver(Driver);
73     }
74 
75 
76     Points = Vect_new_line_struct();
77     Cats = Vect_new_cats_struct();
78 
79     while ((type = Vect_read_next_line(&Map, Points, Cats)) >= 0) {
80 	double dval;
81 
82 	if (!(type & GV_POINTS))
83 	    continue;
84 
85 	if (!with_z) {
86 	    int cat;
87 
88 	    /* TODO: what to do with multiple cats */
89 	    Vect_cat_get(Cats, field, &cat);
90 	    if (cat < 0) /* skip features without category */
91 		continue;
92 
93 	    if (col) {
94 		int ival, ret;
95 		if (ctype == DB_C_TYPE_INT) {
96 		    ret = db_CatValArray_get_value_int(&cvarr, cat, &ival);
97 		    dval = ival;
98 		}
99 		else {		/* DB_C_TYPE_DOUBLE */
100 		    ret = db_CatValArray_get_value_double(&cvarr, cat, &dval);
101 		}
102 		if (ret != DB_OK) {
103 		    G_warning(_("No record for point (cat = %d)"), cat);
104 		    continue;
105 		}
106 	    }
107 	    else {
108 		dval = cat;
109 	    }
110 	}
111 	else
112 	    dval = Points->z[0];
113 
114 	newpoint(dval, Points->x[0], Points->y[0], noindex);
115     }
116 
117     if (col)
118 	db_CatValArray_free(&cvarr);
119 
120     Vect_set_release_support(&Map);
121     Vect_close(&Map);
122 
123     G_message(n_("%ld point loaded", "%ld points loaded", npoints), npoints);
124 }
125