support.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include <grass/vector.h>
  4. #include "local_proto.h"
  5. int make_support(struct rr_state *theState, int percent, double percentage)
  6. {
  7. char title[100];
  8. struct History hist;
  9. struct Categories cats;
  10. struct Colors clr;
  11. char *inraster;
  12. struct RASTER_MAP_PTR nulls;
  13. /* write categories for output raster
  14. use values from input or cover map
  15. */
  16. if (theState->docover == 1) {
  17. inraster = theState->inrcover;
  18. nulls = theState->cnulls;
  19. }
  20. else {
  21. inraster = theState->inraster;
  22. nulls = theState->nulls;
  23. }
  24. if (Rast_read_cats(inraster, "", &cats) >= 0) {
  25. sprintf(title, "Random points on <%s>", inraster);
  26. Rast_set_cats_title(title, &cats);
  27. if (theState->use_nulls)
  28. Rast_set_cat(nulls.data.v,
  29. nulls.data.v,
  30. "Points with NULL values in original",
  31. &cats, nulls.type);
  32. Rast_write_cats(theState->outraster, &cats);
  33. }
  34. /* write history for output raster */
  35. if (Rast_read_history(theState->outraster, G_mapset(), &hist) >= 0) {
  36. Rast_short_history(theState->outraster, "raster", &hist);
  37. Rast_format_history(&hist, HIST_DATSRC_1, "Based on map <%s>", inraster);
  38. if (percent)
  39. Rast_format_history(
  40. &hist, HIST_DATSRC_2,
  41. "Random points over %.2f percent of the base map <%s>",
  42. percentage, inraster);
  43. else
  44. Rast_format_history(
  45. &hist, HIST_DATSRC_2,
  46. "%ld random points on the base map <%s>",
  47. theState->nRand, theState->inraster);
  48. Rast_command_history(&hist);
  49. Rast_write_history(theState->outraster, &hist);
  50. }
  51. /* write commandline to output vector */
  52. if (theState->outvector) {
  53. struct Map_info map;
  54. Vect_open_old(&map, theState->outvector, G_mapset());
  55. Vect_hist_command(&map);
  56. Vect_close(&map);
  57. }
  58. /* set colors for output raster */
  59. if (Rast_read_colors(inraster, "", &clr) >= 0) {
  60. if (theState->use_nulls) {
  61. Rast_add_color_rule(nulls.data.v, 127, 127, 127,
  62. nulls.data.v, 127, 127, 127, &clr,
  63. nulls.type);
  64. }
  65. Rast_write_colors(theState->outraster, G_mapset(), &clr);
  66. }
  67. return 0;
  68. }