File: Gp3.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (218 lines) | stat: -rw-r--r-- 4,649 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*!
   \file Gp3.c

   \brief OGSF library - loading point sets (lower level functions)

   GRASS OpenGL gsurf OGSF Library 

   (C) 1999-2008 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.

   \author Bill Brown USACERL, GMSL/University of Illinois (January 1994)
   \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
 */

#include <stdlib.h>

#include <grass/gis.h>
#include <grass/site.h>
#include <grass/Vect.h>
#include <grass/glocale.h>
#include <grass/gstypes.h>

/*!
   \brief Set color for point set

   used when site attribute mode is ST_ATT_COLOR

   Gets color structure for grass file, goes through points and
   uses fattr as CAT, putting rgb color in iattr.

   \param grassname raster map name
   \param gp pointer to geopoint struct

   \return 1 on success
   \return 0 on failure
 */
int Gp_set_color(const char *grassname, geopoint * gp)
{
    const char *col_map;
    struct Colors sc;
    CELL cat;
    geopoint *tp;
    int r, g, b, color;

    /* TODO: handle error messages */

    if (grassname) {
	col_map = G_find_cell2(grassname, "");
	if (!col_map) {
	    G_warning(_("Raster map <%s> not found"), grassname);
	    return 0;
	}

	G_read_colors(grassname, col_map, &sc);

	for (tp = gp; tp; tp = tp->next) {
	    cat = (int)tp->fattr;
	    color = NULL_COLOR;

	    if (G_get_color(cat, &r, &g, &b, &sc)) {
		color = (r & 0xff) | ((g & 0xff) << 8) | ((b & 0xff) << 16);
	    }

	    tp->iattr = color;
	}

	return (1);
    }

    return (0);
}

/*!
   \brief Load to points to memory

   The other alternative may be to load to a tmp file.

   \param grassname vector point map 
   \param nsites
   \param has_z 2D or 3D points?
   \param has_att attributes included

   \return pointer to geopoint struct
   \return NULL on failure
 */
geopoint *Gp_load_sites(const char *grassname, int *nsites, int *has_z,
			int *has_att)
{
    struct Map_info map;
    static struct line_pnts *Points = NULL;
    static struct line_cats *Cats = NULL;
    geopoint *top, *gpt, *prev;
    int np, ltype, eof;
    struct Cell_head wind;
    RASTER_MAP_TYPE rtype;
    int ndim;
    const char *mapset;

    np = 0;
    eof = 0;
    *has_z = *has_att = 0;

    mapset = G_find_vector2(grassname, "");
    if (!mapset) {
	G_warning(_("Vector map <%s> not found"), grassname);
	return NULL;
    }

    Vect_set_open_level(2);
    if (Vect_open_old(&map, grassname, "") == -1) {
	G_fatal_error(_("Unable to open vector map <%s>"),
		      G_fully_qualified_name(grassname, mapset));
    }

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

    top = gpt = (geopoint *) G_malloc(sizeof(geopoint));
    if (!top) {
	return (NULL);
    }

    G_get_set_window(&wind);
    Vect_set_constraint_region(&map, wind.north, wind.south, wind.east,
			       wind.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);

    /* get ndim */
    ndim = 2;
    if (Vect_is_3d(&map)) {
	ndim = 3;
    }

    /* set rtype */
    rtype = CELL_TYPE;

    while (eof == 0) {
	ltype = Vect_read_next_line(&map, Points, Cats);
	switch (ltype) {
	case -1:
	    {
		G_warning(_("Unable to read vector map <%s>"),
			  G_fully_qualified_name(grassname, mapset));
		return (NULL);
	    }
	case -2:		/* EOF */
	    {
		eof = 1;
		continue;
	    }
	}
	if ((ltype & GV_POINTS)) {
	    np++;
	    gpt->p3[X] = Points->x[0];
	    gpt->p3[Y] = Points->y[0];

	    if (ndim > 2) {
		*has_z = 1;
		gpt->dims = 3;
		gpt->p3[Z] = Points->z[0];
	    }
	    else {
		gpt->dims = 2;
		*has_z = 0;
	    }

	    if (Cats->n_cats > 0) {
		*has_att = 1;
		gpt->fattr = Cats->field[0];	/* Is this correct? */
		/* gpt->cat = ; ??** */
		gpt->highlight_color = gpt->highlight_size =
		    gpt->highlight_marker = FALSE;
	    }
	    else {
		gpt->fattr = 0;
		*has_att = 0;
	    }

	    gpt->iattr = gpt->fattr;
	    gpt->cattr = NULL;

	    G_debug(3, "loading vector point %d %f %f -- %d",
		    np, Points->x[0], Points->y[0], Cats->n_cats);

	    gpt->next = (geopoint *) G_malloc(sizeof(geopoint));	/* G_fatal_error */
	    if (!gpt->next) {
		return (NULL);
	    }

	    prev = gpt;
	    gpt = gpt->next;
	}

    }
    if (np > 0) {
	prev->next = NULL;
	G_free(gpt);
    }

    Vect_close(&map);

    if (!np) {
	G_warning(_("No points from vector map <%s> fall within current region"),
		  G_fully_qualified_name(grassname, mapset));
	return (NULL);
    }
    else {
	G_message(_("Vector map <%s> loaded (%d points)"),
		  G_fully_qualified_name(grassname, mapset), np);
    }

    *nsites = np;

    return (top);
}