array.c 8.9 KB

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