cats.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*!
  2. * \file cats.c
  3. *
  4. * \brief Vector library - Category management
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2001-2009 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
  14. * Higgins
  15. * \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  16. * \author Various updates by Martin Landa <landa.martin gmail.com>
  17. */
  18. #include <grass/config.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/vector.h>
  23. #include <grass/glocale.h>
  24. static int cmp(const void *pa, const void *pb);
  25. struct line_cats *Vect__new_cats_struct(void);
  26. /*!
  27. \brief Creates and initializes line_cats structure.
  28. This structure is used for reading and writing vector cats. The
  29. library routines handle all memory allocation.
  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. 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] 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 found
  119. \param Cats line_cats structure
  120. \param field layer number
  121. \param[out] cat pointer to variable where cat will be written (can be NULL)
  122. \return 1 found
  123. \return 0 layer does not exist
  124. */
  125. int Vect_cat_get(const struct line_cats *Cats, int field, int *cat)
  126. {
  127. int n;
  128. /* check input value */
  129. /*
  130. if (field < 1 || field > GV_FIELD_MAX)
  131. return (0);
  132. */
  133. if (cat)
  134. *cat = -1;
  135. /* go through cats and find if field exist */
  136. for (n = 0; n < Cats->n_cats; n++) {
  137. if (Cats->field[n] == field) {
  138. if (cat)
  139. *cat = Cats->cat[n];
  140. return 1;
  141. }
  142. }
  143. /* field was not found */
  144. return 0;
  145. }
  146. /*!
  147. \brief Get list of categories of given field.
  148. \param Cats line_cats structure
  149. \param field layer number
  150. \param[out] cats pointer to list where cats will be written
  151. \return number of found categories
  152. \return -1 on invalid field
  153. */
  154. int Vect_field_cat_get(const struct line_cats *Cats, int field, struct ilist *cats)
  155. {
  156. int n;
  157. /* reset list of categories */
  158. Vect_reset_list(cats);
  159. /* check input value */
  160. if (field < 1 || field > GV_FIELD_MAX)
  161. return -1;
  162. /* go through cats and find if field exist */
  163. for (n = 0; n < Cats->n_cats; n++) {
  164. if (Cats->field[n] != field)
  165. continue;
  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 1 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 = 0;
  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. for (n = 0; n < Cats->n_cats; n++) {
  187. if (Cats->field[n] == field) {
  188. for (m = n; m < Cats->n_cats - 1; m++) {
  189. Cats->field[m] = Cats->field[m + 1];
  190. Cats->cat[m] = Cats->cat[m + 1];
  191. }
  192. Cats->n_cats--;
  193. found = 1;
  194. n--; /* check again this position */
  195. }
  196. }
  197. return (found);
  198. }
  199. /*!
  200. \brief Delete field/cat from line_cats structure
  201. \param[in,out] Cats line_cats structure
  202. \param field layer number
  203. \param cat category to be deleted or -1 to delete all cats of given field
  204. \return 1 deleted
  205. \return 0 field/category number does not exist
  206. */
  207. int Vect_field_cat_del(struct line_cats *Cats, int field, int cat)
  208. {
  209. register int n, m, found = 0;
  210. /* check input value */
  211. /*
  212. if (field < 1 || field > GV_FIELD_MAX)
  213. return (0);
  214. */
  215. /* go through cats and find if field exist */
  216. for (n = 0; n < Cats->n_cats; n++) {
  217. if (Cats->field[n] == field && (Cats->cat[n] == cat || cat == -1)) {
  218. for (m = n; m < Cats->n_cats - 1; m++) {
  219. Cats->field[m] = Cats->field[m + 1];
  220. Cats->cat[m] = Cats->cat[m + 1];
  221. }
  222. Cats->n_cats--;
  223. found = 1;
  224. }
  225. }
  226. return (found);
  227. }
  228. /*!
  229. \brief Reset category structure to make sure cats structure is clean to be re-used.
  230. I.e. it has no cats associated with it. Cats must have
  231. previously been created with Vect_new_cats_struct()
  232. \param[out] Cats line_cats structure
  233. \return 0
  234. */
  235. int Vect_reset_cats(struct line_cats *Cats)
  236. {
  237. Cats->n_cats = 0;
  238. return 0;
  239. }
  240. /*!
  241. \brief Allocate memory for cat_list structure.
  242. \return poiter to allocated structure
  243. \return NULL on out of memory
  244. */
  245. struct cat_list *Vect_new_cat_list()
  246. {
  247. struct cat_list *p;
  248. p = (struct cat_list *)G_malloc(sizeof(struct cat_list));
  249. /* n_ranges MUST be initialized to zero */
  250. if (p) {
  251. p->n_ranges = 0;
  252. p->alloc_ranges = 0;
  253. p->field = 0;
  254. p->min = NULL;
  255. p->max = NULL;
  256. }
  257. return p;
  258. }
  259. /*!
  260. \brief Frees allocated cat_list memory.
  261. \param p pointer to line_cats structure
  262. */
  263. void Vect_destroy_cat_list(struct cat_list *p)
  264. {
  265. if (p) { /* probably a moot test */
  266. if (p->n_ranges) {
  267. G_free((void *)p->min);
  268. G_free((void *)p->max);
  269. }
  270. G_free((void *)p);
  271. }
  272. }
  273. /*!
  274. \brief Convert string of categories and cat ranges separated by commas to cat_list.
  275. Examples of string: 2,3,5-9,20. str - input string
  276. \param[in] str cat list string
  277. \param[out] list result cat_list structure
  278. \return number of errors in ranges
  279. */
  280. int Vect_str_to_cat_list(const char *str, struct cat_list *list)
  281. {
  282. int i, nr, l, err = 0;
  283. const char *s, *e;
  284. char buf[100];
  285. int min, max;
  286. G_debug(3, "Vect_str_to_cat_list(): str = %s", str);
  287. list->n_ranges = 0;
  288. l = strlen(str);
  289. /* find number of ranges */
  290. nr = 1; /* one range */
  291. for (i = 0; i < l; i++)
  292. if (str[i] == ',')
  293. nr++;
  294. /* allocate space */
  295. if (list->alloc_ranges == 0) {
  296. list->min = (int *)G_malloc(nr * sizeof(int));
  297. list->max = (int *)G_malloc(nr * sizeof(int));
  298. }
  299. else if (nr > list->alloc_ranges) {
  300. list->min = (int *)G_realloc((void *)list->min, nr * sizeof(int));
  301. list->max = (int *)G_realloc((void *)list->max, nr * sizeof(int));
  302. }
  303. /* go through string and read ranges */
  304. i = 0;
  305. s = str;
  306. while (s) {
  307. e = (char *)strchr(s, ','); /* first comma */
  308. if (e) {
  309. l = e - s;
  310. strncpy(buf, s, l);
  311. buf[l] = '\0';
  312. s = e + 1;
  313. }
  314. else {
  315. strcpy(buf, s);
  316. s = NULL;
  317. }
  318. G_debug(3, " buf = %s", buf);
  319. if (sscanf(buf, "%d-%d", &min, &max) == 2) {
  320. }
  321. else if (sscanf(buf, "%d", &min) == 1)
  322. max = min;
  323. else { /* error */
  324. G_warning(_("Unable to convert category string '%s' (from '%s') to category range"),
  325. buf, str);
  326. err++;
  327. continue;
  328. }
  329. list->min[i] = min;
  330. list->max[i] = max;
  331. i++;
  332. }
  333. list->n_ranges = i;
  334. return (err);
  335. }
  336. /*!
  337. \brief Convert ordered array of integers to cat_list structure.
  338. \param vals array of integers
  339. \param nvals number of values
  340. \param[out] list result cat_list structure
  341. \return number of ranges
  342. */
  343. int Vect_array_to_cat_list(const int *vals, int nvals, struct cat_list *list)
  344. {
  345. int i, range;
  346. G_debug(1, "Vect_array_to_cat_list()");
  347. range = -1;
  348. for (i = 0; i < nvals; i++) {
  349. if (i == 0 || (vals[i] - list->max[range]) > 1) {
  350. range++;
  351. if (range == list->alloc_ranges) {
  352. list->alloc_ranges += 1000;
  353. list->min = (int *)G_realloc((void *)list->min,
  354. list->alloc_ranges *
  355. sizeof(int));
  356. list->max =
  357. (int *)G_realloc((void *)list->max,
  358. list->alloc_ranges * sizeof(int));
  359. }
  360. list->min[range] = vals[i];
  361. list->max[range] = vals[i];
  362. }
  363. else {
  364. list->max[range] = vals[i];
  365. }
  366. }
  367. list->n_ranges = range + 1;
  368. return (list->n_ranges);
  369. }
  370. /*!
  371. \brief Check if category number is in list.
  372. \param cat category number
  373. \param list cat_list structure
  374. \return TRUE if cat is in list
  375. \return FALSE if it is not
  376. */
  377. int Vect_cat_in_cat_list(int cat, const struct cat_list *list)
  378. {
  379. int i;
  380. for (i = 0; i < list->n_ranges; i++)
  381. if (cat >= list->min[i] && cat <= list->max[i])
  382. return (TRUE);
  383. return (FALSE);
  384. }
  385. /*!
  386. \brief Check if category is in ordered array of integers.
  387. \param cat category number
  388. \param array ordered array of integers
  389. \param ncats number of categories in array
  390. \return TRUE if cat is in list
  391. \return FALSE if it is not
  392. */
  393. int Vect_cat_in_array(int cat, const int *array, int ncats)
  394. {
  395. int *i;
  396. i = bsearch((void *)&cat, (void *)array, (size_t) ncats,
  397. sizeof(int), cmp);
  398. if (i != NULL)
  399. return (TRUE);
  400. return (FALSE);
  401. }
  402. static int cmp(const void *pa, const void *pb)
  403. {
  404. int *p1 = (int *)pa;
  405. int *p2 = (int *)pb;
  406. if (*p1 < *p2)
  407. return -1;
  408. if (*p1 > *p2)
  409. return 1;
  410. return 0;
  411. }