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