plot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. #include <math.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include <grass/display.h>
  6. #include <grass/gprojects.h>
  7. #include <grass/glocale.h>
  8. #include "local_proto.h"
  9. int plot_grid(double grid_size, double east, double north, int do_text,
  10. int gcolor, int tcolor, int fontsize, int mark_type)
  11. {
  12. double x, y, y0;
  13. double e1, e2;
  14. struct Cell_head window;
  15. double row_dist, colm_dist;
  16. char text[128];
  17. G_get_set_window(&window);
  18. /* pull right and bottom edges back one pixel; display lib bug? */
  19. row_dist = D_d_to_u_row(0.) - D_d_to_u_row(1.);
  20. colm_dist = D_d_to_u_col(1.) - D_d_to_u_col(0.);
  21. window.south = window.south + row_dist;
  22. window.east = window.east - colm_dist;
  23. /* Draw vertical grids */
  24. if (window.west > east)
  25. x = ceil((window.west - east) / grid_size) * grid_size + east;
  26. else
  27. x = east - ceil((east - window.west) / grid_size) * grid_size;
  28. while (x <= window.east) {
  29. if (mark_type == MARK_GRID) {
  30. D_use_color(gcolor);
  31. D_line_abs(x, window.north, x, window.south);
  32. }
  33. if (do_text) {
  34. D_use_color(tcolor);
  35. G_format_easting(x, text, G_projection());
  36. D_text_rotation(270.0);
  37. D_text_size(fontsize, fontsize);
  38. /* Positioning -
  39. x: 4 pixels to the right of the grid line, + 0.5 rounding factor.
  40. y: End of text is 7 pixels up from bottom of screen, +.5 rounding.
  41. fontsize*.81 = actual text width FOR DEFAULT FONT (NOT FreeType)
  42. */
  43. D_pos_abs(x + 4.5 * D_get_d_to_u_xconv(),
  44. D_get_u_south()
  45. - D_get_d_to_u_yconv() * (strlen(text) * fontsize * 0.81) - 7.5);
  46. D_text(text);
  47. }
  48. x += grid_size;
  49. }
  50. D_text_rotation(0.0); /* reset */
  51. /* Draw horizontal grids
  52. *
  53. * For latlon, must draw in shorter sections
  54. * to make sure that each section of the grid
  55. * line is less than half way around the globe
  56. */
  57. e1 = (window.east * 2 + window.west) / 3;
  58. e2 = (window.west * 2 + window.east) / 3;
  59. if (window.south > north)
  60. y = ceil((window.south - north) / grid_size) * grid_size + north;
  61. else
  62. y = north - ceil((north - window.south) / grid_size) * grid_size;
  63. while (y <= window.north) {
  64. if (mark_type == MARK_GRID) {
  65. D_use_color(gcolor);
  66. D_line_abs(window.east, y, e1, y);
  67. D_line_abs(e1, y, e2, y);
  68. D_line_abs(e2, y, window.west, y);
  69. }
  70. if (do_text) {
  71. D_use_color(tcolor);
  72. G_format_northing(y, text, G_projection());
  73. D_text_size(fontsize, fontsize);
  74. /* Positioning -
  75. x: End of text is 7 pixels left from right edge of screen, +.5 rounding.
  76. fontsize*.81 = actual text width FOR DEFAULT FONT (NOT FreeType)
  77. y: 4 pixels above each grid line, +.5 rounding.
  78. */
  79. D_pos_abs(
  80. D_get_u_east()
  81. - D_get_d_to_u_xconv() * (strlen(text) * fontsize * 0.81 - 7.5),
  82. y + D_get_d_to_u_yconv() * 4.5);
  83. D_text(text);
  84. }
  85. y += grid_size;
  86. }
  87. /* draw marks not grid lines */
  88. if (mark_type != MARK_GRID) {
  89. /* reset x and y */
  90. if (window.west > east)
  91. x = ceil((window.west - east) / grid_size) * grid_size + east;
  92. else
  93. x = east - ceil((east - window.west) / grid_size) * grid_size;
  94. if (window.south > north)
  95. y0 = ceil((window.south - north) / grid_size) * grid_size + north;
  96. else
  97. y0 = north - ceil((north - window.south) / grid_size) * grid_size;
  98. /* plot marks */
  99. while (x <= window.east) {
  100. y = y0; /* reset */
  101. while (y <= window.north) {
  102. if (mark_type == MARK_CROSS)
  103. plot_cross(x, y, gcolor, 0.0);
  104. if (mark_type == MARK_FIDUCIAL)
  105. plot_fiducial(x, y, gcolor, 0.0);
  106. y += grid_size;
  107. }
  108. x += grid_size;
  109. }
  110. }
  111. return 0;
  112. }
  113. int plot_geogrid(double size, struct pj_info info_in, struct pj_info info_out,
  114. int do_text, int gcolor, int tcolor, int fontsize,
  115. int mark_type)
  116. {
  117. double g;
  118. double e1, e2, n1, n2;
  119. double east, west, north, south;
  120. double start_coord;
  121. double lat, lon;
  122. int j, ll;
  123. int SEGS = 100;
  124. char text[128];
  125. float border_off = 4.5;
  126. float grid_off = 3.;
  127. double row_dist, colm_dist;
  128. float font_angle;
  129. struct Cell_head window;
  130. /* geo current region */
  131. G_get_set_window(&window);
  132. /* Adjust south and east back by one pixel for display bug? */
  133. row_dist = D_d_to_u_row(0.) - D_d_to_u_row(1.);
  134. colm_dist = D_d_to_u_col(1.) - D_d_to_u_col(0.);
  135. window.south += row_dist;
  136. window.east -= colm_dist;
  137. /* get lat long min max */
  138. /* probably need something like boardwalk ?? */
  139. get_ll_bounds(&west, &east, &south, &north, window, info_in, info_out);
  140. G_debug(3, "REGION BOUNDS N=%f S=%f E=%f W=%f", north, south, east, west);
  141. /* Lines of Latitude */
  142. g = floor(north / size) * size;
  143. e1 = east;
  144. for (j = 0; g >= south; j++, g -= size) {
  145. start_coord = -9999.;
  146. if (g == north || g == south)
  147. continue;
  148. /* Set grid color */
  149. D_use_color(gcolor);
  150. for (ll = 0; ll < SEGS; ll++) {
  151. n1 = n2 = g;
  152. e1 = west + (ll * ((east - west) / SEGS));
  153. e2 = e1 + ((east - west) / SEGS);
  154. if (pj_do_proj(&e1, &n1, &info_in, &info_out) < 0)
  155. G_fatal_error(_("Error in pj_do_proj"));
  156. check_coords(e1, n1, &lon, &lat, 1, window, info_in, info_out);
  157. e1 = lon;
  158. n1 = lat;
  159. if (pj_do_proj(&e2, &n2, &info_in, &info_out) < 0)
  160. G_fatal_error(_("Error in pj_do_proj"));
  161. check_coords(e2, n2, &lon, &lat, 1, window, info_in, info_out);
  162. e2 = lon;
  163. n2 = lat;
  164. if (start_coord == -9999.) {
  165. start_coord = n1;
  166. font_angle = get_heading((e1 - e2), (n1 - n2));
  167. }
  168. D_line_abs(e1, n1, e2, n2);
  169. }
  170. if (do_text) {
  171. /* Set text color */
  172. D_use_color(tcolor);
  173. G_format_northing(g, text, PROJECTION_LL);
  174. D_text_rotation(font_angle);
  175. D_text_size(fontsize, fontsize);
  176. D_pos_abs(D_get_u_west() + D_get_d_to_u_xconv() * border_off,
  177. start_coord - D_get_d_to_u_yconv() * grid_off);
  178. D_text(text);
  179. }
  180. }
  181. /* Lines of Longitude */
  182. g = floor(east / size) * size;
  183. n1 = north;
  184. for (j = 0; g > west; j++, g -= size) {
  185. start_coord = -9999.;
  186. if (g == east || g == west)
  187. continue;
  188. /* Set grid color */
  189. D_use_color(gcolor);
  190. for (ll = 0; ll < SEGS; ll++) {
  191. e1 = e2 = g;
  192. n1 = north - (ll * ((north - south) / SEGS));
  193. n2 = n1 - ((north - south) / SEGS);
  194. /*
  195. n1 = south + (ll *((north - south)/SEGS));
  196. n2 = n1 + ((north - south)/SEGS);
  197. */
  198. if (pj_do_proj(&e1, &n1, &info_in, &info_out) < 0)
  199. G_fatal_error(_("Error in pj_do_proj"));
  200. check_coords(e1, n1, &lon, &lat, 2, window, info_in, info_out);
  201. e1 = lon;
  202. n1 = lat;
  203. if (pj_do_proj(&e2, &n2, &info_in, &info_out) < 0)
  204. G_fatal_error(_("Error in pj_do_proj"));
  205. check_coords(e2, n2, &lon, &lat, 2, window, info_in, info_out);
  206. e2 = lon;
  207. n2 = lat;
  208. if (start_coord == -9999.) {
  209. font_angle = get_heading((e1 - e2), (n1 - n2));
  210. start_coord = e1;
  211. }
  212. D_line_abs(e1, n1, e2, n2);
  213. }
  214. if (do_text) {
  215. /* Set text color */
  216. D_use_color(tcolor);
  217. G_format_easting(g, text, PROJECTION_LL);
  218. D_text_rotation(font_angle);
  219. D_text_size(fontsize, fontsize);
  220. D_pos_abs(start_coord + D_get_d_to_u_xconv() * (grid_off + 1.5),
  221. D_get_u_north() + D_get_d_to_u_yconv() * border_off);
  222. D_text(text);
  223. }
  224. }
  225. D_text_rotation(0.0); /* reset */
  226. return 0;
  227. }
  228. /******************************************************
  229. * initialze projection stuff and return proj structures
  230. ********************************************************/
  231. void init_proj(struct pj_info *info_in, struct pj_info *info_out, int wgs84)
  232. {
  233. struct Key_Value *out_proj_keys, *out_unit_keys;
  234. /* Proj stuff for geo grid */
  235. /* Out Info */
  236. out_proj_keys = G_get_projinfo();
  237. out_unit_keys = G_get_projunits();
  238. if (pj_get_kv(info_out, out_proj_keys, out_unit_keys) < 0)
  239. G_fatal_error(_("Can't get projection key values of current location"));
  240. /* In Info */
  241. if (!wgs84) {
  242. /* Set lat/long to same ellipsoid as location if we're not looking
  243. * for the WGS84 values */
  244. if (GPJ_get_equivalent_latlong(info_in, info_out) < 0)
  245. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  246. }
  247. else {
  248. struct Key_Value *in_proj_info, *in_unit_info;
  249. char buff[100], dum[100];
  250. in_proj_info = G_create_key_value();
  251. in_unit_info = G_create_key_value();
  252. /* Check that datumparams are defined for this location (otherwise
  253. * the WGS84 values would be meaningless), and if they are set the
  254. * input datum to WGS84 */
  255. if (G_get_datumparams_from_projinfo(out_proj_keys, buff, dum) < 0)
  256. G_fatal_error(_("WGS84 grid output not possible as this location does not contain\n"
  257. "datum transformation parameters. Try running g.setproj."));
  258. else
  259. G_set_key_value("datum", "wgs84", in_proj_info);
  260. /* set input projection to lat/long */
  261. G_set_key_value("proj", "ll", in_proj_info);
  262. G_set_key_value("unit", "degree", in_unit_info);
  263. G_set_key_value("units", "degrees", in_unit_info);
  264. G_set_key_value("meters", "1.0", in_unit_info);
  265. if (pj_get_kv(info_in, in_proj_info, in_unit_info) < 0)
  266. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  267. G_free_key_value(in_proj_info);
  268. G_free_key_value(in_unit_info);
  269. }
  270. G_free_key_value(out_proj_keys);
  271. G_free_key_value(out_unit_keys);
  272. return;
  273. }
  274. /******************************************************
  275. * Use Proj to get min max bounds of region in lat long
  276. ********************************************************/
  277. void
  278. get_ll_bounds(double *w,
  279. double *e,
  280. double *s,
  281. double *n,
  282. struct Cell_head window,
  283. struct pj_info info_in, struct pj_info info_out)
  284. {
  285. double east, west, north, south;
  286. double e1, w1, n1, s1;
  287. double ew, ns;
  288. double ew_res, ns_res;
  289. int first;
  290. *w = *e = *n = *s = -999.;
  291. west = east = north = south = -999.;
  292. e1 = window.east;
  293. w1 = window.west;
  294. n1 = window.north;
  295. s1 = window.south;
  296. /* calculate resolution based upon 100 rows/cols
  297. * to prevent possible time consuming parsing in large regions
  298. */
  299. ew_res = (e1 - w1) / 100.;
  300. ns_res = (n1 - s1) / 100.;
  301. /* Get geographic min max from ala boardwalk style */
  302. /* North */
  303. first = 0;
  304. for (ew = window.west; ew <= window.east; ew += ew_res) {
  305. e1 = ew;
  306. n1 = window.north;
  307. if (pj_do_proj(&e1, &n1, &info_out, &info_in) < 0)
  308. G_fatal_error(_("Error in pj_do_proj"));
  309. if (!first) {
  310. north = n1;
  311. first = 1;
  312. }
  313. else {
  314. if (n1 > north)
  315. north = n1;
  316. }
  317. }
  318. /*South */
  319. first = 0;
  320. for (ew = window.west; ew <= window.east; ew += ew_res) {
  321. e1 = ew;
  322. s1 = window.south;
  323. if (pj_do_proj(&e1, &s1, &info_out, &info_in) < 0)
  324. G_fatal_error(_("Error in pj_do_proj"));
  325. if (!first) {
  326. south = s1;
  327. first = 1;
  328. }
  329. else {
  330. if (s1 < south)
  331. south = s1;
  332. }
  333. }
  334. /*East */
  335. first = 0;
  336. for (ns = window.south; ns <= window.north; ns += ns_res) {
  337. e1 = window.east;
  338. n1 = ns;
  339. if (pj_do_proj(&e1, &n1, &info_out, &info_in) < 0)
  340. G_fatal_error(_("Error in pj_do_proj"));
  341. if (!first) {
  342. east = e1;
  343. first = 1;
  344. }
  345. else {
  346. if (e1 > east)
  347. east = e1;
  348. }
  349. }
  350. /*West */
  351. first = 0;
  352. for (ns = window.south; ns <= window.north; ns += ns_res) {
  353. w1 = window.west;
  354. n1 = ns;
  355. if (pj_do_proj(&w1, &n1, &info_out, &info_in) < 0)
  356. G_fatal_error(_("Error in pj_do_proj"));
  357. if (!first) {
  358. west = w1;
  359. first = 1;
  360. }
  361. else {
  362. if (w1 < west)
  363. west = w1;
  364. }
  365. }
  366. *w = west;
  367. *e = east;
  368. *s = south;
  369. *n = north;
  370. return;
  371. }
  372. /******************************************************
  373. * check projected coords to make sure they do not
  374. * go outside region -- if so re-project
  375. ********************************************************/
  376. void
  377. check_coords(double e,
  378. double n,
  379. double *lon,
  380. double *lat,
  381. int par,
  382. struct Cell_head w,
  383. struct pj_info info_in, struct pj_info info_out)
  384. {
  385. double x, y;
  386. int proj = 0;
  387. *lat = y = n;
  388. *lon = x = e;
  389. if (e < w.west) {
  390. x = w.west;
  391. proj = 1;
  392. }
  393. if (e > w.east) {
  394. x = w.east;
  395. proj = 1;
  396. }
  397. if (n < w.south) {
  398. y = w.south;
  399. proj = 1;
  400. }
  401. if (n > w.north) {
  402. y = w.north;
  403. proj = 1;
  404. }
  405. if (proj) {
  406. /* convert original coords to ll */
  407. if (pj_do_proj(&e, &n, &info_out, &info_in) < 0)
  408. G_fatal_error(_("Error in pj_do_proj1"));
  409. if (par == 1) {
  410. /* lines of latitude -- const. northing */
  411. /* convert correct UTM to ll */
  412. if (pj_do_proj(&x, &y, &info_out, &info_in) < 0)
  413. G_fatal_error(_("Error in pj_do_proj2"));
  414. /* convert new ll back to coords */
  415. if (pj_do_proj(&x, &n, &info_in, &info_out) < 0)
  416. G_fatal_error(_("Error in pj_do_proj3"));
  417. *lat = n;
  418. *lon = x;
  419. }
  420. if (par == 2) {
  421. /* lines of longitude -- const. easting */
  422. /* convert correct UTM to ll */
  423. if (pj_do_proj(&x, &y, &info_out, &info_in) < 0)
  424. G_fatal_error(_("Error in pj_do_proj5"));
  425. /* convert new ll back to coords */
  426. if (pj_do_proj(&e, &y, &info_in, &info_out) < 0)
  427. G_fatal_error(_("Error in pj_do_proj6"));
  428. *lat = y;
  429. *lon = e;
  430. }
  431. }
  432. return;
  433. }
  434. /*******************************************
  435. * function to calculate azimuth in degrees
  436. * from rows and columns
  437. *******************************************/
  438. float get_heading(double rows, double cols)
  439. {
  440. float azi;
  441. /* NE Quad or due south */
  442. if (rows < 0 && cols <= 0) {
  443. azi = RAD_TO_DEG * atan((cols / rows));
  444. if (azi < 0.)
  445. azi *= -1.;
  446. }
  447. /* SE Quad or due east */
  448. if (rows >= 0 && cols < 0) {
  449. azi = RAD_TO_DEG * atan((rows / cols));
  450. if (azi < 0.)
  451. azi *= -1.;
  452. azi = 90. + azi;
  453. }
  454. /* SW Quad or due south */
  455. if (rows > 0 && cols >= 0) {
  456. azi = RAD_TO_DEG * atan((rows / cols));
  457. if (azi < 0.)
  458. azi *= -1.;
  459. azi = 270. - azi;
  460. }
  461. /* NW Quad or due south */
  462. if (rows <= 0 && cols > 0) {
  463. azi = RAD_TO_DEG * atan((rows / cols));
  464. if (azi < 0.)
  465. azi *= -1.;
  466. azi = 270. + azi;
  467. }
  468. return (azi);
  469. }