cats.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*!
  2. * \file lib/vector/Vlib/cats.c
  3. *
  4. * \brief Vector library - Category management
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2001-2012 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public License
  11. * (>=v2). Read the file COPYING that comes with GRASS for details.
  12. *
  13. * \author Original author CERL, probably Dave Gerdes or Mike Higgins
  14. * \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  15. * \author Various updates by Martin Landa <landa.martin gmail.com>
  16. * \author Various updates by Markus Metz
  17. */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/vector.h>
  21. #include <grass/dbmi.h>
  22. #include <grass/glocale.h>
  23. static int cmp(const void *pa, const void *pb);
  24. static struct line_cats *Vect__new_cats_struct(void);
  25. /*!
  26. \brief Creates and initializes line_cats structure.
  27. This structure is used for reading and writing vector cats. The
  28. library routines handle all memory allocation.
  29. To free allocated memory call Vect_destroy_cats_struct().
  30. \return struct line_cats *
  31. \return NULL on error
  32. */
  33. struct line_cats *Vect_new_cats_struct()
  34. {
  35. struct line_cats *p;
  36. if (NULL == (p = Vect__new_cats_struct()))
  37. G_fatal_error(_("Vect_new_cats_struct(): Out of memory"));
  38. return p;
  39. }
  40. /*!
  41. \brief Creates and initializes line_cats structure (lower level fn)
  42. This structure is used for reading and writing vector cats. The
  43. library routines handle all memory allocation.
  44. \return struct line_cats *
  45. */
  46. static struct line_cats *Vect__new_cats_struct()
  47. {
  48. struct line_cats *p;
  49. p = (struct line_cats *)G_malloc(sizeof(struct line_cats));
  50. /* n_cats MUST be initialized to zero */
  51. if (p)
  52. p->n_cats = 0;
  53. if (p)
  54. p->alloc_cats = 0;
  55. return p;
  56. }
  57. /*!
  58. \brief Frees all memory associated with line_cats structure,
  59. including the struct itself.
  60. \param p line_cats structure
  61. */
  62. void Vect_destroy_cats_struct(struct line_cats *p)
  63. {
  64. if (p) { /* probably a moot test */
  65. if (p->n_cats) {
  66. G_free((void *)p->field);
  67. G_free((void *)p->cat);
  68. }
  69. G_free((void *)p);
  70. }
  71. }
  72. /*!
  73. \brief Add new field/cat to category structure if doesn't exist
  74. yet.
  75. \param[in,out] Cats line_cats structure
  76. \param[in] field layer number
  77. \param[in] cat category number
  78. \return number of categories
  79. \return 0 if no space for new category in structure, n_cats would be > GV_NCATS_MAX
  80. \return -1 on out of memory
  81. \return -2 if field out of range: 1 - GV_FIELD_MAX or cat out of range: 1 - GV_CAT_MAX
  82. */
  83. int Vect_cat_set(struct line_cats *Cats, int field, int cat)
  84. {
  85. register int n;
  86. /* check input values */
  87. /* compiler may warn:
  88. * comparison is always 0 due to limited range of data type
  89. * but remember that limit is set to portable data type length
  90. * and machine native size may be longer */
  91. /*
  92. if (field < 1 || field > GV_FIELD_MAX || cat < 0 || cat > GV_CAT_MAX)
  93. return (-2);
  94. */
  95. /* go through old cats and find if field/category exists */
  96. for (n = 0; n < Cats->n_cats; n++) {
  97. if (Cats->field[n] == field && Cats->cat[n] == cat)
  98. return (1);
  99. }
  100. /* field was not found so we shall append new cat */
  101. /* test if space exist */
  102. if (n >= GV_NCATS_MAX) {
  103. G_fatal_error(_("Too many categories (%d), unable to set cat %d (layer %d)"),
  104. Cats->n_cats, cat, field);
  105. }
  106. if (Cats->n_cats == Cats->alloc_cats) {
  107. if (0 > dig_alloc_cats(Cats, Cats->n_cats + 100))
  108. return (-1);
  109. }
  110. n = Cats->n_cats;
  111. Cats->field[n] = field;
  112. Cats->cat[n] = cat;
  113. Cats->n_cats++;
  114. return (1);
  115. }
  116. /*!
  117. \brief Get first found category of given field.
  118. <em>cat</em> is set to first category found or -1 if field was not
  119. found
  120. \param Cats pointer line_cats structure
  121. \param field layer number
  122. \param[out] cat pointer to variable where cat will be written (can be NULL)
  123. \return number of found cats for given field (first reported)
  124. \return 0 layer does not exist
  125. */
  126. int Vect_cat_get(const struct line_cats *Cats, int field, int *cat)
  127. {
  128. int n, ret;
  129. /* field was not found */
  130. ret = 0;
  131. if (cat)
  132. *cat = -1;
  133. /* check input value */
  134. if (field < 1 || field > GV_FIELD_MAX)
  135. return (0);
  136. /* go through cats and find if field exist */
  137. for (n = 0; n < Cats->n_cats; n++) {
  138. if (Cats->field[n] == field) {
  139. if (cat && ret == 0) {
  140. *cat = Cats->cat[n];
  141. }
  142. ret++;
  143. }
  144. }
  145. return ret;
  146. }
  147. /*!
  148. \brief Get list of categories of given field.
  149. \param Cats line_cats structure
  150. \param field layer number
  151. \param[out] cats pointer to list where cats will be written
  152. \return number of found categories
  153. \return -1 on invalid field
  154. */
  155. int Vect_field_cat_get(const struct line_cats *Cats, int field, struct ilist *cats)
  156. {
  157. int n;
  158. /* reset list of categories */
  159. Vect_reset_list(cats);
  160. /* check input value */
  161. if (field < 1 || field > GV_FIELD_MAX)
  162. return -1;
  163. /* go through cats and find if field exist */
  164. for (n = 0; n < Cats->n_cats; n++) {
  165. if (Cats->field[n] == field)
  166. Vect_list_append(cats, Cats->cat[n]);
  167. }
  168. return cats->n_values;
  169. }
  170. /*!
  171. \brief Delete all categories of given layer
  172. \param[in,out] Cats line_cats structure
  173. \param field layer number
  174. \return number of categories deleted
  175. \return 0 layer does not exist
  176. */
  177. int Vect_cat_del(struct line_cats *Cats, int field)
  178. {
  179. int n, m, found;
  180. /* check input value */
  181. /*
  182. if (field < 1 || field > GV_FIELD_MAX)
  183. return (0);
  184. */
  185. /* go through cats and find if field exist */
  186. m = 0;
  187. for (n = 0; n < Cats->n_cats; n++) {
  188. if (Cats->field[n] != field) {
  189. Cats->field[m] = Cats->field[n];
  190. Cats->cat[m] = Cats->cat[n];
  191. m++;
  192. }
  193. }
  194. found = Cats->n_cats - m;
  195. Cats->n_cats = m;
  196. return (found);
  197. }
  198. /*!
  199. \brief Delete field/cat from line_cats structure
  200. \param[in,out] Cats line_cats structure
  201. \param field layer number
  202. \param cat category to be deleted or -1 to delete all cats of given field
  203. \return number of categories deleted
  204. \return 0 field/category number does not exist
  205. */
  206. int Vect_field_cat_del(struct line_cats *Cats, int field, int cat)
  207. {
  208. register int n, m, found;
  209. /* check input value */
  210. /*
  211. if (field < 1 || field > GV_FIELD_MAX)
  212. return (0);
  213. */
  214. if (cat == -1)
  215. return Vect_cat_del(Cats, field);
  216. /* go through cats and find if field exist */
  217. m = 0;
  218. for (n = 0; n < Cats->n_cats; n++) {
  219. if (Cats->field[n] != field || Cats->cat[n] != cat) {
  220. Cats->field[m] = Cats->field[n];
  221. Cats->cat[m] = Cats->cat[n];
  222. m++;
  223. }
  224. }
  225. found = Cats->n_cats - m;
  226. Cats->n_cats = m;
  227. return (found);
  228. }
  229. /*!
  230. \brief Reset category structure to make sure cats structure is clean to be re-used.
  231. I.e. it has no cats associated with it. Cats must have
  232. previously been created with Vect_new_cats_struct()
  233. \param[out] Cats line_cats structure
  234. \return 0
  235. */
  236. int Vect_reset_cats(struct line_cats *Cats)
  237. {
  238. Cats->n_cats = 0;
  239. return 0;
  240. }
  241. /*!
  242. \brief Allocate memory for cat_list structure.
  243. \return pointer to allocated structure
  244. \return NULL if out of memory
  245. */
  246. struct cat_list *Vect_new_cat_list()
  247. {
  248. struct cat_list *p;
  249. p = (struct cat_list *)G_malloc(sizeof(struct cat_list));
  250. /* n_ranges MUST be initialized to zero */
  251. if (p)
  252. G_zero(p, sizeof(struct cat_list));
  253. return p;
  254. }
  255. /*!
  256. \brief Frees allocated cat_list memory.
  257. \param p pointer to line_cats structure
  258. */
  259. void Vect_destroy_cat_list(struct cat_list *p)
  260. {
  261. if (p) { /* probably a moot test */
  262. if (p->n_ranges) {
  263. G_free((void *)p->min);
  264. G_free((void *)p->max);
  265. }
  266. G_free((void *)p);
  267. }
  268. }
  269. /*!
  270. \brief Converts string of categories and cat ranges separated by commas to cat_list.
  271. \par Examples of string:
  272. \verbatim
  273. 5,6,7
  274. 3-9
  275. 2,3,5-9,20\endverbatim
  276. \par Example:
  277. \code
  278. ...
  279. str = "2,3,5-9,20"
  280. cat_list = Vect_new_cat_list()
  281. Vect_str_to_cat_list(str, cat_list)
  282. \endcode
  283. \verbatim
  284. cat_list->field = 0
  285. cat_list->n_ranges = 4
  286. cat_list->min = {2, 3, 5, 20}
  287. cat_list->max = {2, 3, 9, 20}
  288. \endverbatim
  289. \param str category list as a string
  290. \param[in,out] list pointer to cat_list structure
  291. \return number of errors in ranges
  292. */
  293. int Vect_str_to_cat_list(const char *str, struct cat_list *list)
  294. {
  295. int i, nr, l, err = 0;
  296. const char *s, *e;
  297. char buf[100];
  298. int min, max;
  299. G_debug(3, "Vect_str_to_cat_list(): str = %s", str);
  300. list->n_ranges = 0;
  301. l = strlen(str);
  302. /* find number of ranges */
  303. nr = 1; /* one range */
  304. for (i = 0; i < l; i++)
  305. if (str[i] == ',')
  306. nr++;
  307. /* allocate space */
  308. if (list->alloc_ranges == 0) {
  309. list->min = (int *)G_malloc(nr * sizeof(int));
  310. list->max = (int *)G_malloc(nr * sizeof(int));
  311. }
  312. else if (nr > list->alloc_ranges) {
  313. list->min = (int *)G_realloc((void *)list->min, nr * sizeof(int));
  314. list->max = (int *)G_realloc((void *)list->max, nr * sizeof(int));
  315. }
  316. /* go through string and read ranges */
  317. i = 0;
  318. s = str;
  319. while (s) {
  320. e = (char *)strchr(s, ','); /* first comma */
  321. if (e) {
  322. l = e - s;
  323. strncpy(buf, s, l);
  324. buf[l] = '\0';
  325. s = e + 1;
  326. }
  327. else {
  328. strcpy(buf, s);
  329. s = NULL;
  330. }
  331. G_debug(3, " buf = %s", buf);
  332. if (sscanf(buf, "%d-%d", &min, &max) == 2) {
  333. }
  334. else if (sscanf(buf, "%d", &min) == 1)
  335. max = min;
  336. else { /* error */
  337. G_warning(_("Unable to convert category string '%s' (from '%s') to category range"),
  338. buf, str);
  339. err++;
  340. continue;
  341. }
  342. list->min[i] = min;
  343. list->max[i] = max;
  344. i++;
  345. }
  346. list->n_ranges = i;
  347. return (err);
  348. }
  349. /*!
  350. \brief Convert ordered array of integers to cat_list structure.
  351. \param vals array of integers
  352. \param nvals number of values
  353. \param[in,out] list pointer to cat_list structure
  354. \return number of ranges
  355. */
  356. int Vect_array_to_cat_list(const int *vals, int nvals, struct cat_list *list)
  357. {
  358. int i, range;
  359. G_debug(1, "Vect_array_to_cat_list()");
  360. range = -1;
  361. for (i = 0; i < nvals; i++) {
  362. if (i == 0 || (vals[i] - list->max[range]) > 1) {
  363. range++;
  364. if (range == list->alloc_ranges) {
  365. list->alloc_ranges += 1000;
  366. list->min = (int *)G_realloc((void *)list->min,
  367. list->alloc_ranges *
  368. sizeof(int));
  369. list->max =
  370. (int *)G_realloc((void *)list->max,
  371. list->alloc_ranges * sizeof(int));
  372. }
  373. list->min[range] = vals[i];
  374. list->max[range] = vals[i];
  375. }
  376. else {
  377. list->max[range] = vals[i];
  378. }
  379. }
  380. list->n_ranges = range + 1;
  381. return (list->n_ranges);
  382. }
  383. /*!
  384. \brief Convert cat_list struct to ordered array of unique integers.
  385. Output array do not contain duplicate items.
  386. Allocated array should be freed by G_free().
  387. \param cat_list pointer to cat_list struct
  388. \param[out] vals array of integers
  389. \param[out] nvals number of values
  390. \return 0 on success
  391. \return -1 on failure
  392. */
  393. int Vect_cat_list_to_array(const struct cat_list *list, int **vals, int *nvals)
  394. {
  395. int i, j, k, n, n_cats, n_ucats, last_cat;
  396. int *cats, *ucats;
  397. G_debug(1, "Vect_cat_list_to_array()");
  398. *nvals = n_cats = 0;
  399. cats = NULL;
  400. for (i = 0; i < list->n_ranges; i++) {
  401. n = list->max[i] - list->min[i] + 1;
  402. if (n < 1)
  403. return -1;
  404. /* realloc array */
  405. cats = (int *) G_realloc(cats, sizeof(int) * (n_cats + n));
  406. for (j = n_cats, k = 0; j < n_cats + n; j++, k++) {
  407. cats[j] = list->min[i] + k;
  408. }
  409. n_cats += n;
  410. }
  411. /* sort array */
  412. qsort(cats, n_cats, sizeof(int), cmp);
  413. /* skip duplicated values */
  414. ucats = G_malloc(sizeof(int) * n_cats);
  415. last_cat = ucats[0] = cats[0];
  416. n_ucats = 1;
  417. for (i = 1; i < n_cats; i++) {
  418. if (last_cat == cats[i])
  419. continue;
  420. last_cat = ucats[n_ucats++] = cats[i];
  421. }
  422. G_free(cats);
  423. /* reallocate array for unique values */
  424. ucats = (int *) G_realloc(ucats, sizeof(int) * n_ucats);
  425. *nvals = n_ucats;
  426. *vals = ucats;
  427. return 0;
  428. }
  429. /*!
  430. \brief Check if category number is in list.
  431. \param cat category number
  432. \param list cat_list structure
  433. \return TRUE if cat is in list
  434. \return FALSE if not
  435. */
  436. int Vect_cat_in_cat_list(int cat, const struct cat_list *list)
  437. {
  438. int i;
  439. for (i = 0; i < list->n_ranges; i++)
  440. if (cat >= list->min[i] && cat <= list->max[i])
  441. return (TRUE);
  442. return (FALSE);
  443. }
  444. /*!
  445. \brief Set category constraints using 'where' or 'cats' option and layer number.
  446. \param Map pointer to Map_info structure
  447. \param layer layer number
  448. \param where where statement
  449. \param catstr category list as string
  450. \return pointer to cat_list structure or NULL
  451. */
  452. struct cat_list *Vect_cats_set_constraint(struct Map_info *Map, int layer,
  453. char *where, char *catstr)
  454. {
  455. struct cat_list *list = NULL;
  456. int ret;
  457. if (layer < 1) {
  458. G_warning(_("Layer number must be > 0 for category constraints"));
  459. /* no valid constraints, all categories qualify */
  460. return list;
  461. }
  462. /* where has precedence over cats */
  463. if (where) {
  464. struct field_info *Fi = NULL;
  465. dbDriver *driver = NULL;
  466. int ncats, *cats = NULL;
  467. int i, j;
  468. if (catstr)
  469. G_warning(_("'%s' and '%s' parameters were supplied, cats will be ignored"), "where", "cats");
  470. Fi = Vect_get_field(Map, layer);
  471. if (!Fi) {
  472. G_fatal_error(_("Database connection not defined for layer %d"),
  473. layer);
  474. }
  475. G_verbose_message(_("Loading categories from table <%s>..."), Fi->table);
  476. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  477. if (driver == NULL)
  478. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  479. Fi->database, Fi->driver);
  480. ncats = db_select_int(driver, Fi->table, Fi->key, where,
  481. &cats);
  482. if (ncats == -1)
  483. G_fatal_error(_("Unable select records from table <%s>"),
  484. Fi->table);
  485. G_verbose_message(n_("One category loaded", "%d categories loaded", ncats), ncats);
  486. db_close_database_shutdown_driver(driver);
  487. /* sort */
  488. qsort(cats, ncats, sizeof(int), cmp);
  489. /* remove duplicates */
  490. j = 1;
  491. for (i = 1; i < ncats; i++) {
  492. if (cats[i] != cats[j - 1]) {
  493. cats[j] = cats[i];
  494. j++;
  495. }
  496. }
  497. ncats = j;
  498. /* convert to cat list */
  499. list = Vect_new_cat_list();
  500. ret = Vect_array_to_cat_list(cats, ncats, list);
  501. if (ret == 0)
  502. G_warning(_("No categories selected with '%s' option"), "where");
  503. if (cats)
  504. G_free(cats);
  505. }
  506. else if (catstr) {
  507. list = Vect_new_cat_list();
  508. ret = Vect_str_to_cat_list(catstr, list);
  509. if (ret > 0)
  510. G_warning(_("%d errors in '%s' option"), ret, "cats");
  511. }
  512. if (list) {
  513. if (list->n_ranges < 1) {
  514. Vect_destroy_cat_list(list);
  515. list = NULL;
  516. }
  517. else
  518. list->field = layer;
  519. }
  520. return list;
  521. }
  522. /*!
  523. \brief Check if categories match with category constraints.
  524. \param Cats line_cats structure
  525. \param layer layer number
  526. \param list cat_list structure
  527. \return 0 no match, categories are outside constraints
  528. \return 1 match, categories are inside constraints
  529. */
  530. /* TODO:
  531. * for GRASS 8, change return type:
  532. * return a list of all category numbers that match the constraints
  533. * return NULL if no category number matches the constraints
  534. */
  535. int Vect_cats_in_constraint(struct line_cats *Cats, int layer,
  536. struct cat_list *list)
  537. {
  538. int i;
  539. if (layer < 1) {
  540. G_warning(_("Layer number must be > 0 for category constraints"));
  541. /* no valid constraint, all categories qualify */
  542. return 1;
  543. }
  544. if (list) {
  545. for (i = 0; i < Cats->n_cats; i++) {
  546. if (Cats->field[i] == layer &&
  547. Vect_cat_in_cat_list(Cats->cat[i], list)) {
  548. return 1;
  549. }
  550. }
  551. return 0;
  552. }
  553. for (i = 0; i < Cats->n_cats; i++) {
  554. if (Cats->field[i] == layer)
  555. return 1;
  556. }
  557. return 0;
  558. }
  559. /*!
  560. \brief Check if category is in ordered array of integers.
  561. \param cat category number
  562. \param array ordered array of integers
  563. \param ncats number of categories in array
  564. \return TRUE if cat is in list
  565. \return FALSE if it is not
  566. */
  567. int Vect_cat_in_array(int cat, const int *array, int ncats)
  568. {
  569. int *i;
  570. i = bsearch((void *)&cat, (void *)array, (size_t) ncats,
  571. sizeof(int), cmp);
  572. return (i != NULL);
  573. }
  574. /* return -1 if *p1 < *p2
  575. * return 1 if *p1 > *p2
  576. * return 0 if *p1 == *p2 */
  577. static int cmp(const void *pa, const void *pb)
  578. {
  579. int *p1 = (int *)pa;
  580. int *p2 = (int *)pb;
  581. if (*p1 < *p2)
  582. return -1;
  583. return (*p1 > *p2);
  584. }