main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /****************************************************************
  2. *
  3. * MODULE: d.grid
  4. *
  5. * AUTHOR(S): James Westervelt, U.S. Army CERL
  6. * Geogrid support: Bob Covill, www.tekmap.ns.ca
  7. *
  8. * PURPOSE: Draw the coordinate grid the user wants displayed on
  9. * top of the current image
  10. *
  11. * COPYRIGHT: (C) 1999-2008 by the GRASS Development Team
  12. *
  13. * This program is free software under the
  14. * GNU General Public License (>=v2).
  15. * Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. **************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <grass/gis.h>
  23. #include <grass/display.h>
  24. #include <grass/gprojects.h>
  25. #include <grass/glocale.h>
  26. #include "local_proto.h"
  27. int main(int argc, char **argv)
  28. {
  29. int colorg = 0;
  30. int colorb = 0;
  31. int colort = 0;
  32. double size = 0., gsize = 0.; /* initialize to zero */
  33. double east, north;
  34. int do_text, fontsize, mark_type, line_width;
  35. struct GModule *module;
  36. struct Option *opt1, *opt2, *opt3, *opt4, *fsize, *tcolor, *lwidth;
  37. struct Flag *noborder, *notext, *geogrid, *nogrid, *wgs84, *cross,
  38. *fiducial, *dot, *align;
  39. struct pj_info info_in; /* Proj structures */
  40. struct pj_info info_out; /* Proj structures */
  41. struct Cell_head wind;
  42. /* Initialize the GIS calls */
  43. G_gisinit(argv[0]);
  44. module = G_define_module();
  45. G_add_keyword(_("display"));
  46. G_add_keyword(_("cartography"));
  47. module->description =
  48. _("Overlays a user-specified grid "
  49. "in the active display frame on the graphics monitor.");
  50. opt2 = G_define_option();
  51. opt2->key = "size";
  52. opt2->key_desc = "value";
  53. opt2->type = TYPE_STRING;
  54. opt2->required = YES;
  55. opt2->label = _("Size of grid to be drawn (0: north-south resolution of the current region)");
  56. opt2->description = _("In map units or DDD:MM:SS format. "
  57. "Example: \"1000\" or \"0:10\"");
  58. opt3 = G_define_option();
  59. opt3->key = "origin";
  60. opt3->type = TYPE_STRING;
  61. opt3->key_desc = "easting,northing";
  62. opt3->answer = "0,0";
  63. opt3->multiple = NO;
  64. opt3->description = _("Lines of the grid pass through this coordinate");
  65. lwidth = G_define_option();
  66. lwidth->key = "width";
  67. lwidth->type = TYPE_DOUBLE;
  68. lwidth->required = NO;
  69. lwidth->description = _("Grid line width");
  70. opt1 = G_define_standard_option(G_OPT_C_FG);
  71. opt1->answer = "gray";
  72. opt1->label = _("Grid color");
  73. opt1->guisection = _("Color");
  74. opt4 = G_define_standard_option(G_OPT_C_FG);
  75. opt4->key = "bordercolor";
  76. opt4->label = _("Border color");
  77. opt4->guisection = _("Color");
  78. tcolor = G_define_standard_option(G_OPT_C_FG);
  79. tcolor->key = "textcolor";
  80. tcolor->answer = "gray";
  81. tcolor->label = _("Text color");
  82. tcolor->guisection = _("Color");
  83. fsize = G_define_option();
  84. fsize->key = "fontsize";
  85. fsize->type = TYPE_INTEGER;
  86. fsize->required = NO;
  87. fsize->answer = "9";
  88. fsize->options = "1-72";
  89. fsize->description = _("Font size for gridline coordinate labels");
  90. align = G_define_flag();
  91. align->key = 'a';
  92. align->description = _("Align the origin to the east-north corner of the current region");
  93. geogrid = G_define_flag();
  94. geogrid->key = 'g';
  95. geogrid->description =
  96. _("Draw geographic grid (referenced to current ellipsoid)");
  97. wgs84 = G_define_flag();
  98. wgs84->key = 'w';
  99. wgs84->description =
  100. _("Draw geographic grid (referenced to WGS84 ellipsoid)");
  101. cross = G_define_flag();
  102. cross->key = 'c';
  103. cross->description = _("Draw '+' marks instead of grid lines");
  104. dot = G_define_flag();
  105. dot->key = 'd';
  106. dot->description = _("Draw '.' marks instead of grid lines");
  107. fiducial = G_define_flag();
  108. fiducial->key = 'f';
  109. fiducial->description = _("Draw fiducial marks instead of grid lines");
  110. nogrid = G_define_flag();
  111. nogrid->key = 'n';
  112. nogrid->description = _("Disable grid drawing");
  113. nogrid->guisection = _("Disable");
  114. noborder = G_define_flag();
  115. noborder->key = 'b';
  116. noborder->description = _("Disable border drawing");
  117. noborder->guisection = _("Disable");
  118. notext = G_define_flag();
  119. notext->key = 't';
  120. notext->description = _("Disable text drawing");
  121. notext->guisection = _("Disable");
  122. /* Check command line */
  123. if (G_parser(argc, argv))
  124. exit(EXIT_FAILURE);
  125. /* do some checking */
  126. if (nogrid->answer && noborder->answer)
  127. G_fatal_error(_("Both grid and border drawing are disabled"));
  128. if (wgs84->answer)
  129. geogrid->answer = 1; /* -w implies -g */
  130. if (geogrid->answer && G_projection() == PROJECTION_LL)
  131. G_fatal_error(_("Geo-Grid option is not available for LL projection"));
  132. if (geogrid->answer && G_projection() == PROJECTION_XY)
  133. G_fatal_error(_("Geo-Grid option is not available for XY projection"));
  134. if (notext->answer)
  135. do_text = FALSE;
  136. else
  137. do_text = TRUE;
  138. if (lwidth->answer) {
  139. line_width = atoi(lwidth->answer);
  140. if(line_width < 0 || line_width > 1e3)
  141. G_fatal_error("Invalid line width.");
  142. }
  143. else
  144. line_width = 0;
  145. fontsize = atoi(fsize->answer);
  146. mark_type = MARK_GRID;
  147. if (cross->answer + fiducial->answer + dot->answer > 1)
  148. G_fatal_error(_("Choose a single mark style"));
  149. if (cross->answer)
  150. mark_type = MARK_CROSS;
  151. if (fiducial->answer)
  152. mark_type = MARK_FIDUCIAL;
  153. if (dot->answer)
  154. mark_type = MARK_DOT;
  155. if (align->answer || strcmp(opt2->answer, "0") == 0)
  156. G__get_window(&wind, "", "WIND", G_mapset());
  157. if (strcmp(opt2->answer, "0") == 0) {
  158. if (geogrid->answer)
  159. gsize = wind.ns_res;
  160. else
  161. size = wind.ns_res;
  162. } else {
  163. /* get grid size */
  164. if (geogrid->answer) {
  165. if (!G_scan_resolution(opt2->answer, &gsize, PROJECTION_LL) ||
  166. gsize <= 0.0)
  167. G_fatal_error(_("Invalid geo-grid size <%s>"), opt2->answer);
  168. }
  169. else {
  170. if (!G_scan_resolution(opt2->answer, &size, G_projection()) ||
  171. size <= 0.0)
  172. G_fatal_error(_("Invalid grid size <%s>"), opt2->answer);
  173. }
  174. }
  175. if (align->answer) {
  176. /* reduce accumulated errors when ew_res is not the same as ns_res. */
  177. struct Cell_head w;
  178. G_get_set_window(&w);
  179. east = wind.west + (int)((w.west - wind.west) / wind.ew_res) * wind.ew_res;
  180. north = wind.south + (int)((w.south - wind.south) / wind.ns_res) * wind.ns_res;
  181. } else {
  182. /* get grid easting start */
  183. if (!G_scan_easting(opt3->answers[0], &east, G_projection())) {
  184. G_usage();
  185. G_fatal_error(_("Illegal east coordinate <%s>"), opt3->answers[0]);
  186. }
  187. /* get grid northing start */
  188. if (!G_scan_northing(opt3->answers[1], &north, G_projection())) {
  189. G_usage();
  190. G_fatal_error(_("Illegal north coordinate <%s>"), opt3->answers[1]);
  191. }
  192. }
  193. /* Setup driver and check important information */
  194. if (D_open_driver() != 0)
  195. G_fatal_error(_("No graphics device selected. "
  196. "Use d.mon to select graphics device."));
  197. /* Parse and select grid color */
  198. colorg = D_parse_color(opt1->answer, FALSE);
  199. /* Parse and select border color */
  200. colorb = D_parse_color(opt4->answer, FALSE);
  201. /* Parse and select text color */
  202. colort = D_parse_color(tcolor->answer, FALSE);
  203. D_setup(0);
  204. /* draw grid */
  205. if (!nogrid->answer) {
  206. if (geogrid->answer) {
  207. /* initialzie proj stuff */
  208. init_proj(&info_in, &info_out, wgs84->answer);
  209. plot_geogrid(gsize, info_in, info_out, do_text, colorg, colort,
  210. fontsize, mark_type, line_width);
  211. }
  212. else {
  213. /* Do the grid plotting */
  214. plot_grid(size, east, north, do_text, colorg, colort, fontsize,
  215. mark_type, line_width);
  216. }
  217. }
  218. /* Draw border */
  219. if (!noborder->answer) {
  220. /* Set border color */
  221. D_use_color(colorb);
  222. /* Do the border plotting */
  223. plot_border(size, east, north);
  224. }
  225. D_save_command(G_recreate_command());
  226. D_close_driver();
  227. exit(EXIT_SUCCESS);
  228. }