load.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. \file load.cpp
  3. \brief Experimental C++ wxWidgets Nviz prototype -- load data layers
  4. Used by wxGUI Nviz extension.
  5. Copyright: (C) by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. \author Martin Landa <landa.martin gmail.com> (Google SoC 2008)
  10. \date 2008
  11. */
  12. #include "nviz.h"
  13. extern "C" {
  14. #include <grass/G3d.h>
  15. #include <grass/glocale.h>
  16. }
  17. /*!
  18. \brief Load raster map (surface)
  19. \param name raster map name
  20. \param color_name raster map for color (NULL for color_value)
  21. \param color_value color string (named color or RGB triptet)
  22. \return object id
  23. \return -1 on failure
  24. */
  25. int Nviz::LoadSurface(const char* name, const char *color_name, const char *color_value)
  26. {
  27. const char *mapset;
  28. int id;
  29. mapset = G_find_cell2 (name, "");
  30. if (mapset == NULL) {
  31. G_warning(_("Raster map <%s> not found"),
  32. name);
  33. return -1;
  34. }
  35. /* topography */
  36. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  37. G_fully_qualified_name(name, mapset), 0.0,
  38. data);
  39. if (color_name) { /* check for color map */
  40. mapset = G_find_cell2 (color_name, "");
  41. if (mapset == NULL) {
  42. G_warning(_("Raster map <%s> not found"),
  43. color_name);
  44. GS_delete_surface(id);
  45. return -1;
  46. }
  47. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  48. G_fully_qualified_name(color_name, mapset), -1.0,
  49. data);
  50. }
  51. else if (color_value) { /* check for color value */
  52. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  53. NULL, Nviz_color_from_str(color_value),
  54. data);
  55. }
  56. else { /* use by default elevation map for coloring */
  57. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  58. G_fully_qualified_name(name, mapset), -1.0,
  59. data);
  60. }
  61. /*
  62. if (i > 1)
  63. set_default_wirecolors(data, i);
  64. */
  65. /* focus on loaded data */
  66. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1);
  67. G_debug(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id);
  68. return id;
  69. }
  70. /*!
  71. \brief Unload surface
  72. \param id surface id
  73. \return 1 on success
  74. \return 0 on failure
  75. */
  76. int Nviz::UnloadSurface(int id)
  77. {
  78. if (!GS_surf_exists(id)) {
  79. return 0;
  80. }
  81. G_debug(1, "Nviz::UnloadSurface(): id=%d", id);
  82. if (GS_delete_surface(id) < 0)
  83. return 0;
  84. return 1;
  85. }
  86. /*!
  87. \brief Load vector map overlay
  88. \param name vector map name
  89. \param points if true load 2d points rather then 2d lines
  90. \return object id
  91. \return -1 on failure
  92. */
  93. int Nviz::LoadVector(const char *name, bool points)
  94. {
  95. int id;
  96. const char *mapset;
  97. if (GS_num_surfs() == 0) { /* load base surface if no loaded */
  98. int *surf_list, nsurf;
  99. Nviz_new_map_obj(MAP_OBJ_SURF, NULL, 0.0, data);
  100. surf_list = GS_get_surf_list(&nsurf);
  101. GS_set_att_const(surf_list[0], ATT_TRANSP, 255);
  102. }
  103. mapset = G_find_vector2 (name, "");
  104. if (mapset == NULL) {
  105. G_warning(_("Vector map <%s> not found"),
  106. name);
  107. }
  108. if (points) {
  109. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  110. G_fully_qualified_name(name, mapset), 0.0,
  111. data);
  112. }
  113. else {
  114. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  115. G_fully_qualified_name(name, mapset), 0.0,
  116. data);
  117. }
  118. G_debug(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id);
  119. return id;
  120. }
  121. /*!
  122. \brief Unload vector set
  123. \param id vector set id
  124. \param points vector points or lines set
  125. \return 1 on success
  126. \return 0 on failure
  127. */
  128. int Nviz::UnloadVector(int id, bool points)
  129. {
  130. G_debug(1, "Nviz::UnloadVector(): id=%d", id);
  131. if (points) {
  132. if (!GP_site_exists(id)) {
  133. return 0;
  134. }
  135. if (GP_delete_site(id) < 0)
  136. return 0;
  137. }
  138. else {
  139. if (!GV_vect_exists(id)) {
  140. return 0;
  141. }
  142. if (GV_delete_vector(id) < 0)
  143. return 0;
  144. }
  145. return 1;
  146. }
  147. /*!
  148. \brief Load 3d raster map (volume)
  149. \param name 3d raster map name
  150. \param color_name 3d raster map for color (NULL for color_value)
  151. \param color_value color string (named color or RGB triptet)
  152. \return object id
  153. \return -1 on failure
  154. */
  155. int Nviz::LoadVolume(const char* name, const char *color_name, const char *color_value)
  156. {
  157. char *mapset;
  158. int id;
  159. mapset = G_find_grid3(name, "");
  160. if (mapset == NULL) {
  161. G_warning(_("3d raster map <%s> not found"),
  162. name);
  163. return -1;
  164. }
  165. /* topography */
  166. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  167. G_fully_qualified_name(name, mapset), 0.0,
  168. data);
  169. if (color_name) { /* check for color map */
  170. mapset = G_find_grid3(color_name, "");
  171. if (mapset == NULL) {
  172. G_warning(_("3d raster map <%s> not found"),
  173. color_name);
  174. GVL_delete_vol(id);
  175. return -1;
  176. }
  177. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  178. G_fully_qualified_name(color_name, mapset), -1.0,
  179. data);
  180. }
  181. else if (color_value) { /* check for color value */
  182. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  183. NULL, Nviz_color_from_str(color_value),
  184. data);
  185. }
  186. else { /* use by default elevation map for coloring */
  187. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  188. G_fully_qualified_name(name, mapset), -1.0,
  189. data);
  190. }
  191. G_debug(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id);
  192. return id;
  193. }
  194. /*!
  195. \brief Unload volume
  196. \param id volume id
  197. \return 1 on success
  198. \return 0 on failure
  199. */
  200. int Nviz::UnloadVolume(int id)
  201. {
  202. if (!GVL_vol_exists(id)) {
  203. return 0;
  204. }
  205. G_debug(1, "Nviz::UnloadVolume(): id=%d", id);
  206. if (GVL_delete_vol(id) < 0)
  207. return 0;
  208. return 1;
  209. }