array.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*!
  2. \file array.c
  3. \brief Vector library - array structure
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 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. \date 2001-2008
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/dbmi.h>
  16. #include <grass/Vect.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'.
  45. If category of object of given type is in 'cstring' (string
  46. representing category list like: '1,3,5-7'). 'type' may be either:
  47. 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 overwritten.
  49. \param Map vector map
  50. \param field layer number
  51. \param cstring pointer to string with categories
  52. \param type feature type
  53. \param value value to set up
  54. \param[in,out] varray varray structure to modify
  55. \return number of items set
  56. \return -1 on error
  57. */
  58. int
  59. Vect_set_varray_from_cat_string(struct Map_info *Map, int field,
  60. const char *cstring, int type, int value,
  61. VARRAY * varray)
  62. {
  63. int ret;
  64. struct cat_list *Clist;
  65. G_debug(4, "Vect_set_varray_from_cat_string(): cstring = '%s'", cstring);
  66. Clist = Vect_new_cat_list();
  67. ret = Vect_str_to_cat_list(cstring, Clist);
  68. if (ret > 0)
  69. G_warning(_("%d errors in category string."), ret);
  70. G_debug(4, " %d ranges in clist", Clist->n_ranges);
  71. ret =
  72. Vect_set_varray_from_cat_list(Map, field, Clist, type, value, varray);
  73. Vect_destroy_cat_list(Clist);
  74. return ret;
  75. }
  76. /*!
  77. \brief Set values in 'varray' to 'value'
  78. If category of object of given type is in 'clist' (category list).
  79. 'type' may be either: GV_AREA or: GV_POINT | GV_LINE | GV_BOUNDARY |
  80. GV_CENTROID
  81. Array is not reset to zero before, but old values (if any > 0) are overwritten.
  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[in,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(struct Map_info *Map, int field,
  93. struct cat_list *clist, int type, int value,
  94. 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'.
  169. if category of object of given type is in categories selected from
  170. DB based on where statement (given without where). 'type' may be
  171. either: GV_AREA or: GV_POINT | GV_LINE | GV_BOUNDARY | GV_CENTROID
  172. Array is not reset to zero before, but old values (if any > 0) are
  173. overwritten.
  174. \param Map vector map
  175. \param field layer number
  176. \param where where statement
  177. \param type feature type
  178. \param value value to set up
  179. \param[in,out] varray varray structure to modify
  180. \return number of items set
  181. \return -1 on error
  182. */
  183. int
  184. Vect_set_varray_from_db(struct Map_info *Map, int field, const char *where,
  185. int type, int value, VARRAY * varray)
  186. {
  187. int i, n, c, centr, cat, *cats;
  188. int ncats;
  189. int ni = 0; /* number of items set */
  190. int ltype; /* line type */
  191. struct line_cats *Cats;
  192. struct field_info *Fi;
  193. dbDriver *driver;
  194. G_debug(4, "Vect_set_varray_from_db(): field = %d where = '%s'", field,
  195. where);
  196. /* Note: use category index once available */
  197. /* Check type */
  198. if ((type & GV_AREA) && (type & (GV_POINTS | GV_LINES))) {
  199. G_warning(_("Mixed area and other type requested for vector array"));
  200. return 0;
  201. }
  202. Cats = Vect_new_cats_struct();
  203. /* Select categories from DB to array */
  204. Fi = Vect_get_field(Map, field);
  205. if (Fi == NULL) {
  206. G_warning(_("Database connection not defined for layer %d"), field);
  207. return -1;
  208. }
  209. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  210. if (driver == NULL) {
  211. G_warning(_("Unable to open database <%s> by driver <%s>"),
  212. Fi->database, Fi->driver);
  213. return -1;
  214. }
  215. ncats = db_select_int(driver, Fi->table, Fi->key, where, &cats);
  216. db_close_database_shutdown_driver(driver);
  217. if (ncats == -1) {
  218. G_warning(_("Unable to select record from table <%s> (key %s, where %s)"),
  219. Fi->table, Fi->key, where);
  220. return -1;
  221. }
  222. if (type & GV_AREA) { /* Areas */
  223. n = Vect_get_num_areas(Map);
  224. if (n > varray->size) { /* not enough space */
  225. G_warning(_("Not enough space in vector array"));
  226. return 0;
  227. }
  228. for (i = 1; i <= n; i++) {
  229. centr = Vect_get_area_centroid(Map, i);
  230. if (centr <= 0)
  231. continue; /* No centroid */
  232. Vect_read_line(Map, NULL, Cats, centr);
  233. /*if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
  234. for (c = 0; c < Cats->n_cats; c++) {
  235. if (Cats->field[c] == field &&
  236. in_array(cats, ncats, Cats->cat[c])) {
  237. cat = Cats->cat[c];
  238. varray->c[i] = value;
  239. ni++;
  240. break;
  241. }
  242. }
  243. /*
  244. if ( in_array ( cats, ncats, cat ) ) {
  245. varray->c[i] = value;
  246. ni++;
  247. }
  248. */
  249. }
  250. }
  251. else { /* Lines */
  252. n = Vect_get_num_lines(Map);
  253. if (n > varray->size) { /* not enough space */
  254. G_warning(_("Not enough space in vector array"));
  255. return 0;
  256. }
  257. for (i = 1; i <= n; i++) {
  258. ltype = Vect_read_line(Map, NULL, Cats, i);
  259. if (!(ltype & type))
  260. continue; /* is not specified type */
  261. /* if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
  262. for (c = 0; c < Cats->n_cats; c++) {
  263. if (Cats->field[c] == field &&
  264. in_array(cats, ncats, Cats->cat[c])) {
  265. cat = Cats->cat[c];
  266. varray->c[i] = value;
  267. ni++;
  268. break;
  269. }
  270. }
  271. /*
  272. if ( in_array ( cats, ncats, cat ) ) {
  273. varray->c[i] = value;
  274. ni++;
  275. }
  276. */
  277. }
  278. }
  279. G_free(cats);
  280. Vect_destroy_cats_struct(Cats);
  281. return ni;
  282. }