trans2.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <grass/gis.h>
  2. #include <grass/vector.h>
  3. #include <grass/dbmi.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. /*!
  7. \brief transform 2d vector features to 3d
  8. \param In input vector
  9. \param Out output vector
  10. \param type feature type to be transformed
  11. \param height fixed height (used only if column is NULL)
  12. \param field layer number
  13. \param column attribute column used for height
  14. */
  15. void trans2d(struct Map_info *In, struct Map_info *Out, int type,
  16. double height, const char *field_name, const char *column)
  17. {
  18. int i, ltype, line, field;
  19. int cat;
  20. int ret, ctype;
  21. struct line_pnts *Points;
  22. struct line_cats *Cats;
  23. dbCatValArray cvarr;
  24. Points = Vect_new_line_struct();
  25. Cats = Vect_new_cats_struct();
  26. db_CatValArray_init(&cvarr);
  27. field = Vect_get_field_number(In, field_name);
  28. if (column) {
  29. struct field_info *Fi;
  30. dbDriver *driver;
  31. if (field == -1) {
  32. G_warning(_("Invalid layer number %d, assuming 1"), field);
  33. field = 1;
  34. }
  35. Fi = Vect_get_field(In, field);
  36. if (!Fi) {
  37. G_fatal_error(_("Database connection not defined for layer <%s>"),
  38. field_name);
  39. }
  40. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  41. if (!driver) {
  42. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  43. Fi->database, Fi->driver);
  44. }
  45. db_set_error_handler_driver(driver);
  46. /* column type must numeric */
  47. ctype = db_column_Ctype(driver, Fi->table, column);
  48. if (ctype == -1) {
  49. G_fatal_error(_("Column <%s> not found in table <%s>"),
  50. column, Fi->table);
  51. }
  52. if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE) {
  53. G_fatal_error(_("Column must be numeric"));
  54. }
  55. G_message(_("Fetching height from <%s> column..."), column);
  56. db_select_CatValArray(driver, Fi->table, Fi->key,
  57. column, NULL, &cvarr);
  58. G_debug(3, "%d records selected", cvarr.n_values);
  59. db_close_database_shutdown_driver(driver);
  60. }
  61. G_message(_("Transforming features..."));
  62. line = 1;
  63. while (1) {
  64. ltype = Vect_read_next_line(In, Points, Cats);
  65. if (ltype == -1) {
  66. G_fatal_error(_("Unable to read vector map"));
  67. }
  68. if (ltype == -2) { /* EOF */
  69. break;
  70. }
  71. G_progress(line, 1000);
  72. if (!(ltype & type))
  73. continue;
  74. if (field != -1 && !Vect_cat_get(Cats, field, &cat))
  75. continue;
  76. if (column) {
  77. Vect_cat_get(Cats, field, &cat);
  78. if (cat < 0) {
  79. G_warning(_("Skipping feature without category"));
  80. continue;
  81. }
  82. if (ctype == DB_C_TYPE_DOUBLE)
  83. ret = db_CatValArray_get_value_double(&cvarr, cat, &height);
  84. else { /* integer */
  85. int height_i;
  86. ret = db_CatValArray_get_value_int(&cvarr, cat, &height_i);
  87. height = (double)height_i;
  88. }
  89. if (ret != DB_OK)
  90. G_warning(_("Unable to get height for feature category %d"),
  91. cat);
  92. }
  93. for (i = 0; i < Points->n_points; i++) {
  94. Points->z[i] = height;
  95. }
  96. Vect_write_line(Out, ltype, Points, Cats);
  97. line++;
  98. }
  99. G_progress(1, 1);
  100. Vect_destroy_line_struct(Points);
  101. Vect_destroy_cats_struct(Cats);
  102. }