main.c 8.4 KB

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