plot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 extra_y_off;
  135. float grid_off = 3.;
  136. double row_dist, colm_dist;
  137. float font_angle;
  138. struct Cell_head window;
  139. /* geo current region */
  140. G_get_set_window(&window);
  141. /* Adjust south and east back by one pixel for display bug? */
  142. row_dist = D_d_to_u_row(0.) - D_d_to_u_row(1.);
  143. colm_dist = D_d_to_u_col(1.) - D_d_to_u_col(0.);
  144. window.south += row_dist;
  145. window.east -= colm_dist;
  146. /* get lat long min max */
  147. /* probably need something like boardwalk ?? */
  148. get_ll_bounds(&west, &east, &south, &north, window, info_in, info_out);
  149. G_debug(3, "REGION BOUNDS N=%f S=%f E=%f W=%f", north, south, east, west);
  150. /* Lines of Latitude */
  151. g = floor(north / size) * size;
  152. e1 = east;
  153. for (j = 0; g >= south; j++, g -= size) {
  154. start_coord = -9999.;
  155. if (g == north || g == south)
  156. continue;
  157. /* Set grid color */
  158. D_use_color(gcolor);
  159. for (ll = 0; ll < SEGS; ll++) {
  160. n1 = n2 = g;
  161. e1 = west + (ll * ((east - west) / SEGS));
  162. e2 = e1 + ((east - west) / SEGS);
  163. if (pj_do_proj(&e1, &n1, &info_in, &info_out) < 0)
  164. G_fatal_error(_("Error in pj_do_proj"));
  165. check_coords(e1, n1, &lon, &lat, 1, window, info_in, info_out);
  166. e1 = lon;
  167. n1 = lat;
  168. if (pj_do_proj(&e2, &n2, &info_in, &info_out) < 0)
  169. G_fatal_error(_("Error in pj_do_proj"));
  170. check_coords(e2, n2, &lon, &lat, 1, window, info_in, info_out);
  171. e2 = lon;
  172. n2 = lat;
  173. if (start_coord == -9999.) {
  174. start_coord = n1;
  175. font_angle = get_heading((e1 - e2), (n1 - n2));
  176. }
  177. if (line_width)
  178. D_line_width(line_width);
  179. if (mark_type == MARK_GRID)
  180. D_line_abs(e1, n1, e2, n2);
  181. D_line_width(0);
  182. }
  183. if (do_text) {
  184. /* Set text color */
  185. D_use_color(tcolor);
  186. G_format_northing(g, text, PROJECTION_LL);
  187. D_text_rotation(font_angle);
  188. D_text_size(fontsize, fontsize);
  189. D_pos_abs(D_get_u_west() + D_get_d_to_u_xconv() * border_off,
  190. start_coord - D_get_d_to_u_yconv() * grid_off);
  191. D_text(text);
  192. }
  193. }
  194. /* Lines of Longitude */
  195. g = floor(east / size) * size;
  196. n1 = north;
  197. for (j = 0; g > west; j++, g -= size) {
  198. start_coord = -9999.;
  199. extra_y_off = 0.0;
  200. if (g == east || g == west)
  201. continue;
  202. /* Set grid color */
  203. D_use_color(gcolor);
  204. for (ll = 0; ll < SEGS; ll++) {
  205. e1 = e2 = g;
  206. n1 = north - (ll * ((north - south) / SEGS));
  207. n2 = n1 - ((north - south) / SEGS);
  208. /*
  209. n1 = south + (ll *((north - south)/SEGS));
  210. n2 = n1 + ((north - south)/SEGS);
  211. */
  212. if (pj_do_proj(&e1, &n1, &info_in, &info_out) < 0)
  213. G_fatal_error(_("Error in pj_do_proj"));
  214. check_coords(e1, n1, &lon, &lat, 2, window, info_in, info_out);
  215. e1 = lon;
  216. n1 = lat;
  217. if (pj_do_proj(&e2, &n2, &info_in, &info_out) < 0)
  218. G_fatal_error(_("Error in pj_do_proj"));
  219. check_coords(e2, n2, &lon, &lat, 2, window, info_in, info_out);
  220. e2 = lon;
  221. n2 = lat;
  222. if ((start_coord == -9999.) && (D_u_to_a_row(n1) > 0)) {
  223. font_angle = get_heading((e1 - e2), (n1 - n2));
  224. start_coord = e1;
  225. /* font rotates by bottom-left corner, try to keep top-left cnr on screen */
  226. if(font_angle - 270 > 0) {
  227. extra_y_off = sin((font_angle - 270) * M_PI/180) * fontsize;
  228. if(D_u_to_a_row(n1) < extra_y_off)
  229. start_coord = -9999.; /* wait until the next point south */
  230. }
  231. }
  232. if (line_width)
  233. D_line_width(line_width);
  234. if (mark_type == MARK_GRID)
  235. D_line_abs(e1, n1, e2, n2);
  236. D_line_width(0);
  237. }
  238. if (do_text) {
  239. /* Set text color */
  240. D_use_color(tcolor);
  241. G_format_easting(g, text, PROJECTION_LL);
  242. D_text_rotation(font_angle);
  243. D_text_size(fontsize, fontsize);
  244. D_pos_abs(start_coord + D_get_d_to_u_xconv() * (grid_off + 1.5),
  245. D_get_u_north() + D_get_d_to_u_yconv() *
  246. (border_off + extra_y_off));
  247. D_text(text);
  248. }
  249. }
  250. D_text_rotation(0.0); /* reset */
  251. /* draw marks not grid lines */
  252. if (mark_type != MARK_GRID) {
  253. G_warning("Geogrid marks not yet implemented");
  254. #ifdef TODO
  255. e1 = combine above;
  256. n1 = combine above;
  257. /* plot marks */
  258. while (e1 <= window.east) {
  259. n1 = y0; /* reset */
  260. while (n1 <= window.north) {
  261. if (mark_type == MARK_CROSS)
  262. plot_cross(e1, n1, gcolor, 0.0);
  263. else if (mark_type == MARK_FIDUCIAL)
  264. plot_fiducial(e1, n1, gcolor, 0.0);
  265. else if (mark_type == MARK_DOT)
  266. plot_dot(e1, n1, gcolor);
  267. n1 += grid_size;
  268. }
  269. e1 += grid_size;
  270. }
  271. #endif
  272. /* also TODO: rotate cross and fiducial marks by the converge angle; see g.region -n */
  273. }
  274. return 0;
  275. }
  276. /******************************************************
  277. * initialze projection stuff and return proj structures
  278. ********************************************************/
  279. void init_proj(struct pj_info *info_in, struct pj_info *info_out, int wgs84)
  280. {
  281. struct Key_Value *out_proj_keys, *out_unit_keys;
  282. /* Proj stuff for geo grid */
  283. /* Out Info */
  284. out_proj_keys = G_get_projinfo();
  285. out_unit_keys = G_get_projunits();
  286. if (pj_get_kv(info_out, out_proj_keys, out_unit_keys) < 0)
  287. G_fatal_error(_("Can't get projection key values of current location"));
  288. /* In Info */
  289. if (!wgs84) {
  290. /* Set lat/long to same ellipsoid as location if we're not looking
  291. * for the WGS84 values */
  292. if (GPJ_get_equivalent_latlong(info_in, info_out) < 0)
  293. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  294. }
  295. else {
  296. struct Key_Value *in_proj_info, *in_unit_info;
  297. char buff[100], dum[100];
  298. in_proj_info = G_create_key_value();
  299. in_unit_info = G_create_key_value();
  300. /* Check that datumparams are defined for this location (otherwise
  301. * the WGS84 values would be meaningless), and if they are set the
  302. * input datum to WGS84 */
  303. if (G_get_datumparams_from_projinfo(out_proj_keys, buff, dum) < 0)
  304. G_fatal_error(_("WGS84 grid output not possible as this location does not contain\n"
  305. "datum transformation parameters. Try running g.setproj."));
  306. else
  307. G_set_key_value("datum", "wgs84", in_proj_info);
  308. /* set input projection to lat/long */
  309. G_set_key_value("proj", "ll", in_proj_info);
  310. G_set_key_value("unit", "degree", in_unit_info);
  311. G_set_key_value("units", "degrees", in_unit_info);
  312. G_set_key_value("meters", "1.0", in_unit_info);
  313. if (pj_get_kv(info_in, in_proj_info, in_unit_info) < 0)
  314. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  315. G_free_key_value(in_proj_info);
  316. G_free_key_value(in_unit_info);
  317. }
  318. G_free_key_value(out_proj_keys);
  319. G_free_key_value(out_unit_keys);
  320. return;
  321. }
  322. /******************************************************
  323. * Use Proj to get min max bounds of region in lat long
  324. ********************************************************/
  325. void
  326. get_ll_bounds(double *w,
  327. double *e,
  328. double *s,
  329. double *n,
  330. struct Cell_head window,
  331. struct pj_info info_in, struct pj_info info_out)
  332. {
  333. double east, west, north, south;
  334. double e1, w1, n1, s1;
  335. double ew, ns;
  336. double ew_res, ns_res;
  337. int first;
  338. *w = *e = *n = *s = -999.;
  339. west = east = north = south = -999.;
  340. e1 = window.east;
  341. w1 = window.west;
  342. n1 = window.north;
  343. s1 = window.south;
  344. /* calculate resolution based upon 100 rows/cols
  345. * to prevent possible time consuming parsing in large regions
  346. */
  347. ew_res = (e1 - w1) / 100.;
  348. ns_res = (n1 - s1) / 100.;
  349. /* Get geographic min max from ala boardwalk style */
  350. /* North */
  351. first = 0;
  352. for (ew = window.west; ew <= window.east; ew += ew_res) {
  353. e1 = ew;
  354. n1 = window.north;
  355. if (pj_do_proj(&e1, &n1, &info_out, &info_in) < 0)
  356. G_fatal_error(_("Error in pj_do_proj"));
  357. if (!first) {
  358. north = n1;
  359. first = 1;
  360. }
  361. else {
  362. if (n1 > north)
  363. north = n1;
  364. }
  365. }
  366. /*South */
  367. first = 0;
  368. for (ew = window.west; ew <= window.east; ew += ew_res) {
  369. e1 = ew;
  370. s1 = window.south;
  371. if (pj_do_proj(&e1, &s1, &info_out, &info_in) < 0)
  372. G_fatal_error(_("Error in pj_do_proj"));
  373. if (!first) {
  374. south = s1;
  375. first = 1;
  376. }
  377. else {
  378. if (s1 < south)
  379. south = s1;
  380. }
  381. }
  382. /*East */
  383. first = 0;
  384. for (ns = window.south; ns <= window.north; ns += ns_res) {
  385. e1 = window.east;
  386. n1 = ns;
  387. if (pj_do_proj(&e1, &n1, &info_out, &info_in) < 0)
  388. G_fatal_error(_("Error in pj_do_proj"));
  389. if (!first) {
  390. east = e1;
  391. first = 1;
  392. }
  393. else {
  394. if (e1 > east)
  395. east = e1;
  396. }
  397. }
  398. /*West */
  399. first = 0;
  400. for (ns = window.south; ns <= window.north; ns += ns_res) {
  401. w1 = window.west;
  402. n1 = ns;
  403. if (pj_do_proj(&w1, &n1, &info_out, &info_in) < 0)
  404. G_fatal_error(_("Error in pj_do_proj"));
  405. if (!first) {
  406. west = w1;
  407. first = 1;
  408. }
  409. else {
  410. if (w1 < west)
  411. west = w1;
  412. }
  413. }
  414. *w = west;
  415. *e = east;
  416. *s = south;
  417. *n = north;
  418. return;
  419. }
  420. /******************************************************
  421. * check projected coords to make sure they do not
  422. * go outside region -- if so re-project
  423. ********************************************************/
  424. void
  425. check_coords(double e,
  426. double n,
  427. double *lon,
  428. double *lat,
  429. int par,
  430. struct Cell_head w,
  431. struct pj_info info_in, struct pj_info info_out)
  432. {
  433. double x, y;
  434. int proj = 0;
  435. *lat = y = n;
  436. *lon = x = e;
  437. if (e < w.west) {
  438. x = w.west;
  439. proj = 1;
  440. }
  441. if (e > w.east) {
  442. x = w.east;
  443. proj = 1;
  444. }
  445. if (n < w.south) {
  446. y = w.south;
  447. proj = 1;
  448. }
  449. if (n > w.north) {
  450. y = w.north;
  451. proj = 1;
  452. }
  453. if (proj) {
  454. /* convert original coords to ll */
  455. if (pj_do_proj(&e, &n, &info_out, &info_in) < 0)
  456. G_fatal_error(_("Error in pj_do_proj1"));
  457. if (par == 1) {
  458. /* lines of latitude -- const. northing */
  459. /* convert correct UTM to ll */
  460. if (pj_do_proj(&x, &y, &info_out, &info_in) < 0)
  461. G_fatal_error(_("Error in pj_do_proj2"));
  462. /* convert new ll back to coords */
  463. if (pj_do_proj(&x, &n, &info_in, &info_out) < 0)
  464. G_fatal_error(_("Error in pj_do_proj3"));
  465. *lat = n;
  466. *lon = x;
  467. }
  468. if (par == 2) {
  469. /* lines of longitude -- const. easting */
  470. /* convert correct UTM to ll */
  471. if (pj_do_proj(&x, &y, &info_out, &info_in) < 0)
  472. G_fatal_error(_("Error in pj_do_proj5"));
  473. /* convert new ll back to coords */
  474. if (pj_do_proj(&e, &y, &info_in, &info_out) < 0)
  475. G_fatal_error(_("Error in pj_do_proj6"));
  476. *lat = y;
  477. *lon = e;
  478. }
  479. }
  480. return;
  481. }
  482. /*******************************************
  483. * function to calculate azimuth in degrees
  484. * from rows and columns
  485. *******************************************/
  486. float get_heading(double rows, double cols)
  487. {
  488. float azi;
  489. /* NE Quad or due south */
  490. if (rows < 0 && cols <= 0) {
  491. azi = RAD_TO_DEG * atan((cols / rows));
  492. if (azi < 0.)
  493. azi *= -1.;
  494. }
  495. /* SE Quad or due east */
  496. if (rows >= 0 && cols < 0) {
  497. azi = RAD_TO_DEG * atan((rows / cols));
  498. if (azi < 0.)
  499. azi *= -1.;
  500. azi = 90. + azi;
  501. }
  502. /* SW Quad or due south */
  503. if (rows > 0 && cols >= 0) {
  504. azi = RAD_TO_DEG * atan((rows / cols));
  505. if (azi < 0.)
  506. azi *= -1.;
  507. azi = 270. - azi;
  508. }
  509. /* NW Quad or due south */
  510. if (rows <= 0 && cols > 0) {
  511. azi = RAD_TO_DEG * atan((rows / cols));
  512. if (azi < 0.)
  513. azi *= -1.;
  514. azi = 270. + azi;
  515. }
  516. return (azi);
  517. }