PageRenderTime 66ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/grass-6.4.2/lib/vector/Vlib/array.c

#
C | 373 lines | 195 code | 65 blank | 113 comment | 45 complexity | ab7a44aea958d32ad6ba057fe1c43095 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, 0BSD, BSD-3-Clause
  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. Array must be initialised by Vect_new_varray(size) 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[in,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(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'
  79. If category of object of given type is in 'clist' (category list).
  80. 'type' may be either: GV_AREA or: GV_POINT | GV_LINE | GV_BOUNDARY |
  81. GV_CENTROID
  82. Array is not reset to zero before, but old values (if any > 0) are overwritten.
  83. Array must be initialised by Vect_new_varray(size) 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[in,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(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'.
  171. if category of object of given type is in categories selected from
  172. DB based on where statement (given without where). 'type' may be
  173. either: GV_AREA or: GV_POINT | GV_LINE | GV_BOUNDARY | GV_CENTROID
  174. Array is not reset to zero before, but old values (if any > 0) are
  175. overwritten. Array must be initialised by Vect_new_varray(size) call.
  176. \param Map vector map
  177. \param field layer number
  178. \param where where statement
  179. \param type feature type
  180. \param value value to set up
  181. \param[in,out] varray varray structure to modify
  182. \return number of items set
  183. \return -1 on error
  184. */
  185. int
  186. Vect_set_varray_from_db(struct Map_info *Map, int field, const char *where,
  187. int type, int value, VARRAY * varray)
  188. {
  189. int i, n, c, centr, cat, *cats;
  190. int ncats;
  191. int ni = 0; /* number of items set */
  192. int ltype; /* line type */
  193. struct line_cats *Cats;
  194. struct field_info *Fi;
  195. dbDriver *driver;
  196. G_debug(4, "Vect_set_varray_from_db(): field = %d where = '%s'", field,
  197. where);
  198. /* Note: use category index once available */
  199. /* Check type */
  200. if ((type & GV_AREA) && (type & (GV_POINTS | GV_LINES))) {
  201. G_warning(_("Mixed area and other type requested for vector array"));
  202. return 0;
  203. }
  204. Cats = Vect_new_cats_struct();
  205. /* Select categories from DB to array */
  206. Fi = Vect_get_field(Map, field);
  207. if (Fi == NULL) {
  208. G_warning(_("Database connection not defined for layer %d"), field);
  209. return -1;
  210. }
  211. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  212. if (driver == NULL) {
  213. G_warning(_("Unable to open database <%s> by driver <%s>"),
  214. Fi->database, Fi->driver);
  215. return -1;
  216. }
  217. ncats = db_select_int(driver, Fi->table, Fi->key, where, &cats);
  218. db_close_database_shutdown_driver(driver);
  219. if (ncats == -1) {
  220. G_warning(_("Unable to select record from table <%s> (key %s, where %s)"),
  221. Fi->table, Fi->key, where);
  222. return -1;
  223. }
  224. if (type & GV_AREA) { /* Areas */
  225. n = Vect_get_num_areas(Map);
  226. /* IMHO varray should be allocated only when it's required AND only as large as required
  227. as WHERE will create a small subset of all vector features and thus on large datasets
  228. it's waste of memory to allocate it for all features. */
  229. if (n > varray->size) { /* not enough space */
  230. G_warning(_("Not enough space in vector array"));
  231. return 0;
  232. }
  233. for (i = 1; i <= n; i++) {
  234. centr = Vect_get_area_centroid(Map, i);
  235. if (centr <= 0)
  236. continue; /* No centroid */
  237. Vect_read_line(Map, NULL, Cats, centr);
  238. /*if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
  239. for (c = 0; c < Cats->n_cats; c++) {
  240. if (Cats->field[c] == field &&
  241. in_array(cats, ncats, Cats->cat[c])) {
  242. cat = Cats->cat[c];
  243. varray->c[i] = value;
  244. ni++;
  245. break;
  246. }
  247. }
  248. /*
  249. if ( in_array ( cats, ncats, cat ) ) {
  250. varray->c[i] = value;
  251. ni++;
  252. }
  253. */
  254. }
  255. }
  256. else { /* Lines */
  257. n = Vect_get_num_lines(Map);
  258. if (n > varray->size) { /* not enough space */
  259. G_warning(_("Not enough space in vector array"));
  260. return 0;
  261. }
  262. for (i = 1; i <= n; i++) {
  263. ltype = Vect_read_line(Map, NULL, Cats, i);
  264. if (!(ltype & type))
  265. continue; /* is not specified type */
  266. /* if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
  267. for (c = 0; c < Cats->n_cats; c++) {
  268. if (Cats->field[c] == field &&
  269. in_array(cats, ncats, Cats->cat[c])) {
  270. cat = Cats->cat[c];
  271. varray->c[i] = value;
  272. ni++;
  273. break;
  274. }
  275. }
  276. /*
  277. if ( in_array ( cats, ncats, cat ) ) {
  278. varray->c[i] = value;
  279. ni++;
  280. }
  281. */
  282. }
  283. }
  284. G_free(cats);
  285. Vect_destroy_cats_struct(Cats);
  286. return ni;
  287. }