main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. int colorbg = 0;
  32. double size = 0., gsize = 0.; /* initialize to zero */
  33. double east, north;
  34. int do_text, fontsize, mark_type, line_width, dirn;
  35. struct GModule *module;
  36. struct Option *opt1, *opt2, *opt3, *opt4, *fsize, *tcolor, *lwidth,
  37. *direction, *bgcolor;
  38. struct Flag *noborder, *notext, *geogrid, *nogrid, *wgs84, *cross,
  39. *fiducial, *dot, *align;
  40. struct pj_info info_in; /* Proj structures */
  41. struct pj_info info_out; /* Proj structures */
  42. struct Cell_head wind;
  43. /* Initialize the GIS calls */
  44. G_gisinit(argv[0]);
  45. module = G_define_module();
  46. G_add_keyword(_("display"));
  47. G_add_keyword(_("cartography"));
  48. G_add_keyword(_("graticule"));
  49. G_add_keyword(_("grid"));
  50. module->description =
  51. _("Overlays a user-specified grid "
  52. "in the active display frame on the graphics monitor.");
  53. opt2 = G_define_option();
  54. opt2->key = "size";
  55. opt2->key_desc = "value";
  56. opt2->type = TYPE_STRING;
  57. opt2->required = YES;
  58. opt2->label = _("Size of grid to be drawn (in map units)");
  59. opt2->description =
  60. _("0 for north-south resolution of the current region. "
  61. "In map units or DDD:MM:SS format. "
  62. "Example: \"1000\" or \"0:10\"");
  63. opt3 = G_define_standard_option(G_OPT_M_COORDS);
  64. opt3->key = "origin";
  65. opt3->answer = "0,0";
  66. opt3->multiple = NO;
  67. opt3->description = _("Lines of the grid pass through this coordinate");
  68. direction = G_define_option();
  69. direction->key = "direction";
  70. direction->type = TYPE_STRING;
  71. direction->required = NO;
  72. direction->answer = "both";
  73. direction->options = "both,east-west,north-south";
  74. direction->description =
  75. _("Draw only east-west lines, north-south lines, or both ");
  76. direction->guisection = _("Disable");
  77. lwidth = G_define_option();
  78. lwidth->key = "width";
  79. lwidth->type = TYPE_DOUBLE;
  80. lwidth->required = NO;
  81. lwidth->description = _("Grid line width");
  82. opt1 = G_define_standard_option(G_OPT_C);
  83. opt1->answer = "gray";
  84. opt1->label = _("Grid color");
  85. opt1->guisection = _("Color");
  86. opt4 = G_define_standard_option(G_OPT_C);
  87. opt4->key = "border_color";
  88. opt4->label = _("Border color");
  89. opt4->guisection = _("Color");
  90. tcolor = G_define_standard_option(G_OPT_C);
  91. tcolor->key = "text_color";
  92. tcolor->answer = "gray";
  93. tcolor->label = _("Text color");
  94. tcolor->guisection = _("Color");
  95. bgcolor = G_define_standard_option(G_OPT_CN);
  96. bgcolor->key = "bgcolor";
  97. bgcolor->answer = "none";
  98. bgcolor->label = _("Background color");
  99. bgcolor->guisection = _("Color");
  100. fsize = G_define_option();
  101. fsize->key = "fontsize";
  102. fsize->type = TYPE_INTEGER;
  103. fsize->required = NO;
  104. fsize->answer = "9";
  105. fsize->options = "1-72";
  106. fsize->description = _("Font size for gridline coordinate labels");
  107. align = G_define_flag();
  108. align->key = 'a';
  109. align->description =
  110. _("Align the origin to the east-north corner of the current region");
  111. geogrid = G_define_flag();
  112. geogrid->key = 'g';
  113. geogrid->description =
  114. _("Draw geographic grid (referenced to current ellipsoid)");
  115. geogrid->guisection = _("Draw");
  116. wgs84 = G_define_flag();
  117. wgs84->key = 'w';
  118. wgs84->description =
  119. _("Draw geographic grid (referenced to WGS84 ellipsoid)");
  120. wgs84->guisection = _("Draw");
  121. cross = G_define_flag();
  122. cross->key = 'c';
  123. cross->description = _("Draw '+' marks instead of grid lines");
  124. cross->guisection = _("Draw");
  125. dot = G_define_flag();
  126. dot->key = 'd';
  127. dot->description = _("Draw '.' marks instead of grid lines");
  128. dot->guisection = _("Draw");
  129. fiducial = G_define_flag();
  130. fiducial->key = 'f';
  131. fiducial->description = _("Draw fiducial marks instead of grid lines");
  132. fiducial->guisection = _("Draw");
  133. nogrid = G_define_flag();
  134. nogrid->key = 'n';
  135. nogrid->description = _("Disable grid drawing");
  136. nogrid->guisection = _("Disable");
  137. noborder = G_define_flag();
  138. noborder->key = 'b';
  139. noborder->description = _("Disable border drawing");
  140. noborder->guisection = _("Disable");
  141. notext = G_define_flag();
  142. notext->key = 't';
  143. notext->description = _("Disable text drawing");
  144. notext->guisection = _("Disable");
  145. /* Check command line */
  146. if (G_parser(argc, argv))
  147. exit(EXIT_FAILURE);
  148. /* do some checking */
  149. if (nogrid->answer && noborder->answer)
  150. G_fatal_error(_("Both grid and border drawing are disabled"));
  151. if (wgs84->answer)
  152. geogrid->answer = 1; /* -w implies -g */
  153. if (geogrid->answer && G_projection() == PROJECTION_LL)
  154. G_fatal_error(_("Geo-grid option not available for LL projection, use without -g/-w"));
  155. if (geogrid->answer && G_projection() == PROJECTION_XY)
  156. G_fatal_error(_("Geo-grid option not available for XY projection, use without -g/-w"));
  157. if (notext->answer)
  158. do_text = FALSE;
  159. else
  160. do_text = TRUE;
  161. if (lwidth->answer) {
  162. line_width = atoi(lwidth->answer);
  163. if (line_width < 0 || line_width > 1e3)
  164. G_fatal_error("Invalid line width");
  165. }
  166. else
  167. line_width = 0;
  168. fontsize = atoi(fsize->answer);
  169. mark_type = MARK_GRID;
  170. if (cross->answer + fiducial->answer + dot->answer > 1)
  171. G_fatal_error(_("Choose a single mark style"));
  172. if (cross->answer)
  173. mark_type = MARK_CROSS;
  174. if (fiducial->answer)
  175. mark_type = MARK_FIDUCIAL;
  176. if (dot->answer)
  177. mark_type = MARK_DOT;
  178. if (G_strcasecmp(direction->answer, "both") == 0)
  179. dirn = DIRN_BOTH;
  180. else if (G_strcasecmp(direction->answer, "east-west") == 0)
  181. dirn = DIRN_LAT;
  182. else if (G_strcasecmp(direction->answer, "north-south") == 0)
  183. dirn = DIRN_LON;
  184. else
  185. G_fatal_error("Invalid direction: %s", direction->answer);
  186. if (align->answer || strcmp(opt2->answer, "0") == 0)
  187. G_get_element_window(&wind, "", "WIND", G_mapset());
  188. if (strcmp(opt2->answer, "0") == 0) {
  189. if (geogrid->answer)
  190. gsize = wind.ns_res;
  191. else
  192. size = wind.ns_res;
  193. }
  194. else {
  195. /* get grid size */
  196. if (geogrid->answer) {
  197. if (!G_scan_resolution(opt2->answer, &gsize, PROJECTION_LL) ||
  198. gsize <= 0.0)
  199. G_fatal_error(_("Invalid geo-grid size <%s>"), opt2->answer);
  200. }
  201. else {
  202. if (!G_scan_resolution(opt2->answer, &size, G_projection()) ||
  203. size <= 0.0)
  204. G_fatal_error(_("Invalid grid size <%s>"), opt2->answer);
  205. }
  206. }
  207. if (align->answer) {
  208. /* reduce accumulated errors when ew_res is not the same as ns_res. */
  209. struct Cell_head w;
  210. G_get_set_window(&w);
  211. east =
  212. wind.west +
  213. (int)((w.west - wind.west) / wind.ew_res) * wind.ew_res;
  214. north =
  215. wind.south +
  216. (int)((w.south - wind.south) / wind.ns_res) * wind.ns_res;
  217. }
  218. else {
  219. /* get grid easting start */
  220. if (!G_scan_easting(opt3->answers[0], &east, G_projection())) {
  221. G_usage();
  222. G_fatal_error(_("Illegal east coordinate <%s>"),
  223. opt3->answers[0]);
  224. }
  225. /* get grid northing start */
  226. if (!G_scan_northing(opt3->answers[1], &north, G_projection())) {
  227. G_usage();
  228. G_fatal_error(_("Illegal north coordinate <%s>"),
  229. opt3->answers[1]);
  230. }
  231. }
  232. /* Setup driver and check important information */
  233. D_open_driver();
  234. /* Parse and select grid color */
  235. colorg = D_parse_color(opt1->answer, FALSE);
  236. /* Parse and select border color */
  237. colorb = D_parse_color(opt4->answer, FALSE);
  238. /* Parse and select text color */
  239. colort = D_parse_color(tcolor->answer, FALSE);
  240. /* Parse and select background color */
  241. colorbg = D_parse_color(bgcolor->answer, TRUE);
  242. D_setup(0);
  243. /* draw grid */
  244. if (!nogrid->answer) {
  245. if (geogrid->answer) {
  246. /* initialzie proj stuff */
  247. init_proj(&info_in, &info_out, wgs84->answer);
  248. plot_geogrid(gsize, info_in, info_out, do_text, colorg, colort,
  249. colorbg, fontsize, mark_type, line_width, dirn);
  250. }
  251. else {
  252. /* Do the grid plotting */
  253. plot_grid(size, east, north, do_text, colorg, colort, colorbg,
  254. fontsize, mark_type, line_width, dirn);
  255. }
  256. }
  257. /* Draw border */
  258. if (!noborder->answer) {
  259. /* Set border color */
  260. D_use_color(colorb);
  261. /* Do the border plotting */
  262. plot_border(size, east, north, dirn);
  263. }
  264. D_save_command(G_recreate_command());
  265. D_close_driver();
  266. exit(EXIT_SUCCESS);
  267. }