main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2000 by the GRASS Development Team
  3. * Author: Bob Covill <bcovill@tekmap.ns.ca>
  4. *
  5. * This Program is free software under the GPL (>=v2)
  6. * Read the file COPYING coming with GRASS for details
  7. *
  8. */
  9. #include <stdlib.h>
  10. #include <grass/gis.h>
  11. #include <grass/raster.h>
  12. #include <grass/glocale.h>
  13. #include "local_proto.h"
  14. int clr;
  15. struct Colors colors;
  16. static double dist, e, n;
  17. int main(int argc, char *argv[])
  18. {
  19. char *name, *outfile;
  20. int fd, projection;
  21. FILE *fp;
  22. double res;
  23. char *null_string;
  24. char ebuf[256], nbuf[256], label[512], formatbuff[256];
  25. char b1[100], b2[100];
  26. int n;
  27. int havefirst = FALSE;
  28. int coords = 0, i, k = -1;
  29. double e1, e2, n1, n2;
  30. RASTER_MAP_TYPE data_type;
  31. struct Cell_head window;
  32. struct
  33. {
  34. struct Option *opt1, *profile, *res, *output, *null_str;
  35. struct Flag *g, *c;
  36. }
  37. parm;
  38. struct GModule *module;
  39. G_gisinit(argv[0]);
  40. /* Set description */
  41. module = G_define_module();
  42. G_add_keyword(_("raster"));
  43. G_add_keyword(_("profile"));
  44. module->description =
  45. _("Outputs the raster map layer values lying on user-defined line(s).");
  46. parm.opt1 = G_define_standard_option(G_OPT_R_INPUT);
  47. parm.output = G_define_standard_option(G_OPT_F_OUTPUT);
  48. parm.output->required = NO;
  49. parm.output->answer = "-";
  50. parm.output->description =
  51. _("Name of file for output (use output=- for stdout)");
  52. parm.profile = G_define_option();
  53. parm.profile->key = "profile";
  54. parm.profile->type = TYPE_STRING;
  55. parm.profile->required = NO;
  56. parm.profile->multiple = YES;
  57. parm.profile->key_desc = "east,north";
  58. parm.profile->description = _("Profile coordinate pairs");
  59. parm.res = G_define_option();
  60. parm.res->key = "res";
  61. parm.res->type = TYPE_DOUBLE;
  62. parm.res->required = NO;
  63. parm.res->description =
  64. _("Resolution along profile (default = current region resolution)");
  65. parm.null_str = G_define_option();
  66. parm.null_str->key = "null";
  67. parm.null_str->type = TYPE_STRING;
  68. parm.null_str->required = NO;
  69. parm.null_str->answer = "*";
  70. parm.null_str->description = _("Character to represent no data cell");
  71. parm.g = G_define_flag();
  72. parm.g->key = 'g';
  73. parm.g->description =
  74. _("Output easting and northing in first two columns of four column output");
  75. parm.c = G_define_flag();
  76. parm.c->key = 'c';
  77. parm.c->description =
  78. _("Output RRR:GGG:BBB color values for each profile point");
  79. if (G_parser(argc, argv))
  80. exit(EXIT_FAILURE);
  81. clr = 0;
  82. if (parm.c->answer)
  83. clr = 1; /* color output */
  84. null_string = parm.null_str->answer;
  85. G_get_window(&window);
  86. projection = G_projection();
  87. if (parm.res->answer) {
  88. res = atof(parm.res->answer);
  89. /* Catch bad resolution ? */
  90. if (res <= 0)
  91. G_fatal_error(_("Illegal resolution! [%g]"), res);
  92. }
  93. else {
  94. /* Do average of EW and NS res */
  95. res = (window.ew_res + window.ns_res) / 2;
  96. }
  97. G_message(_("Using resolution [%g]"), res);
  98. G_begin_distance_calculations();
  99. /* Open Input File for reading */
  100. /* Get Input Name */
  101. name = parm.opt1->answer;
  102. if (parm.g->answer)
  103. coords = 1;
  104. /* Open Raster File */
  105. fd = Rast_open_old(name, "");
  106. /* initialize color structure */
  107. if (clr)
  108. Rast_read_colors(name, "", &colors);
  109. /* Open ASCII file for output or stdout */
  110. outfile = parm.output->answer;
  111. if ((strcmp("-", outfile)) == 0) {
  112. fp = stdout;
  113. }
  114. else if (NULL == (fp = fopen(outfile, "w")))
  115. G_fatal_error(_("Unable to open file <%s>"), outfile);
  116. /* Get Raster Type */
  117. data_type = Rast_get_map_type(fd);
  118. /* Done with file */
  119. /* Show message giving output format */
  120. G_message(_("Output Format:"));
  121. if (coords == 1)
  122. sprintf(formatbuff,
  123. _("[Easting] [Northing] [Along Track Dist.(m)] [Elevation]"));
  124. else
  125. sprintf(formatbuff, _("[Along Track Dist.(m)] [Elevation]"));
  126. if (clr)
  127. strcat(formatbuff, _(" [RGB Color]"));
  128. G_message(formatbuff);
  129. /* Get Profile Start Coords */
  130. if (!parm.profile->answer) {
  131. /* Assume input from stdin */
  132. for (n = 1; input(b1, ebuf, b2, nbuf, label); n++) {
  133. G_debug(4, "stdin line %d: ebuf=[%s] nbuf=[%s]", n, ebuf, nbuf);
  134. if (!G_scan_easting(ebuf, &e2, G_projection()) ||
  135. !G_scan_northing(nbuf, &n2, G_projection()))
  136. G_fatal_error(_("Invalid coordinates %s %s"), ebuf, nbuf);
  137. if (havefirst)
  138. do_profile(e1, e2, n1, n2, name, coords, res, fd, data_type,
  139. fp, null_string);
  140. e1 = e2;
  141. n1 = n2;
  142. havefirst = TRUE;
  143. }
  144. }
  145. else {
  146. /* Coords from Command Line */
  147. for (i = 0; parm.profile->answers[i]; i += 2) {
  148. /* Test for number coordinate pairs */
  149. k = i;
  150. }
  151. if (k == 0) {
  152. /* Only one coordinate pair supplied */
  153. G_scan_easting(parm.profile->answers[0], &e1, G_projection());
  154. G_scan_northing(parm.profile->answers[1], &n1, G_projection());
  155. e2 = e1;
  156. n2 = n1;
  157. /* Get profile info */
  158. do_profile(e1, e2, n1, n2, name, coords, res, fd, data_type, fp,
  159. null_string);
  160. }
  161. else {
  162. for (i = 0; i <= k - 2; i += 2) {
  163. G_scan_easting(parm.profile->answers[i], &e1, G_projection());
  164. G_scan_northing(parm.profile->answers[i + 1], &n1,
  165. G_projection());
  166. G_scan_easting(parm.profile->answers[i + 2], &e2,
  167. G_projection());
  168. G_scan_northing(parm.profile->answers[i + 3], &n2,
  169. G_projection());
  170. /* Get profile info */
  171. do_profile(e1, e2, n1, n2, name, coords, res, fd, data_type,
  172. fp, null_string);
  173. }
  174. }
  175. }
  176. Rast_close(fd);
  177. fclose(fp);
  178. if (clr)
  179. Rast_free_colors(&colors);
  180. exit(EXIT_SUCCESS);
  181. } /* Done with main */
  182. /* Calculate the Profile Now */
  183. /* Establish parameters */
  184. int do_profile(double e1, double e2, double n1, double n2, char *name,
  185. int coords, double res, int fd, int data_type, FILE * fp,
  186. char *null_string)
  187. {
  188. float rows, cols, LEN;
  189. double Y, X, AZI;
  190. cols = e1 - e2;
  191. rows = n1 - n2;
  192. LEN = G_distance(e1, n1, e2, n2);
  193. G_message(_("Approx. transect length [%f] m"), LEN);
  194. if (!G_point_in_region(e2, n2))
  195. G_warning(_("Endpoint coordinates are outside of current region settings"));
  196. /* Calculate Azimuth of Line */
  197. if (rows == 0 && cols == 0) {
  198. /* Special case for no movement */
  199. e = e1;
  200. n = n1;
  201. read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
  202. }
  203. if (rows >= 0 && cols < 0) {
  204. /* SE Quad or due east */
  205. AZI = atan((rows / cols));
  206. Y = res * sin(AZI);
  207. X = res * cos(AZI);
  208. if (Y < 0)
  209. Y = Y * -1.;
  210. if (X < 0)
  211. X = X * -1.;
  212. if (e != 0.0 && (e != e1 || n != n1)) {
  213. dist -= G_distance(e, n, e1, n1);
  214. }
  215. for (e = e1, n = n1; e < e2 || n > n2; e += X, n -= Y) {
  216. read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
  217. /* d+=res; */
  218. dist += G_distance(e - X, n + Y, e, n);
  219. }
  220. }
  221. if (rows < 0 && cols <= 0) {
  222. /* NE Quad or due north */
  223. AZI = atan((cols / rows));
  224. X = res * sin(AZI);
  225. Y = res * cos(AZI);
  226. if (Y < 0)
  227. Y = Y * -1.;
  228. if (X < 0)
  229. X = X * -1.;
  230. if (e != 0.0 && (e != e1 || n != n1)) {
  231. dist -= G_distance(e, n, e1, n1);
  232. /*
  233. * read_rast (e1, n1, dist, fd, coords, data_type, fp, null_string);
  234. */
  235. }
  236. for (e = e1, n = n1; e < e2 || n < n2; e += X, n += Y) {
  237. read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
  238. /* d+=res; */
  239. dist += G_distance(e - X, n - Y, e, n);
  240. }
  241. }
  242. if (rows > 0 && cols >= 0) {
  243. /* SW Quad or due south */
  244. AZI = atan((rows / cols));
  245. X = res * cos(AZI);
  246. Y = res * sin(AZI);
  247. if (Y < 0)
  248. Y = Y * -1.;
  249. if (X < 0)
  250. X = X * -1.;
  251. if (e != 0.0 && (e != e1 || n != n1)) {
  252. dist -= G_distance(e, n, e1, n1);
  253. }
  254. for (e = e1, n = n1; e > e2 || n > n2; e -= X, n -= Y) {
  255. read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
  256. /* d+=res; */
  257. dist += G_distance(e + X, n + Y, e, n);
  258. }
  259. }
  260. if (rows <= 0 && cols > 0) {
  261. /* NW Quad or due west */
  262. AZI = atan((rows / cols));
  263. X = res * cos(AZI);
  264. Y = res * sin(AZI);
  265. if (Y < 0)
  266. Y = Y * -1.;
  267. if (X < 0)
  268. X = X * -1.;
  269. if (e != 0.0 && (e != e1 || n != n1)) {
  270. dist -= G_distance(e, n, e1, n1);
  271. }
  272. for (e = e1, n = n1; e > e2 || n < n2; e -= X, n += Y) {
  273. read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
  274. /* d+=res; */
  275. dist += G_distance(e + X, n - Y, e, n);
  276. }
  277. }
  278. /*
  279. * return dist;
  280. */
  281. return 0;
  282. } /* done with do_profile */