main.c 8.1 KB

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