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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*!
\file lib/vector/Vlib/array.c
\brief Vector library - category array
Higher level functions for reading/writing/manipulating vectors.
(C) 2001-2009 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 Radim Blazek
*/
#include <stdlib.h>
#include <grass/dbmi.h>
#include <grass/vector.h>
#include <grass/glocale.h>
/* function prototypes */
static int cmp(const void *pa, const void *pb);
static int in_array(int *cats, size_t ncats, int cat);
/*!
\brief Create new struct varray and allocate space for given number of items.
Space allocated is 'size + 1' so that lines are accessed by line id.
Array values are set to 0.
\param size size of array
\return pointer to new struct varray
\return NULL if failed
*/
struct varray *Vect_new_varray(int size)
{
struct varray *p;
p = (struct varray *)G_malloc(sizeof(struct varray));
if (p == NULL)
return NULL;
p->size = size;
p->c = (int *)G_calloc(sizeof(char) * size + 1, sizeof(int));
if (p->c == NULL) {
G_free(p);
return NULL;
}
return p;
}
/*!
\brief Set values in 'varray' to 'value' from category string.
If category of object of given type is in <em>cstring</em> (string
representing category list like: '1,3,5-7'). <em>type</em> may be
either: GV_AREA or: GV_POINT | GV_LINE | GV_BOUNDARY | GV_CENTROID
Array is not reset to zero before, but old values (if any > 0) are
overwritten. Array must be initialised by Vect_new_varray() call.
\param Map vector map
\param field layer number
\param cstring pointer to string with categories
\param type feature type
\param value value to set up
\param[out] varray varray structure to modify
\return number of items set
\return -1 on error
*/
int Vect_set_varray_from_cat_string(struct Map_info *Map, int field,
const char *cstring, int type, int value,
struct varray *varray)
{
int ret;
struct cat_list *Clist;
G_debug(4, "Vect_set_varray_from_cat_string(): cstring = '%s'", cstring);
Clist = Vect_new_cat_list();
ret = Vect_str_to_cat_list(cstring, Clist);
if (ret > 0)
G_warning(_("%d errors in category string"), ret);
G_debug(4, " %d ranges in clist", Clist->n_ranges);
ret = Vect_set_varray_from_cat_list(Map, field, Clist, type, value, varray);
Vect_destroy_cat_list(Clist);
return ret;
}
/*!
\brief Set values in 'varray' to 'value' from category list
If category of object of given type is in <em>clist</em> (category
list). <em>type</em> may be either: GV_AREA or: GV_POINT | GV_LINE
| GV_BOUNDARY | GV_CENTROID
Array is not reset to zero before, but old values (if any > 0) are
overwritten. Array must be initialised by Vect_new_varray() call.
\param Map vector map
\param field layer number
\param clist list of categories
\param type feature type
\param value value to set up
\param[out] varray varray structure to modify
\return number of items set
\return -1 on error
*/
int Vect_set_varray_from_cat_list(struct Map_info *Map, int field,
struct cat_list *clist, int type, int value,
struct varray *varray)
{
int i, n, centr, cat;
int ni = 0; /* number of items set */
int ltype; /* line type */
struct line_cats *Cats;
G_debug(4, "Vect_set_varray_from_cat_list(): field = %d", field);
/* Check type */
if ((type & GV_AREA) && (type & (GV_POINTS | GV_LINES))) {
G_warning(_("Mixed area and other type requested for vector array"));
return 0;
}
Cats = Vect_new_cats_struct();
if (type & GV_AREA) { /* Areas */
n = Vect_get_num_areas(Map);
if (n > varray->size) { /* not enough space */
G_warning(_("Not enough space in vector array"));
return 0;
}
for (i = 1; i <= n; i++) {
centr = Vect_get_area_centroid(Map, i);
if (centr <= 0)
continue; /* No centroid */
Vect_read_line(Map, NULL, Cats, centr);
if (!Vect_cat_get(Cats, field, &cat))
continue; /* No such field */
if (Vect_cat_in_cat_list(cat, clist)) { /* cat is in list */
varray->c[i] = value;
ni++;
}
}
}
else { /* Lines */
n = Vect_get_num_lines(Map);
if (n > varray->size) { /* not enough space */
G_warning(_("Not enough space in vector array"));
return 0;
}
for (i = 1; i <= n; i++) {
ltype = Vect_read_line(Map, NULL, Cats, i);
if (!(ltype & type))
continue; /* is not specified type */
if (!Vect_cat_get(Cats, field, &cat))
continue; /* No such field */
if (Vect_cat_in_cat_list(cat, clist)) { /* cat is in list */
varray->c[i] = value;
ni++;
}
}
}
Vect_destroy_cats_struct(Cats);
return ni;
}
/* compare 2 integers in array */
static int cmp(const void *pa, const void *pb)
{
int *p1 = (int *)pa;
int *p2 = (int *)pb;
if (*p1 < *p2)
return -1;
if (*p1 > *p2)
return 1;
return 0;
}
/* check if cat is in array */
static int in_array(int *cats, size_t ncats, int cat)
{
int *p;
p = (int *)bsearch((void *)&cat, cats, ncats, sizeof(int), cmp);
if (p == NULL)
return 0;
return 1;
}
/*!
\brief Set values in 'varray' to 'value' from DB (where statement)
I category of object of given type is in categories selected from
DB based on where statement (given without where). <em>type</em>
may be either: GV_AREA or: GV_POINT | GV_LINE | GV_BOUNDARY |
GV_CENTROID
Array is not reset to zero before, but old values (if any > 0) are
overwritten. Array must be initialised by Vect_new_varray() call.
\param Map vector map
\param field layer number
\param where where statement
\param type feature type
\param value value to set up
\param[out] varray varray structure to modify
\return number of items set
\return -1 on error
*/
int Vect_set_varray_from_db(struct Map_info *Map, int field, const char *where,
int type, int value, struct varray *varray)
{
int i, n, c, centr, *cats;
int ncats;
int ni = 0; /* number of items set */
int ltype; /* line type */
struct line_cats *Cats;
struct field_info *Fi;
dbDriver *driver;
G_debug(4, "Vect_set_varray_from_db(): field = %d where = '%s'", field,
where);
/* Note: use category index once available */
/* Check type */
if ((type & GV_AREA) && (type & (GV_POINTS | GV_LINES))) {
G_warning(_("Mixed area and other type requested for vector array"));
return 0;
}
Cats = Vect_new_cats_struct();
/* Select categories from DB to array */
Fi = Vect_get_field(Map, field);
if (Fi == NULL) {
G_warning(_("Database connection not defined for layer %d"), field);
return -1;
}
driver = db_start_driver_open_database(Fi->driver, Fi->database);
if (driver == NULL) {
G_warning(_("Unable to open database <%s> by driver <%s>"),
Fi->database, Fi->driver);
return -1;
}
ncats = db_select_int(driver, Fi->table, Fi->key, where, &cats);
db_close_database_shutdown_driver(driver);
if (ncats == -1) {
G_warning(
_("Unable to select record from table <%s> (key %s, where %s)"),
Fi->table, Fi->key, where);
return -1;
}
if (type & GV_AREA) { /* Areas */
n = Vect_get_num_areas(Map);
/* IMHO varray should be allocated only when it's required AND only as
large as required as WHERE will create a small subset of all vector
features and thus on large datasets it's waste of memory to allocate
it for all features. */
if (n > varray->size) { /* not enough space */
G_warning(_("Not enough space in vector array"));
return 0;
}
for (i = 1; i <= n; i++) {
centr = Vect_get_area_centroid(Map, i);
if (centr <= 0)
continue; /* No centroid */
Vect_read_line(Map, NULL, Cats, centr);
/*if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
for (c = 0; c < Cats->n_cats; c++) {
if (Cats->field[c] == field &&
in_array(cats, ncats, Cats->cat[c])) {
varray->c[i] = value;
ni++;
break;
}
}
/*
if ( in_array ( cats, ncats, cat ) ) {
varray->c[i] = value;
ni++;
}
*/
}
}
else { /* Lines */
n = Vect_get_num_lines(Map);
if (n > varray->size) { /* not enough space */
G_warning(_("Not enough space in vector array"));
return 0;
}
for (i = 1; i <= n; i++) {
ltype = Vect_read_line(Map, NULL, Cats, i);
if (!(ltype & type))
continue; /* is not specified type */
/* if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field
*/
for (c = 0; c < Cats->n_cats; c++) {
if (Cats->field[c] == field &&
in_array(cats, ncats, Cats->cat[c])) {
varray->c[i] = value;
ni++;
break;
}
}
/*
if ( in_array ( cats, ncats, cat ) ) {
varray->c[i] = value;
ni++;
}
*/
}
}
G_free(cats);
Vect_destroy_cats_struct(Cats);
return ni;
}
|