array.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*!
  2. \file lib/vector/Vlib/array.c
  3. \brief Vector library - category array
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Radim Blazek
  11. */
  12. #include <stdlib.h>
  13. #include <grass/dbmi.h>
  14. #include <grass/vector.h>
  15. #include <grass/glocale.h>
  16. /* function prototypes */
  17. static int cmp(const void *pa, const void *pb);
  18. static int in_array(int *cats, size_t ncats, int cat);
  19. /*!
  20. \brief Create new struct varray and allocate space for given number of items.
  21. Space allocated is 'size + 1' so that lines are accessed by line id.
  22. Array values are set to 0.
  23. \param size size of array
  24. \return pointer to new struct varray
  25. \return NULL if failed
  26. */
  27. struct varray *Vect_new_varray(int size)
  28. {
  29. struct varray *p;
  30. p = (struct varray *) G_malloc(sizeof(struct varray));
  31. if (p == NULL)
  32. return NULL;
  33. p->size = size;
  34. p->c = (int *)G_calloc(sizeof(char) * size + 1, sizeof(int));
  35. if (p->c == NULL) {
  36. G_free(p);
  37. return NULL;
  38. }
  39. return p;
  40. }
  41. /*!
  42. \brief Set values in 'varray' to 'value' from category string.
  43. If category of object of given type is in <em>cstring</em> (string
  44. representing category list like: '1,3,5-7'). <em>type</em> may be
  45. either: GV_AREA or: GV_POINT | GV_LINE | GV_BOUNDARY | GV_CENTROID
  46. Array is not reset to zero before, but old values (if any > 0) are
  47. overwritten. Array must be initialised by Vect_new_varray() call.
  48. \param Map vector map
  49. \param field layer number
  50. \param cstring pointer to string with categories
  51. \param type feature type
  52. \param value value to set up
  53. \param[out] varray varray structure to modify
  54. \return number of items set
  55. \return -1 on error
  56. */
  57. int
  58. Vect_set_varray_from_cat_string(const struct Map_info *Map, int field,
  59. const char *cstring, int type, int value,
  60. struct varray * varray)
  61. {
  62. int ret;
  63. struct cat_list *Clist;
  64. G_debug(4, "Vect_set_varray_from_cat_string(): cstring = '%s'", cstring);
  65. Clist = Vect_new_cat_list();
  66. ret = Vect_str_to_cat_list(cstring, Clist);
  67. if (ret > 0)
  68. G_warning(_("%d errors in category string"), ret);
  69. G_debug(4, " %d ranges in clist", Clist->n_ranges);
  70. ret =
  71. Vect_set_varray_from_cat_list(Map, field, Clist, type, value, varray);
  72. Vect_destroy_cat_list(Clist);
  73. return ret;
  74. }
  75. /*!
  76. \brief Set values in 'varray' to 'value' from category list
  77. If category of object of given type is in <em>clist</em> (category
  78. list). <em>type</em> may be either: GV_AREA or: GV_POINT | GV_LINE
  79. | GV_BOUNDARY | GV_CENTROID
  80. Array is not reset to zero before, but old values (if any > 0) are
  81. overwritten. Array must be initialised by Vect_new_varray() call.
  82. \param Map vector map
  83. \param field layer number
  84. \param clist list of categories
  85. \param type feature type
  86. \param value value to set up
  87. \param[out] varray varray structure to modify
  88. \return number of items set
  89. \return -1 on error
  90. */
  91. int
  92. Vect_set_varray_from_cat_list(const struct Map_info *Map, int field,
  93. struct cat_list *clist, int type, int value,
  94. struct varray * varray)
  95. {
  96. int i, n, centr, cat;
  97. int ni = 0; /* number of items set */
  98. int ltype; /* line type */
  99. struct line_cats *Cats;
  100. G_debug(4, "Vect_set_varray_from_cat_list(): field = %d", field);
  101. /* Check type */
  102. if ((type & GV_AREA) && (type & (GV_POINTS | GV_LINES))) {
  103. G_warning(_("Mixed area and other type requested for vector array"));
  104. return 0;
  105. }
  106. Cats = Vect_new_cats_struct();
  107. if (type & GV_AREA) { /* Areas */
  108. n = Vect_get_num_areas(Map);
  109. if (n > varray->size) { /* not enough space */
  110. G_warning(_("Not enough space in vector array"));
  111. return 0;
  112. }
  113. for (i = 1; i <= n; i++) {
  114. centr = Vect_get_area_centroid(Map, i);
  115. if (centr <= 0)
  116. continue; /* No centroid */
  117. Vect_read_line(Map, NULL, Cats, centr);
  118. if (!Vect_cat_get(Cats, field, &cat))
  119. continue; /* No such field */
  120. if (Vect_cat_in_cat_list(cat, clist)) { /* cat is in list */
  121. varray->c[i] = value;
  122. ni++;
  123. }
  124. }
  125. }
  126. else { /* Lines */
  127. n = Vect_get_num_lines(Map);
  128. if (n > varray->size) { /* not enough space */
  129. G_warning(_("Not enough space in vector array"));
  130. return 0;
  131. }
  132. for (i = 1; i <= n; i++) {
  133. ltype = Vect_read_line(Map, NULL, Cats, i);
  134. if (!(ltype & type))
  135. continue; /* is not specified type */
  136. if (!Vect_cat_get(Cats, field, &cat))
  137. continue; /* No such field */
  138. if (Vect_cat_in_cat_list(cat, clist)) { /* cat is in list */
  139. varray->c[i] = value;
  140. ni++;
  141. }
  142. }
  143. }
  144. Vect_destroy_cats_struct(Cats);
  145. return ni;
  146. }
  147. /* compare 2 integers in array */
  148. static int cmp(const void *pa, const void *pb)
  149. {
  150. int *p1 = (int *)pa;
  151. int *p2 = (int *)pb;
  152. if (*p1 < *p2)
  153. return -1;
  154. if (*p1 > *p2)
  155. return 1;
  156. return 0;
  157. }
  158. /* check if cat is in array */
  159. static int in_array(int *cats, size_t ncats, int cat)
  160. {
  161. int *p;
  162. p = (int *)bsearch((void *)&cat, cats, ncats, sizeof(int), cmp);
  163. if (p == NULL)
  164. return 0;
  165. return 1;
  166. }
  167. /*!
  168. \brief Set values in 'varray' to 'value' from DB (where statement)
  169. I category of object of given type is in categories selected from
  170. DB based on where statement (given without where). <em>type</em>
  171. may be either: GV_AREA or: GV_POINT | GV_LINE | GV_BOUNDARY |
  172. GV_CENTROID
  173. Array is not reset to zero before, but old values (if any > 0) are
  174. overwritten. Array must be initialised by Vect_new_varray() call.
  175. \param Map vector map
  176. \param field layer number
  177. \param where where statement
  178. \param type feature type
  179. \param value value to set up
  180. \param[out] varray varray structure to modify
  181. \return number of items set
  182. \return -1 on error
  183. */
  184. int
  185. Vect_set_varray_from_db(const struct Map_info *Map, int field, const char *where,
  186. int type, int value, struct varray * varray)
  187. {
  188. int i, n, c, centr, *cats;
  189. int ncats;
  190. int ni = 0; /* number of items set */
  191. int ltype; /* line type */
  192. struct line_cats *Cats;
  193. struct field_info *Fi;
  194. dbDriver *driver;
  195. G_debug(4, "Vect_set_varray_from_db(): field = %d where = '%s'", field,
  196. where);
  197. /* Note: use category index once available */
  198. /* Check type */
  199. if ((type & GV_AREA) && (type & (GV_POINTS | GV_LINES))) {
  200. G_warning(_("Mixed area and other type requested for vector array"));
  201. return 0;
  202. }
  203. Cats = Vect_new_cats_struct();
  204. /* Select categories from DB to array */
  205. Fi = Vect_get_field(Map, field);
  206. if (Fi == NULL) {
  207. G_warning(_("Database connection not defined for layer %d"), field);
  208. return -1;
  209. }
  210. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  211. if (driver == NULL) {
  212. G_warning(_("Unable to open database <%s> by driver <%s>"),
  213. Fi->database, Fi->driver);
  214. return -1;
  215. }
  216. ncats = db_select_int(driver, Fi->table, Fi->key, where, &cats);
  217. db_close_database_shutdown_driver(driver);
  218. if (ncats == -1) {
  219. G_warning(_("Unable to select record from table <%s> (key %s, where %s)"),
  220. Fi->table, Fi->key, where);
  221. return -1;
  222. }
  223. if (type & GV_AREA) { /* Areas */
  224. n = Vect_get_num_areas(Map);
  225. /* IMHO varray should be allocated only when it's required AND only as large as required
  226. as WHERE will create a small subset of all vector features and thus on large datasets
  227. it's waste of memory to allocate it for all features. */
  228. if (n > varray->size) { /* not enough space */
  229. G_warning(_("Not enough space in vector array"));
  230. return 0;
  231. }
  232. for (i = 1; i <= n; i++) {
  233. centr = Vect_get_area_centroid(Map, i);
  234. if (centr <= 0)
  235. continue; /* No centroid */
  236. Vect_read_line(Map, NULL, Cats, centr);
  237. /*if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
  238. for (c = 0; c < Cats->n_cats; c++) {
  239. if (Cats->field[c] == field &&
  240. in_array(cats, ncats, Cats->cat[c])) {
  241. varray->c[i] = value;
  242. ni++;
  243. break;
  244. }
  245. }
  246. /*
  247. if ( in_array ( cats, ncats, cat ) ) {
  248. varray->c[i] = value;
  249. ni++;
  250. }
  251. */
  252. }
  253. }
  254. else { /* Lines */
  255. n = Vect_get_num_lines(Map);
  256. if (n > varray->size) { /* not enough space */
  257. G_warning(_("Not enough space in vector array"));
  258. return 0;
  259. }
  260. for (i = 1; i <= n; i++) {
  261. ltype = Vect_read_line(Map, NULL, Cats, i);
  262. if (!(ltype & type))
  263. continue; /* is not specified type */
  264. /* if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
  265. for (c = 0; c < Cats->n_cats; c++) {
  266. if (Cats->field[c] == field &&
  267. in_array(cats, ncats, Cats->cat[c])) {
  268. varray->c[i] = value;
  269. ni++;
  270. break;
  271. }
  272. }
  273. /*
  274. if ( in_array ( cats, ncats, cat ) ) {
  275. varray->c[i] = value;
  276. ni++;
  277. }
  278. */
  279. }
  280. }
  281. G_free(cats);
  282. Vect_destroy_cats_struct(Cats);
  283. return ni;
  284. }