poly2rast.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include <grass/glocale.h>
  6. #include "format.h"
  7. #include "local_proto.h"
  8. int poly_to_rast(char *input_file, char *raster_map, char *title, int nrows)
  9. {
  10. double *x, *y;
  11. int count;
  12. long cat;
  13. int type;
  14. struct Categories labels;
  15. FILE *ifd; /* for input file */
  16. int rfd; /* for raster map */
  17. int format;
  18. int stat;
  19. int pass, npasses;
  20. struct History history;
  21. /* open input file */
  22. if (strcmp("-", input_file) == 0)
  23. ifd = stdin;
  24. else
  25. ifd = fopen(input_file, "r");
  26. if (ifd == NULL) {
  27. perror(input_file);
  28. exit(EXIT_FAILURE);
  29. }
  30. rfd = Rast_open_c_new(raster_map);
  31. if (title == NULL)
  32. title = "";
  33. G_strip(title);
  34. Rast_init_cats(title, &labels);
  35. format = getformat(ifd);
  36. /* ?? otherwise get complaints about window changes */
  37. G_suppress_warnings(TRUE);
  38. npasses = begin_rasterization(nrows, format);
  39. G_suppress_warnings(FALSE);
  40. pass = 0;
  41. do {
  42. pass++;
  43. if (npasses > 1)
  44. G_message(_("Pass #%d (of %d) ..."), pass, npasses);
  45. G_fseek(ifd, 0L, 0);
  46. while (get_item(ifd, &type, &cat, &x, &y, &count, &labels)) {
  47. set_cat(cat);
  48. switch (type) {
  49. case 'A':
  50. G_plot_polygon(x, y, count);
  51. break;
  52. case 'L':
  53. while (--count > 0) {
  54. G_plot_line2(x[0], y[0], x[1], y[1]);
  55. x++;
  56. y++;
  57. }
  58. break;
  59. case 'P':
  60. G_plot_point(x[0], y[0]);
  61. break;
  62. }
  63. }
  64. G_message(_("Writing raster map..."));
  65. stat = output_raster(rfd);
  66. } while (stat == 0);
  67. /* stat: 0 means repeat
  68. * 1 means done
  69. * -1 means error
  70. */
  71. if (stat < 0) {
  72. Rast_unopen(rfd);
  73. return 1;
  74. }
  75. Rast_close(rfd);
  76. Rast_write_cats(raster_map, &labels);
  77. Rast_short_history(raster_map, "raster", &history);
  78. Rast_command_history(&history);
  79. Rast_write_history(raster_map, &history);
  80. return 0;
  81. }