cats.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. p->n_ranges = 0;
  253. p->alloc_ranges = 0;
  254. p->field = 0;
  255. p->min = NULL;
  256. p->max = NULL;
  257. }
  258. return p;
  259. }
  260. /*!
  261. \brief Frees allocated cat_list memory.
  262. \param p pointer to line_cats structure
  263. */
  264. void Vect_destroy_cat_list(struct cat_list *p)
  265. {
  266. if (p) { /* probably a moot test */
  267. if (p->n_ranges) {
  268. G_free((void *)p->min);
  269. G_free((void *)p->max);
  270. }
  271. G_free((void *)p);
  272. }
  273. }
  274. /*!
  275. \brief Converts string of categories and cat ranges separated by commas to cat_list.
  276. \par Examples of string:
  277. \verbatim
  278. 5,6,7
  279. 3-9
  280. 2,3,5-9,20\endverbatim
  281. \par Example:
  282. \code
  283. ...
  284. str = "2,3,5-9,20"
  285. cat_list = Vect_new_cat_list()
  286. Vect_str_to_cat_list(str, cat_list)
  287. \endcode
  288. \verbatim
  289. cat_list->field = 0
  290. cat_list->n_ranges = 4
  291. cat_list->min = {2, 3, 5, 20}
  292. cat_list->max = {2, 3, 9, 20}\endverbatim
  293. \param[in] str category list as a string
  294. \param[in,out] list pointer to cat_list structure
  295. \return number of errors in ranges
  296. */
  297. int Vect_str_to_cat_list(const char *str, struct cat_list *list)
  298. {
  299. int i, nr, l, err = 0;
  300. const char *s, *e;
  301. char buf[100];
  302. int min, max;
  303. G_debug(3, "Vect_str_to_cat_list(): str = %s", str);
  304. list->n_ranges = 0;
  305. l = strlen(str);
  306. /* find number of ranges */
  307. nr = 1; /* one range */
  308. for (i = 0; i < l; i++)
  309. if (str[i] == ',')
  310. nr++;
  311. /* allocate space */
  312. if (list->alloc_ranges == 0) {
  313. list->min = (int *)G_malloc(nr * sizeof(int));
  314. list->max = (int *)G_malloc(nr * sizeof(int));
  315. }
  316. else if (nr > list->alloc_ranges) {
  317. list->min = (int *)G_realloc((void *)list->min, nr * sizeof(int));
  318. list->max = (int *)G_realloc((void *)list->max, nr * sizeof(int));
  319. }
  320. /* go through string and read ranges */
  321. i = 0;
  322. s = str;
  323. while (s) {
  324. e = (char *)strchr(s, ','); /* first comma */
  325. if (e) {
  326. l = e - s;
  327. strncpy(buf, s, l);
  328. buf[l] = '\0';
  329. s = e + 1;
  330. }
  331. else {
  332. strcpy(buf, s);
  333. s = NULL;
  334. }
  335. G_debug(3, " buf = %s", buf);
  336. if (sscanf(buf, "%d-%d", &min, &max) == 2) {
  337. }
  338. else if (sscanf(buf, "%d", &min) == 1)
  339. max = min;
  340. else { /* error */
  341. G_warning(_("Unable to convert category string '%s' (from '%s') to category range"),
  342. buf, str);
  343. err++;
  344. continue;
  345. }
  346. list->min[i] = min;
  347. list->max[i] = max;
  348. i++;
  349. }
  350. list->n_ranges = i;
  351. return (err);
  352. }
  353. /*!
  354. \brief Convert ordered array of integers to cat_list structure.
  355. \param vals array of integers
  356. \param nvals number of values
  357. \param[in,out] list pointer to cat_list structure
  358. \return number of ranges
  359. */
  360. int Vect_array_to_cat_list(const int *vals, int nvals, struct cat_list *list)
  361. {
  362. int i, range;
  363. G_debug(1, "Vect_array_to_cat_list()");
  364. range = -1;
  365. for (i = 0; i < nvals; i++) {
  366. if (i == 0 || (vals[i] - list->max[range]) > 1) {
  367. range++;
  368. if (range == list->alloc_ranges) {
  369. list->alloc_ranges += 1000;
  370. list->min = (int *)G_realloc((void *)list->min,
  371. list->alloc_ranges *
  372. sizeof(int));
  373. list->max =
  374. (int *)G_realloc((void *)list->max,
  375. list->alloc_ranges * sizeof(int));
  376. }
  377. list->min[range] = vals[i];
  378. list->max[range] = vals[i];
  379. }
  380. else {
  381. list->max[range] = vals[i];
  382. }
  383. }
  384. list->n_ranges = range + 1;
  385. return (list->n_ranges);
  386. }
  387. /*!
  388. \brief Check if category number is in list.
  389. \param cat category number
  390. \param list cat_list structure
  391. \return TRUE if cat is in list
  392. \return FALSE if not
  393. */
  394. int Vect_cat_in_cat_list(int cat, const struct cat_list *list)
  395. {
  396. int i;
  397. for (i = 0; i < list->n_ranges; i++)
  398. if (cat >= list->min[i] && cat <= list->max[i])
  399. return (TRUE);
  400. return (FALSE);
  401. }
  402. /*!
  403. \brief Set category constraints using 'where' or 'cats' option and layer number.
  404. \param Map pointer to Map_info structure
  405. \param layer layer number
  406. \param where where statement
  407. \param catstr category list as string
  408. \return pointer to cat_list structure or NULL
  409. */
  410. struct cat_list *Vect_cats_set_constraint(struct Map_info *Map, int layer,
  411. char *where, char *catstr)
  412. {
  413. struct cat_list *list = NULL;
  414. int ret;
  415. if (layer < 1) {
  416. G_warning(_("Layer number must be > 0 for category constraints"));
  417. /* no valid constraints, all categories qualify */
  418. return list;
  419. }
  420. /* where has precedence over cats */
  421. if (where) {
  422. struct field_info *Fi = NULL;
  423. dbDriver *driver = NULL;
  424. int ncats, *cats = NULL;
  425. int i, j;
  426. if (catstr)
  427. G_warning(_("'%s' and '%s' parameters were supplied, cats will be ignored"), "where", "cats");
  428. Fi = Vect_get_field(Map, layer);
  429. if (!Fi) {
  430. G_fatal_error(_("Database connection not defined for layer %d"),
  431. layer);
  432. }
  433. G_verbose_message(_("Loading categories from table <%s>..."), Fi->table);
  434. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  435. if (driver == NULL)
  436. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  437. Fi->database, Fi->driver);
  438. ncats = db_select_int(driver, Fi->table, Fi->key, where,
  439. &cats);
  440. if (ncats == -1)
  441. G_fatal_error(_("Unable select records from table <%s>"),
  442. Fi->table);
  443. G_verbose_message(_("%d categories loaded"), ncats);
  444. db_close_database_shutdown_driver(driver);
  445. /* sort */
  446. qsort(cats, ncats, sizeof(int), cmp);
  447. /* remove duplicates */
  448. j = 1;
  449. for (i = 1; i < ncats; i++) {
  450. if (cats[i] != cats[j - 1]) {
  451. cats[j] = cats[i];
  452. j++;
  453. }
  454. }
  455. ncats = j;
  456. /* convert to cat list */
  457. list = Vect_new_cat_list();
  458. ret = Vect_array_to_cat_list(cats, ncats, list);
  459. if (ret > 0)
  460. G_warning(_("%d errors in '%s' option"), ret, "where");
  461. if (cats)
  462. G_free(cats);
  463. }
  464. else if (catstr) {
  465. list = Vect_new_cat_list();
  466. ret = Vect_str_to_cat_list(catstr, list);
  467. if (ret > 0)
  468. G_warning(_("%d errors in '%s' option"), ret, "cats");
  469. }
  470. if (list) {
  471. if (list->n_ranges < 1) {
  472. Vect_destroy_cat_list(list);
  473. list = NULL;
  474. }
  475. }
  476. return list;
  477. }
  478. /*!
  479. \brief Check if categories match with category constraints.
  480. \param Cats line_cats structure
  481. \param layer layer number
  482. \param list cat_list structure
  483. \return 0 no match, categories are outside constraints
  484. \return 1 match, categories are inside constraints
  485. */
  486. int Vect_cats_in_constraint(struct line_cats *Cats, int layer,
  487. struct cat_list *list)
  488. {
  489. int i;
  490. if (layer < 1) {
  491. G_warning(_("Layer number must be > 0 for category constraints"));
  492. /* no valid constraint, all categories qualify */
  493. return 1;
  494. }
  495. if (list) {
  496. for (i = 0; i < Cats->n_cats; i++) {
  497. if (Cats->field[i] == layer &&
  498. Vect_cat_in_cat_list(Cats->cat[i], list)) {
  499. return 1;
  500. }
  501. }
  502. return 0;
  503. }
  504. for (i = 0; i < Cats->n_cats; i++) {
  505. if (Cats->field[i] == layer)
  506. return 1;
  507. }
  508. return 0;
  509. }
  510. /*!
  511. \brief Check if category is in ordered array of integers.
  512. \param cat category number
  513. \param array ordered array of integers
  514. \param ncats number of categories in array
  515. \return TRUE if cat is in list
  516. \return FALSE if it is not
  517. */
  518. int Vect_cat_in_array(int cat, const int *array, int ncats)
  519. {
  520. int *i;
  521. i = bsearch((void *)&cat, (void *)array, (size_t) ncats,
  522. sizeof(int), cmp);
  523. return (i != NULL);
  524. }
  525. /* return -1 if *p1 < *p2
  526. * return 1 if *p1 > *p2
  527. * return 0 if *p1 == *p2 */
  528. static int cmp(const void *pa, const void *pb)
  529. {
  530. int *p1 = (int *)pa;
  531. int *p2 = (int *)pb;
  532. if (*p1 < *p2)
  533. return -1;
  534. return (*p1 > *p2);
  535. }