plot.c 15 KB

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