21#include <grass/colors.h>
22#include <grass/raster.h>
23#include <grass/vector.h>
24#include <grass/dbmi.h>
25#include <grass/glocale.h>
26#include <grass/ogsf.h>
33static int Tot_mem = 0;
50 struct line_pnts *points;
51 struct line_cats *Cats =
NULL;
52 geoline *top, *gln, *prev;
53 int np, i, n, nareas, nl = 0, area, type, is3d;
54 struct Cell_head wind;
60 G_warning(_(
"Vector map <%s> not found"), grassname);
64 Vect_set_open_level(2);
65 if (Vect_open_old(&map, grassname,
"") == -1) {
66 G_warning(_(
"Unable to open vector map <%s>"),
71 top = gln = (geoline *) G_malloc(
sizeof(geoline));
79 Tot_mem +=
sizeof(geoline);
82 points = Vect_new_line_struct();
83 Cats = Vect_new_cats_struct();
86 Vect_set_constraint_region(&map, wind.north, wind.south, wind.east,
87 wind.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
89 is3d = Vect_is_3d(&map);
92 n = Vect_get_num_areas(&map);
94 G_debug(3,
"Reading vector areas (nareas = %d)", n);
95 for (area = 1; area <= n; area++) {
97 Vect_get_area_points(&map, area, points);
98 if (points->n_points < 3)
102 gln->highlighted = 0;
104 gln->type = OGSF_POLYGON;
105 gln->npts = np = points->n_points;
110 gln->p3 = (Point3 *) G_calloc(np,
sizeof(Point3));
115 Tot_mem += (np *
sizeof(Point3));
120 gln->p2 = (Point2 *) G_calloc(np,
sizeof(Point2));
125 Tot_mem += (np *
sizeof(Point2));
129 for (i = 0; i < np; i++) {
131 gln->p3[i][
X] = points->x[i];
132 gln->p3[i][
Y] = points->y[i];
133 gln->p3[i][Z] = points->z[i];
136 gln->p2[i][
X] = points->x[i];
137 gln->p2[i][
Y] = points->y[i];
142 vect[0][
X] = (float)(gln->p3[0][
X] - gln->p3[1][
X]);
143 vect[0][
Y] = (float)(gln->p3[0][
Y] - gln->p3[1][
Y]);
144 vect[0][Z] = (float)(gln->p3[0][Z] - gln->p3[1][Z]);
145 vect[1][
X] = (float)(gln->p3[2][
X] - gln->p3[1][
X]);
146 vect[1][
Y] = (float)(gln->p3[2][
Y] - gln->p3[1][
Y]);
147 vect[1][Z] = (float)(gln->p3[2][Z] - gln->p3[1][Z]);
153 gln->next = (geoline *) G_malloc(
sizeof(geoline));
159 Tot_mem +=
sizeof(geoline);
166 G_debug(3,
"%d areas loaded", nareas);
169 G_debug(3,
"Reading vector lines ...");
170 while (-1 < (type = Vect_read_next_line(&map, points, Cats))) {
171 G_debug(3,
"line type = %d", type);
172 if (type & (GV_LINES | GV_FACE)) {
173 if (type & (GV_LINES)) {
174 gln->type = OGSF_LINE;
177 gln->type = OGSF_POLYGON;
182 gln->highlighted = 0;
184 gln->npts = np = points->n_points;
189 gln->p3 = (Point3 *) G_calloc(np,
sizeof(Point3));
194 Tot_mem += (np *
sizeof(Point3));
199 gln->p2 = (Point2 *) G_calloc(np,
sizeof(Point2));
204 Tot_mem += (np *
sizeof(Point2));
208 for (i = 0; i < np; i++) {
210 gln->p3[i][
X] = points->x[i];
211 gln->p3[i][
Y] = points->y[i];
212 gln->p3[i][Z] = points->z[i];
215 gln->p2[i][
X] = points->x[i];
216 gln->p2[i][
Y] = points->y[i];
220 if (is3d && gln->type == OGSF_POLYGON) {
221 vect[0][
X] = (float)(gln->p3[0][
X] - gln->p3[1][
X]);
222 vect[0][
Y] = (float)(gln->p3[0][
Y] - gln->p3[1][
Y]);
223 vect[0][Z] = (float)(gln->p3[0][Z] - gln->p3[1][Z]);
224 vect[1][
X] = (float)(gln->p3[2][
X] - gln->p3[1][
X]);
225 vect[1][
Y] = (float)(gln->p3[2][
Y] - gln->p3[1][
Y]);
226 vect[1][Z] = (float)(gln->p3[2][Z] - gln->p3[1][Z]);
228 G_debug(3,
"norm %f %f %f", gln->norm[0], gln->norm[1],
233 if (Cats->n_cats > 0) {
235 Cats = Vect_new_cats_struct();
239 Vect_reset_cats(Cats);
242 gln->next = (geoline *) G_malloc(
sizeof(geoline));
247 Tot_mem +=
sizeof(geoline);
255 G_debug(3,
"%d lines loaded", nl);
263 Tot_mem -=
sizeof(geoline);
269 G_warning(_(
"No features from vector map <%s> fall within current region"),
274 G_message(_(
"Vector map <%s> loaded (%d features)"),
281 G_debug(3,
"Total vect memory = %d Kbytes", Tot_mem / 1000);
294 G_debug(5,
"sub_Vectmem(): minus=%d", minus);
318 struct field_info *Fi;
320 int nvals, cat, nlines, nskipped;
328 if(!gv || !gv->tstyle || !gv->filename)
336 Vect_set_open_level(1);
337 if (Vect_open_old(&Map, gv->filename,
"") == -1) {
342 Fi = Vect_get_field(&Map, gv->tstyle->layer);
344 G_warning(_(
"Database connection not defined for layer %d"),
348 driver = db_start_driver_open_database(Fi->driver, Fi->database);
350 G_fatal_error(_(
"Unable to open database <%s> by driver <%s>"),
351 Fi->database, Fi->driver);
353 G_message(_(
"Loading thematic vector layer <%s>..."),
355 nlines = nskipped = 0;
356 for(gvt = gv->lines; gvt; gvt = gvt->next) {
357 gvt->style = (gvstyle *) G_malloc(
sizeof(gvstyle));
358 G_zero(gvt->style,
sizeof(gvstyle));
361 gvt->style->color = gv->style->color;
362 gvt->style->symbol = gv->style->symbol;
363 gvt->style->size = gv->style->size;
364 gvt->style->width = gv->style->width;
368 Vect_cat_get(gvt->cats, gv->tstyle->layer, &cat);
376 if (!Rast_get_c_color((
const CELL *) &cat, &red, &grn, &blu, colors)) {
377 G_warning(_(
"No color rule defined for category %d"), cat);
378 gvt->style->color = gv->style->color;
384 if (gv->tstyle->color_column) {
385 nvals = db_select_value(
driver, Fi->table, Fi->key, cat, gv->tstyle->color_column, &value);
388 str = db_get_value_string(&value);
392 G_warning(_(
"Invalid color definition (%s)"),
394 gvt->style->color = gv->style->color;
403 if (gv->tstyle->width_column) {
404 nvals = db_select_value(
driver, Fi->table, Fi->key, cat, gv->tstyle->width_column, &value);
407 gvt->style->width = db_get_value_int(&value);
414 G_warning(_(
"%d features without category. "
415 "Unable to determine color rules for features without category."),
void G_free(void *buf)
Free allocated memory.
int G_str_to_color(const char *str, int *red, int *grn, int *blu)
Parse color string and set red,green,blue.
int G_debug(int level, const char *msg,...)
Print debugging message.
const struct driver * driver
const char * G_find_vector2(const char *name, const char *mapset)
Find a vector map (look but don't touch)
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void G_message(const char *msg,...)
Print a message to stderr.
void G_warning(const char *msg,...)
Print a warning message to stderr.
void G_get_set_window(struct Cell_head *window)
Get the current working window (region)
void GS_v3cross(float *v1, float *v2, float *v3)
Get the cross product v3 = v1 cross v2.
int Gv_load_vect_thematic(geovect *gv, struct Colors *colors)
Load styles for geolines based on thematic mapping.
void sub_Vectmem(int minus)
Tracking memory.
geoline * Gv_load_vect(const char *grassname, int *nlines)
Load vector map to memory.
char * G_fully_qualified_name(const char *name, const char *mapset)
Get fully qualified element name.
void G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.