support.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_write_history(theState->outraster, &hist);
  49. }
  50. /* write commandline to output vector */
  51. if (theState->outvector) {
  52. struct Map_info map;
  53. Vect_open_old(&map, theState->outvector, G_mapset());
  54. Vect_hist_command(&map);
  55. Vect_close(&map);
  56. }
  57. /* set colors for output raster */
  58. if (Rast_read_colors(inraster, "", &clr) >= 0) {
  59. if (theState->use_nulls) {
  60. Rast_add_color_rule(nulls.data.v, 127, 127, 127,
  61. nulls.data.v, 127, 127, 127, &clr,
  62. nulls.type);
  63. }
  64. Rast_write_colors(theState->outraster, G_mapset(), &clr);
  65. }
  66. return 0;
  67. }