vect2rast.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <string.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/dbmi.h>
  5. #include <grass/vector.h>
  6. #include <grass/glocale.h>
  7. #include "local.h"
  8. int vect_to_rast(const char *vector_map, const char *raster_map, const char *field_name,
  9. const char *column, int cache_mb, int use, double value,
  10. int value_type, const char *rgbcolumn, const char *labelcolumn,
  11. int ftype, char *where, char *cats, int dense)
  12. {
  13. struct Map_info Map;
  14. struct line_pnts *Points;
  15. int i, field;
  16. struct cat_list *cat_list = NULL;
  17. int fd; /* for raster map */
  18. int nareas, nlines; /* number of converted features */
  19. int nareas_all, nplines_all; /* number of all areas, points/lines */
  20. int stat;
  21. int format;
  22. int pass, npasses;
  23. /* Attributes */
  24. int nrec;
  25. int ctype;
  26. struct field_info *Fi;
  27. dbDriver *Driver;
  28. dbCatValArray cvarr;
  29. int is_fp = 0;
  30. nareas = 0;
  31. G_verbose_message(_("Loading data..."));
  32. Vect_set_open_level(2);
  33. if (Vect_open_old2(&Map, vector_map, "", field_name) < 0)
  34. G_fatal_error(_("Unable to open vector map <%s>"), vector_map);
  35. field = Vect_get_field_number(&Map, field_name);
  36. if (field > 0)
  37. cat_list = Vect_cats_set_constraint(&Map, field, where, cats);
  38. if ((use == USE_Z) && !(Vect_is_3d(&Map)))
  39. G_fatal_error(_("Vector map <%s> is not 3D"),
  40. Vect_get_full_name(&Map));
  41. switch (use) {
  42. case USE_ATTR:
  43. db_CatValArray_init(&cvarr);
  44. if (!(Fi = Vect_get_field(&Map, field)))
  45. G_fatal_error(_("Database connection not defined for layer <%s>"),
  46. field_name);
  47. if ((Driver =
  48. db_start_driver_open_database(Fi->driver, Fi->database)) == NULL)
  49. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  50. Fi->database, Fi->driver);
  51. db_set_error_handler_driver(Driver);
  52. /* Note do not check if the column exists in the table because it may be expression */
  53. if ((nrec =
  54. db_select_CatValArray(Driver, Fi->table, Fi->key, column, NULL,
  55. &cvarr)) == -1)
  56. G_fatal_error(_("Column <%s> not found"), column);
  57. G_debug(3, "nrec = %d", nrec);
  58. ctype = cvarr.ctype;
  59. if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
  60. G_fatal_error(_("Column type (%s) not supported (did you mean 'labelcolumn'?)"),
  61. db_sqltype_name(ctype));
  62. if (nrec < 0)
  63. G_fatal_error(_("No records selected from table <%s>"),
  64. Fi->table);
  65. G_debug(1, "%d records selected from table", nrec);
  66. db_close_database_shutdown_driver(Driver);
  67. for (i = 0; i < cvarr.n_values; i++) {
  68. if (ctype == DB_C_TYPE_INT) {
  69. G_debug(3, "cat = %d val = %d", cvarr.value[i].cat,
  70. cvarr.value[i].val.i);
  71. }
  72. else if (ctype == DB_C_TYPE_DOUBLE) {
  73. G_debug(3, "cat = %d val = %f", cvarr.value[i].cat,
  74. cvarr.value[i].val.d);
  75. }
  76. }
  77. switch (ctype) {
  78. case DB_C_TYPE_INT:
  79. format = CELL_TYPE;
  80. break;
  81. case DB_C_TYPE_DOUBLE:
  82. format = DCELL_TYPE;
  83. break;
  84. default:
  85. G_fatal_error(_("Unable to use column <%s>"), column);
  86. break;
  87. }
  88. break;
  89. case USE_CAT:
  90. format = CELL_TYPE;
  91. break;
  92. case USE_VAL:
  93. format = value_type;
  94. break;
  95. case USE_Z:
  96. format = DCELL_TYPE;
  97. is_fp = 1;
  98. break;
  99. case USE_D:
  100. format = DCELL_TYPE;
  101. break;
  102. default:
  103. G_fatal_error(_("Unknown use type: %d"), use);
  104. }
  105. fd = Rast_open_new(raster_map, format);
  106. Points = Vect_new_line_struct();
  107. if (use != USE_Z && use != USE_D && (ftype & GV_AREA)) {
  108. if ((nareas = sort_areas(&Map, Points, field, cat_list)) == 0)
  109. G_warning(_("No areas selected from vector map <%s>"),
  110. vector_map);
  111. G_debug(1, "%d areas sorted", nareas);
  112. }
  113. nlines = 1;
  114. npasses = begin_rasterization(cache_mb, format, dense);
  115. pass = 0;
  116. nareas_all = Vect_get_num_areas(&Map);
  117. do {
  118. pass++;
  119. if (npasses > 1)
  120. G_message(_("Pass %d of %d:"), pass, npasses);
  121. stat = 0;
  122. if ((use != USE_Z && use != USE_D) && nareas) {
  123. if (do_areas
  124. (&Map, Points, &cvarr, ctype, use, value,
  125. value_type) < 0) {
  126. G_warning(_("Problem processing areas from vector map <%s>, continuing..."),
  127. vector_map);
  128. stat = -1;
  129. break;
  130. }
  131. }
  132. if (nlines) {
  133. if ((nlines =
  134. do_lines(&Map, Points, &cvarr, ctype, field, cat_list,
  135. use, value, value_type, ftype,
  136. &nplines_all, dense)) < 0) {
  137. G_warning(_("Problem processing lines from vector map <%s>, continuing..."),
  138. vector_map);
  139. stat = -1;
  140. break;
  141. }
  142. }
  143. G_important_message(_("Writing raster map..."));
  144. stat = output_raster(fd);
  145. } while (stat == 0);
  146. G_suppress_warnings(0);
  147. /* stat: 0 == repeat; 1 == done; -1 == error; */
  148. Vect_destroy_line_struct(Points);
  149. if (stat < 0) {
  150. Rast_unopen(fd);
  151. return 1;
  152. }
  153. Vect_close(&Map);
  154. G_verbose_message(_("Creating support files for raster map..."));
  155. Rast_close(fd);
  156. update_hist(raster_map, vector_map, Map.head.orig_scale);
  157. /* colors */
  158. if (rgbcolumn) {
  159. if (use != USE_ATTR && use != USE_CAT) {
  160. G_warning(_("Color can be updated from database only if use=attr"));
  161. update_colors(raster_map);
  162. }
  163. else {
  164. update_dbcolors(raster_map, vector_map, field, rgbcolumn, is_fp,
  165. column);
  166. }
  167. }
  168. else if (use == USE_D)
  169. update_fcolors(raster_map);
  170. else
  171. update_colors(raster_map);
  172. update_cats(raster_map);
  173. /* labels */
  174. update_labels(raster_map, vector_map, field, labelcolumn, use, value,
  175. column);
  176. if (nareas_all > 0)
  177. G_message(_("Converted areas: %d of %d"), nareas,
  178. nareas_all - Vect_get_num_primitives(&Map, GV_CENTROID));
  179. if (nplines_all > 0)
  180. G_message(_("Converted points/lines: %d of %d"), nlines, nplines_all);
  181. return 0;
  182. }