main.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /****************************************************************************
  2. *
  3. * MODULE: r.random
  4. *
  5. * AUTHOR(S): Dr. James Hinthorne - Central Wash. Uni. GIS Lab
  6. * Modified for GRASS 5.x
  7. * Eric G. Miller
  8. * Modified for GRASS 6.x and updates
  9. * Brad Douglas
  10. *
  11. * PURPOSE: Generate a vector or raster map of random points
  12. * selected from an input map
  13. *
  14. * COPYRIGHT: (C) 2003-2007 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General Public
  17. * License (>=v2). Read the file COPYING that comes with GRASS
  18. * for details.
  19. *
  20. *****************************************************************************/
  21. #include <stdlib.h>
  22. #include <grass/gis.h>
  23. #include <grass/glocale.h>
  24. #include "local_proto.h"
  25. static int has_percent(char *);
  26. int main(int argc, char *argv[])
  27. {
  28. short percent;
  29. double percentage;
  30. gcell_count targets;
  31. gcell_count count;
  32. struct rr_state myState;
  33. long seed_value;
  34. struct GModule *module;
  35. struct
  36. {
  37. struct Option *input, *cover, *raster, *sites, *npoints, *seed;
  38. } parm;
  39. struct
  40. {
  41. struct Flag *zero, *info, *z_geometry, *notopol_flag;
  42. } flag;
  43. G_gisinit(argv[0]);
  44. module = G_define_module();
  45. G_add_keyword(_("raster"));
  46. G_add_keyword(_("sampling"));
  47. G_add_keyword(_("vector"));
  48. G_add_keyword(_("random"));
  49. G_add_keyword(_("level1"));
  50. module->description =
  51. _("Creates a raster map layer and vector point map "
  52. "containing randomly located points.");
  53. parm.input = G_define_standard_option(G_OPT_R_INPUT);
  54. parm.input->description = _("Name of input raster map");
  55. parm.cover = G_define_standard_option(G_OPT_R_INPUT);
  56. parm.cover->key = "cover";
  57. parm.cover->required = NO;
  58. parm.cover->description = _("Name of cover raster map");
  59. parm.npoints = G_define_option();
  60. parm.npoints->key = "npoints";
  61. parm.npoints->key_desc = "number[%]";
  62. parm.npoints->type = TYPE_STRING;
  63. parm.npoints->required = YES;
  64. parm.npoints->description = _("The number of points to allocate");
  65. parm.raster = G_define_standard_option(G_OPT_R_OUTPUT);
  66. parm.raster->required = NO;
  67. parm.raster->key = "raster";
  68. parm.sites = G_define_standard_option(G_OPT_V_OUTPUT);
  69. parm.sites->required = NO;
  70. parm.sites->key = "vector";
  71. parm.seed = G_define_option();
  72. parm.seed->key = "seed";
  73. parm.seed->type = TYPE_INTEGER;
  74. parm.seed->required = NO;
  75. parm.seed->description = _("Seed for rand() function");
  76. flag.zero = G_define_flag();
  77. flag.zero->key = 'z';
  78. flag.zero->description = _("Generate points also for NULL category");
  79. flag.info = G_define_flag();
  80. flag.info->key = 'i';
  81. flag.info->description =
  82. _("Report information about input raster and exit");
  83. flag.z_geometry = G_define_flag();
  84. flag.z_geometry->key = 'd';
  85. flag.z_geometry->description = _("Generate vector points as 3D points");
  86. flag.notopol_flag = G_define_standard_flag(G_FLG_V_TOPO);
  87. flag.notopol_flag->description = _("Do not build topology in points mode");
  88. flag.notopol_flag->guisection = _("Points");
  89. if (G_parser(argc, argv) != 0)
  90. exit(EXIT_FAILURE);
  91. /* Set some state variables */
  92. myState.use_nulls = flag.zero->answer;
  93. myState.inraster = parm.input->answer;
  94. if (parm.cover->answer) {
  95. myState.docover = TRUE;
  96. myState.inrcover = parm.cover->answer;
  97. }
  98. else {
  99. myState.docover = FALSE;
  100. myState.inrcover = NULL;
  101. }
  102. myState.outraster = parm.raster->answer;
  103. myState.outvector = parm.sites->answer;
  104. myState.z_geometry = flag.z_geometry->answer;
  105. myState.notopol = flag.notopol_flag->answer;
  106. /* If they only want info we ignore the rest */
  107. get_stats(&myState);
  108. if (flag.info->answer) {
  109. #ifdef HAVE_LONG_LONG_INT
  110. G_message("Raster: %s\n"
  111. "Cover: %s\n"
  112. "Cell Count: %llu\n"
  113. "Null Cells: %llu\n\n",
  114. myState.inraster, myState.inrcover,
  115. myState.nCells, myState.nNulls);
  116. #else
  117. G_message("Raster: %s\n"
  118. "Cover: %s\n"
  119. "Cell Count: %lu\n"
  120. "Null Cells: %lu\n\n",
  121. myState.inraster, myState.inrcover,
  122. myState.nCells, myState.nNulls);
  123. #endif
  124. exit(EXIT_SUCCESS);
  125. }
  126. if (!(parm.raster->answer || parm.sites->answer))
  127. G_fatal_error(_("Note: one (or both) of %s and %s must be specified"),
  128. parm.raster->key, parm.sites->key);
  129. /* look for n[%] */
  130. percent = has_percent(parm.npoints->answer);
  131. if (percent) {
  132. if (sscanf(parm.npoints->answer, "%lf", &percentage) != 1
  133. || percentage <= 0.0 || percentage > 100.0) {
  134. G_fatal_error(_("<%s=%s> invalid percentage"),
  135. parm.npoints->key, parm.npoints->answer);
  136. }
  137. }
  138. else {
  139. #ifdef HAVE_LONG_LONG_INT
  140. if (sscanf(parm.npoints->answer, "%llu", &targets) != 1
  141. #else
  142. if (sscanf(parm.npoints->answer, "%lu", &targets) != 1
  143. #endif
  144. || targets <= 0) {
  145. G_fatal_error(_("<%s=%s> invalid number of points"),
  146. parm.npoints->key, parm.npoints->answer);
  147. }
  148. }
  149. count = (myState.use_nulls) ? myState.nCells :
  150. myState.nCells - myState.nNulls;
  151. if (percent)
  152. myState.nRand = (gcell_count)(count * percentage / 100.0 + .5);
  153. else {
  154. if (targets > count) {
  155. #ifdef HAVE_LONG_LONG_INT
  156. if (myState.use_nulls)
  157. G_fatal_error(_("There aren't [%llu] cells in the current region"),
  158. targets);
  159. else
  160. G_fatal_error(_("There aren't [%llu] non-NULL cells in the current region"),
  161. targets);
  162. #else
  163. if (myState.use_nulls)
  164. G_fatal_error(_("There aren't [%lu] cells in the current region"),
  165. targets);
  166. else
  167. G_fatal_error(_("There aren't [%lu] non-NULL cells in the current region"),
  168. targets);
  169. #endif
  170. }
  171. if (targets <= 0)
  172. G_fatal_error(_("There are no valid locations in the current region"));
  173. myState.nRand = targets;
  174. }
  175. if (parm.seed->answer) {
  176. seed_value = atol(parm.seed->answer);
  177. G_srand48(seed_value);
  178. G_debug(3, "Read random seed from seed=: %ld", seed_value);
  179. }
  180. else {
  181. seed_value = G_srand48_auto();
  182. G_debug(3, "Generated random seed (-s): %ld", seed_value);
  183. }
  184. execute_random(&myState);
  185. if (myState.outraster)
  186. make_support(&myState, percent, percentage);
  187. return EXIT_SUCCESS;
  188. }
  189. static int has_percent(char *s)
  190. {
  191. while (*s)
  192. if (*s++ == '%')
  193. return 1;
  194. return 0;
  195. }