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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
/*!
* \file cats.c
*
* \brief Vector library - Category management
*
* 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 Original author CERL, probably Dave Gerdes or Mike
* Higgins
* \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
* \author Various updates by Martin Landa <landa.martin gmail.com>
*/
#include <stdlib.h>
#include <string.h>
#include <grass/gis.h>
#include <grass/Vect.h>
#include <grass/glocale.h>
static int cmp(const void *pa, const void *pb);
struct line_cats *Vect__new_cats_struct(void);
/*!
\brief Creates and initializes line_cats structure.
This structure is used for reading and writing vector cats. The
library routines handle all memory allocation.
To free allocated memory call Vect_destroy_cats_struct().
\return struct line_cats *
\return NULL on error
*/
struct line_cats *Vect_new_cats_struct()
{
struct line_cats *p;
if (NULL == (p = Vect__new_cats_struct()))
G_fatal_error(_("Vect_new_cats_struct(): Out of memory"));
return p;
}
/*!
\brief Creates and initializes line_cats structure (lower level fn)
This structure is used for reading and writing vector cats. The
library routines handle all memory allocation.
\return struct line_cats *
*/
struct line_cats *Vect__new_cats_struct()
{
struct line_cats *p;
p = (struct line_cats *)G_malloc(sizeof(struct line_cats));
/* n_cats MUST be initialized to zero */
if (p)
p->n_cats = 0;
if (p)
p->alloc_cats = 0;
return p;
}
/*!
\brief Frees all memory associated with line_cats structure, including the struct itself.
\param p line_cats structure
\return 0
*/
int Vect_destroy_cats_struct(struct line_cats *p)
{
if (p) { /* probably a moot test */
if (p->n_cats) {
G_free((void *)p->field);
G_free((void *)p->cat);
}
G_free((void *)p);
}
return 0;
}
/*!
\brief Add new field/cat to category structure if doesn't exist yet.
\param[in] Cats line_cats structure
\param[in] field layer number
\param[in] cat category number
\return number of categories
\return 0 if no space for new category in structure, n_cats would be > GV_NCATS_MAX
\return -1 on out of memory
\return -2 if field out of range: 1 - GV_FIELD_MAX or cat out of range: 1 - GV_CAT_MAX
*/
int Vect_cat_set(struct line_cats *Cats, int field, int cat)
{
register int n;
/* check input values */
/* compiler may warn:
* comparison is always 0 due to limited range of data type
* but remember that limit is set to portable data type length
* and machine native size may be longer */
/*
if (field < 1 || field > GV_FIELD_MAX || cat < 0 || cat > GV_CAT_MAX)
return (-2);
*/
/* go through old cats and find if field/category exists */
for (n = 0; n < Cats->n_cats; n++) {
if (Cats->field[n] == field && Cats->cat[n] == cat)
return (1);
}
/* field was not found so we shall append new cat */
/* test if space exist */
if (n >= GV_NCATS_MAX) {
G_fatal_error(_("Too many categories (%d), unable to set cat %d (layer %d)"),
Cats->n_cats, cat, field);
}
if (Cats->n_cats == Cats->alloc_cats) {
if (0 > dig_alloc_cats(Cats, Cats->n_cats + 100))
return (-1);
}
n = Cats->n_cats;
Cats->field[n] = field;
Cats->cat[n] = cat;
Cats->n_cats++;
return (1);
}
/*!
\brief Get first found category of given field.
'cat' is set to first category found or -1 if field was not found
\param[in] Cats line_cats structure
\param[in] field layer number
\param[in] cat pointer to variable where cat will be written
\return 1 found
\return 0 layer does not exist
*/
int Vect_cat_get(struct line_cats *Cats, int field, int *cat)
{
register int n;
/* check input value */
/*
if (field < 1 || field > GV_FIELD_MAX)
return (0);
*/
*cat = -1;
/* go through cats and find if field exist */
for (n = 0; n < Cats->n_cats; n++) {
if (Cats->field[n] == field) {
*cat = Cats->cat[n];
return (1);
}
}
/* field was not found */
return (0);
}
/*!
\brief Get list of categories of given field.
\param Cats line_cats structure
\param field layer number
\param[out] cats pointer to list where cats will be written
\return number of found categories
\return -1 on invalid field
*/
int Vect_field_cat_get(struct line_cats *Cats, int field, struct ilist *cats)
{
int n;
/* reset list of categories */
Vect_reset_list(cats);
/* check input value */
if (field < 1 || field > GV_FIELD_MAX)
return -1;
/* go through cats and find if field exist */
for (n = 0; n < Cats->n_cats; n++) {
if (Cats->field[n] != field)
continue;
Vect_list_append(cats, Cats->cat[n]);
}
return cats->n_values;
}
/*!
\brief Delete all categories of given layer
\param[in] Cats line_cats structure
\param[in] field layer number
\return 1 deleted
\return 0 layer does not exist
*/
int Vect_cat_del(struct line_cats *Cats, int field)
{
int n, m, found = 0;
/* check input value */
/*
if (field < 1 || field > GV_FIELD_MAX)
return (0);
*/
/* go through cats and find if field exist */
for (n = 0; n < Cats->n_cats; n++) {
if (Cats->field[n] == field) {
for (m = n; m < Cats->n_cats - 1; m++) {
Cats->field[m] = Cats->field[m + 1];
Cats->cat[m] = Cats->cat[m + 1];
}
Cats->n_cats--;
found = 1;
n--; /* check again this position */
}
}
return (found);
}
/*!
\brief Delete field/cat from line_cats structure
\param[in] Cats line_cats structure
\param[in] field layer number
\param[in] cat category to be deleted or -1 to delete all cats of given field
\return 1 deleted
\return 0 field/category number does not exist
*/
int Vect_field_cat_del(struct line_cats *Cats, int field, int cat)
{
register int n, m, found = 0;
/* check input value */
/*
if (field < 1 || field > GV_FIELD_MAX)
return (0);
*/
/* go through cats and find if field exist */
for (n = 0; n < Cats->n_cats; n++) {
if (Cats->field[n] == field && (Cats->cat[n] == cat || cat == -1)) {
for (m = n; m < Cats->n_cats - 1; m++) {
Cats->field[m] = Cats->field[m + 1];
Cats->cat[m] = Cats->cat[m + 1];
}
Cats->n_cats--;
found = 1;
n--; /* check again this position */
}
}
return (found);
}
/*!
\brief Reset category structure to make sure cats structure is clean to be re-used.
I.e. it has no cats associated with it. Cats must have
previously been created with Vect_new_cats_struct()
\param[out] Cats line_cats structure
\return 0
*/
int Vect_reset_cats(struct line_cats *Cats)
{
Cats->n_cats = 0;
return 0;
}
/*!
\brief Allocate memory for cat_list structure.
\return poiter to allocated structure
\return NULL on out of memory
*/
struct cat_list *Vect_new_cat_list()
{
struct cat_list *p;
p = (struct cat_list *)G_malloc(sizeof(struct cat_list));
/* n_ranges MUST be initialized to zero */
if (p) {
p->n_ranges = 0;
p->alloc_ranges = 0;
p->field = 0;
p->min = NULL;
p->max = NULL;
}
return p;
}
/*!
\brief Frees allocated cat_list memory.
\param[in] p line_cats structure
\return 0
*/
int Vect_destroy_cat_list(struct cat_list *p)
{
if (p) { /* probably a moot test */
if (p->n_ranges) {
G_free((void *)p->min);
G_free((void *)p->max);
}
G_free((void *)p);
}
return 0;
}
/*!
\brief Convert string of categories and cat ranges separated by commas to cat_list.
Examples of string: 2,3,5-9,20. str - input string
\param[in] str cat list string
\param[out] list result cat_list structure
\return number of errors in ranges
*/
int Vect_str_to_cat_list(const char *str, struct cat_list *list)
{
int i, nr, l, err = 0;
const char *s, *e;
char buf[100];
int min, max;
G_debug(3, "Vect_str_to_cat_list(): str = %s", str);
list->n_ranges = 0;
l = strlen(str);
/* find number of ranges */
nr = 1; /* one range */
for (i = 0; i < l; i++)
if (str[i] == ',')
nr++;
/* allocate space */
if (list->alloc_ranges == 0) {
list->min = (int *)G_malloc(nr * sizeof(int));
list->max = (int *)G_malloc(nr * sizeof(int));
}
else if (nr > list->alloc_ranges) {
list->min = (int *)G_realloc((void *)list->min, nr * sizeof(int));
list->max = (int *)G_realloc((void *)list->max, nr * sizeof(int));
}
/* go through string and read ranges */
i = 0;
s = str;
while (s) {
e = (char *)strchr(s, ','); /* first comma */
if (e) {
l = e - s;
strncpy(buf, s, l);
buf[l] = '\0';
s = e + 1;
}
else {
strcpy(buf, s);
s = NULL;
}
G_debug(3, " buf = %s", buf);
if (sscanf(buf, "%d-%d", &min, &max) == 2) {
}
else if (sscanf(buf, "%d", &min) == 1)
max = min;
else { /* error */
G_warning(_("Unable to convert category string '%s' (from '%s') to category range"),
buf, str);
err++;
continue;
}
list->min[i] = min;
list->max[i] = max;
i++;
}
list->n_ranges = i;
return (err);
}
/*!
\brief Convert ordered array of integers to cat_list structure.
\param[in] vals array of integers
\param[in] nvals number of values
\param[out] list result cat_list structure
\return number of ranges
*/
int Vect_array_to_cat_list(int *vals, int nvals, struct cat_list *list)
{
int i, range;
G_debug(1, "Vect_array_to_cat_list()");
range = -1;
for (i = 0; i < nvals; i++) {
if (i == 0 || (vals[i] - list->max[range]) > 1) {
range++;
if (range == list->alloc_ranges) {
list->alloc_ranges += 1000;
list->min = (int *)G_realloc((void *)list->min,
list->alloc_ranges *
sizeof(int));
list->max =
(int *)G_realloc((void *)list->max,
list->alloc_ranges * sizeof(int));
}
list->min[range] = vals[i];
list->max[range] = vals[i];
}
else {
list->max[range] = vals[i];
}
}
list->n_ranges = range + 1;
return (list->n_ranges);
}
/*!
\brief Check if category number is in list.
\param[in] cat category number
\param[in] list cat_list structure
\return TRUE if cat is in list
\return FALSE if it is not
*/
int Vect_cat_in_cat_list(int cat, struct cat_list *list)
{
int i;
for (i = 0; i < list->n_ranges; i++)
if (cat >= list->min[i] && cat <= list->max[i])
return (TRUE);
return (FALSE);
}
/*!
\brief Check if category is in ordered array of integers.
\param[in] cat category number
\param[in] array ordered array of integers
\param[in] ncats number of categories in array
\return TRUE if cat is in list
\return FALSE if it is not
*/
int Vect_cat_in_array(int cat, int *array, int ncats)
{
int *i;
i = bsearch((void *)&cat, (void *)array, (size_t) ncats,
sizeof(int), cmp);
if (i != NULL)
return (TRUE);
return (FALSE);
}
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;
}
|