poly2rast.c 2.1 KB

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