support.c 2.2 KB

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