GRASS 8 Programmer's Manual 8.6.0dev(2026)-55de52a352
Loading...
Searching...
No Matches
gp3.c
Go to the documentation of this file.
1/*!
2 \file lib/ogsf/gp3.c
3
4 \brief OGSF library - loading point sets (lower level functions)
5
6 GRASS OpenGL gsurf OGSF Library
7
8 SPDX-FileCopyrightText: 1999-2008, 2011 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Bill Brown USACERL, GMSL/University of Illinois (January 1994)
12 \author Updated by Martin Landa <landa.martin gmail.com>
13 (doxygenized in May 2008, thematic mapping in June 2011)
14 */
15
16#include <stdlib.h>
17
18#include <grass/gis.h>
19#include <grass/colors.h>
20#include <grass/raster.h>
21#include <grass/vector.h>
22#include <grass/dbmi.h>
23#include <grass/glocale.h>
24#include <grass/ogsf.h>
25
26/*!
27 Free linked list of geopoint objects
28 */
29static void free_geopoint_list(geopoint *top);
30
31/*!
32 \brief Load to points to memory
33
34 The other alternative may be to load to a tmp file.
35
36 \param name name of vector map to be loaded
37 \param[out] nsites number of loaded points
38 \param[out] has_z 2D or 3D points data loaded?
39
40 \return pointer to geopoint struct (array)
41 \return NULL on failure
42 */
43geopoint *Gp_load_sites(const char *name, int *nsites, int *has_z)
44{
45 struct Map_info map;
46 static struct line_pnts *Points = NULL;
47 struct line_cats *Cats = NULL;
48 geopoint *top, *gpt, *prev;
49 int np, ltype, eof;
50 struct Cell_head wind;
51 int ndim;
52 const char *mapset;
53
54 np = 0;
55 eof = 0;
56
57 mapset = G_find_vector2(name, "");
58 if (!mapset) {
59 G_warning(_("Vector map <%s> not found"), name);
60 return NULL;
61 }
62
64 if (Vect_open_old(&map, name, "") == -1) {
65 G_fatal_error(_("Unable to open vector map <%s>"),
67 }
68
69 Points = Vect_new_line_struct();
71
72 top = gpt = (geopoint *)G_malloc(sizeof(geopoint));
73 G_zero(gpt, sizeof(geopoint));
74 if (!top) {
75 return NULL;
76 }
77
78 G_get_set_window(&wind);
79 Vect_set_constraint_region(&map, wind.north, wind.south, wind.east,
81
82 /* get ndim */
83 *has_z = 0;
84 ndim = 2;
85 if (Vect_is_3d(&map)) {
86 *has_z = 1;
87 ndim = 3;
88 }
89 char *mname = G_fully_qualified_name(name, mapset);
90
91 while (eof == 0) {
92 ltype = Vect_read_next_line(&map, Points, Cats);
93 switch (ltype) {
94 case -1: {
95 G_warning(_("Unable to read vector map <%s>"), mname);
97 free_geopoint_list(top);
98 return NULL;
99 }
100 case -2: /* EOF */
101 {
102 eof = 1;
103 continue;
104 }
105 }
106 if ((ltype & GV_POINTS)) {
107 np++;
108 gpt->p3[X] = Points->x[0];
109 gpt->p3[Y] = Points->y[0];
110
111 if (ndim > 2) {
112 gpt->dims = 3;
113 gpt->p3[Z] = Points->z[0];
114 }
115 else {
116 gpt->dims = 2;
117 }
118
119 /* Store category info for thematic display */
120 if (Cats->n_cats > 0) {
121 gpt->cats = Cats;
123 }
124 else {
126 }
127 /* initialize style */
128 gpt->highlighted = 0;
129
130 G_debug(5, "loading vector point %d x=%f y=%f ncats=%d", np,
131 Points->x[0], Points->y[0], Cats->n_cats);
132
133 gpt->next =
134 (geopoint *)G_malloc(sizeof(geopoint)); /* G_fatal_error */
135 G_zero(gpt->next, sizeof(geopoint));
136 if (!gpt->next) {
137 free_geopoint_list(top);
138 return NULL;
139 }
140
141 prev = gpt;
142 gpt = gpt->next;
143 }
144 }
145 if (np > 0) {
146 prev->next = NULL;
147 G_free(gpt);
148 }
149
150 Vect_close(&map);
151
152 if (!np) {
153 G_warning(
154 _("No points from vector map <%s> fall within current region"),
155 mname);
156 G_free(mname);
157 free_geopoint_list(top);
158 return (NULL);
159 }
160 else {
161 G_message(_("Vector map <%s> loaded (%d points)"), mname, np);
162 G_free(mname);
163 }
164
165 *nsites = np;
166
167 return top;
168}
169
170/*!
171 \brief Load styles for geopoints based on thematic mapping
172
173 \param gp pointer to geosite structure
174 \param colors pointer to Colors structure or NULL
175
176 \return number of points defined by thematic mapping
177 \return -1 on error
178 */
180{
181 geopoint *gpt;
182
183 struct Map_info Map;
184 struct field_info *Fi;
185
186 int nvals, cat, npts, nskipped;
187 int red, blu, grn;
188 const char *str;
189 const char *mapset;
190 char *fname;
191
193 dbValue value;
194
195 if (!gp || !gp->tstyle || !gp->filename)
196 return -1;
197
198 mapset = G_find_vector2(gp->filename, "");
199 if (!mapset) {
200 G_fatal_error(_("Vector map <%s> not found"), gp->filename);
201 }
202
204 if (Vect_open_old(&Map, gp->filename, "") == -1) {
205 G_fatal_error(_("Unable to open vector map <%s>"),
206 G_fully_qualified_name(gp->filename, mapset));
207 }
208
209 Fi = Vect_get_field(&Map, gp->tstyle->layer);
210 if (!Fi) {
211 G_warning(_("Database connection not defined for layer %d"),
212 gp->tstyle->layer);
213 }
214 else {
215 driver = db_start_driver_open_database(Fi->driver, Fi->database);
216 if (!driver)
217 G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
218 Fi->database, Fi->driver);
219 }
220 fname = G_fully_qualified_name(gp->filename, mapset);
221 G_message(_("Loading thematic points layer <%s>..."), fname);
222 G_free(fname);
223 npts = nskipped = 0;
224 for (gpt = gp->points; gpt; gpt = gpt->next) {
225 gpt->style = (gvstyle *)G_malloc(sizeof(gvstyle));
226 G_zero(gpt->style, sizeof(gvstyle));
227
228 /* use default style */
229 gpt->style->color = gp->style->color;
230 gpt->style->symbol = gp->style->symbol;
231 gpt->style->size = gp->style->size;
232 gpt->style->width = gp->style->width;
233
234 cat = -1;
235 if (gpt->cats)
236 Vect_cat_get(gpt->cats, gp->tstyle->layer, &cat);
237 if (cat < 0) {
238 nskipped++;
239 continue;
240 }
241
242 /* color */
243 if (colors) {
244 if (!Rast_get_c_color((const CELL *)&cat, &red, &grn, &blu,
245 colors)) {
246 G_warning(_("No color rule defined for category %d"), cat);
247 gpt->style->color = gp->style->color;
248 }
249 gpt->style->color = (red & RED_MASK) +
250 ((int)((grn) << 8) & GRN_MASK) +
251 ((int)((blu) << 16) & BLU_MASK);
252 }
253 if (gp->tstyle->color_column) {
254 if (driver) {
255 nvals = db_select_value(driver, Fi->table, Fi->key, cat,
256 gp->tstyle->color_column, &value);
257 if (nvals < 1)
258 continue;
259 str = db_get_value_string(&value);
260 if (!str)
261 continue;
262 }
263 if (G_str_to_color(str, &red, &grn, &blu) != 1) {
264 G_warning(_("Invalid color definition (%s)"), str);
265 gpt->style->color = gp->style->color;
266 }
267 else {
268 gpt->style->color = (red & RED_MASK) +
269 ((int)((grn) << 8) & GRN_MASK) +
270 ((int)((blu) << 16) & BLU_MASK);
271 }
272 }
273
274 /* size */
275 if (gp->tstyle->size_column) {
276 nvals = db_select_value(driver, Fi->table, Fi->key, cat,
277 gp->tstyle->size_column, &value);
278 if (nvals < 1)
279 continue;
280 gpt->style->size = db_get_value_int(&value);
281 }
282
283 /* width */
284 if (gp->tstyle->width_column) {
285 nvals = db_select_value(driver, Fi->table, Fi->key, cat,
286 gp->tstyle->width_column, &value);
287 if (nvals < 1)
288 continue;
289 gpt->style->width = db_get_value_int(&value);
290 }
291
292 /* symbol/marker */
293 if (gp->tstyle->symbol_column) {
294 nvals = db_select_value(driver, Fi->table, Fi->key, cat,
295 gp->tstyle->symbol_column, &value);
296 if (nvals < 1)
297 continue;
298 str = db_get_value_string(&value);
299 gpt->style->symbol = GP_str_to_marker(str);
300 }
301
302 npts++;
303 }
304
305 if (nskipped > 0)
306 G_warning(
307 _("%d points without category. "
308 "Unable to determine color rules for features without category."),
309 nskipped);
310 if (driver)
313 return npts;
314}
315
316static void free_geopoint_list(geopoint *top)
317{
318 while (top) {
319 geopoint *next = top->next;
320 G_free(top);
321 top = next;
322 }
323}
#define NULL
Definition ccmath.h:32
Main header of GRASS DataBase Management Interface.
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition color_str.c:99
int db_select_value(dbDriver *, const char *, const char *, int, const char *, dbValue *)
Select one (first) value from table/column for key/id.
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
Definition db.c:58
int db_get_value_int(dbValue *)
Get integer value.
Definition value.c:36
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
Definition db.c:25
const char * db_get_value_string(dbValue *)
Get string value.
Definition value.c:90
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:21
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_get_set_window(struct Cell_head *)
Get the current working window (region)
#define G_malloc(n)
Definition defs/gis.h:136
char * G_fully_qualified_name(const char *, const char *)
Get fully qualified element name.
Definition nme_in_mps.c:99
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
Definition find_vect.c:60
int GP_str_to_marker(const char *)
Determine point marker symbol for string.
Definition gp2.c:686
int Rast_get_c_color(const CELL *, int *, int *, int *, struct Colors *)
Gets color from raster map (CELL)
Definition color_get.c:65
int Vect_reset_cats(struct line_cats *)
Reset category structure to make sure cats structure is clean to be re-used.
int Vect_cat_get(const struct line_cats *, int, int *)
Get first found category of given field.
int Vect_set_constraint_region(struct Map_info *, double, double, double, double, double, double)
Set constraint region.
Definition constraint.c:46
int Vect_close(struct Map_info *)
Close vector map.
struct field_info * Vect_get_field(struct Map_info *, int)
Get information about link to database (by layer number)
Definition field.c:508
int Vect_open_old(struct Map_info *, const char *, const char *)
Open existing vector map for reading.
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
void Vect_destroy_field_info(struct field_info *)
Free a struct field_info and all memory associated with it.
Definition field.c:626
int Vect_read_next_line(struct Map_info *, struct line_pnts *, struct line_cats *)
Read next vector feature.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition line.c:43
int Vect_set_open_level(int)
Predetermine level at which a vector map will be opened for reading.
int Vect_is_3d(struct Map_info *)
Check if vector map is 3D.
#define PORT_DOUBLE_MAX
Limits for portable types.
Definition dig_defines.h:66
#define GV_POINTS
int CELL
Definition gis.h:631
#define _(str)
Definition glocale.h:10
geopoint * Gp_load_sites(const char *name, int *nsites, int *has_z)
Load to points to memory.
Definition gp3.c:43
int Gp_load_sites_thematic(geosite *gp, struct Colors *colors)
Load styles for geopoints based on thematic mapping.
Definition gp3.c:179
const char * name
Definition named_colr.c:6
OGSF header file (structures)
#define RED_MASK
Definition ogsf.h:201
#define X
Definition ogsf.h:141
#define BLU_MASK
Definition ogsf.h:203
#define Z
Definition ogsf.h:143
#define GRN_MASK
Definition ogsf.h:202
#define Y
Definition ogsf.h:142
2D/3D raster map header (used also for region)
Definition gis.h:443
double north
Extent coordinates (north)
Definition gis.h:489
double east
Extent coordinates (east)
Definition gis.h:493
double top
Extent coordinates (top) - 3D data.
Definition gis.h:497
double south
Extent coordinates (south)
Definition gis.h:491
double west
Extent coordinates (west)
Definition gis.h:495
Definition gis.h:689
Vector map info.
Layer (old: field) information.
Point instance.
Definition ogsf.h:401
struct g_point * next
Definition ogsf.h:412
Vector map (points)
Definition ogsf.h:416
Struct for vector feature displaying attributes.
Definition ogsf.h:308
Feature category info.
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
double * x
Array of X coordinates.
double * z
Array of Z coordinates.