r_instructions.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include <string.h>
  2. #include <grass/colors.h>
  3. #include <grass/imagery.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. #define KEY(x) (strcmp(key,x)==0)
  7. extern FILE *inputfd;
  8. extern int do_mapinfo;
  9. extern int do_vlegend;
  10. extern int ps_copies;
  11. static char *help[] = {
  12. "cell rastermap rast rastermap",
  13. "raster rastermap group imagery group",
  14. "greyrast greyscale rastermap grayrast grayscale rastermap",
  15. "rgb 3 rastermaps for RGB setcolor val_range(s) color",
  16. "vpoints vector points map scalebar [f|s]",
  17. "vlines vector lines map paper [a4|a3|us-letter|...]",
  18. "vareas vector areas map maploc x y [width height]",
  19. "labels labelfile text east north text",
  20. "region regionfile line east north east north",
  21. "grid spacing point east north",
  22. "geogrid spacing header header text",
  23. "colortable [y|n] vlegend vector legend",
  24. "comments [unix-file] psfile PostScript include file",
  25. "read unix-file eps Encapsulated PostScript file",
  26. "border [y|n] mapinfo map information",
  27. "window region definition region region definition",
  28. "maskcolor MASK color",
  29. "rectangle east north east north",
  30. "scale 1:#|# inches|# panels|1 inch = # miles",
  31. "outline map composition outline",
  32. "copies number of copies",
  33. ""
  34. };
  35. void read_instructions(int copies_set, int can_reset_scale)
  36. {
  37. int i;
  38. int iflag;
  39. /* name can be fully qualified */
  40. char name[GNAME_MAX + GMAPSET_MAX], mapset[GMAPSET_MAX];
  41. char buf[1024];
  42. iflag = 0;
  43. while (1) {
  44. char *key;
  45. char *data;
  46. if (!input(1, buf, help)) {
  47. if (!iflag) {
  48. /* TODO: check also if instructions are piped in
  49. * through stdin, not interactive */
  50. if (inputfd != stdin) {
  51. while (G_getl2(buf, 12, inputfd)) {
  52. /* empty lines and comments are fine */
  53. if (key_data(buf, &key, &data))
  54. G_warning(_("Data exist after final 'end' instruction!"));
  55. }
  56. fclose(inputfd);
  57. inputfd = stdin;
  58. }
  59. break;
  60. }
  61. iflag = 0;
  62. continue;
  63. }
  64. if (!key_data(buf, &key, &data))
  65. continue;
  66. if (KEY("read")) {
  67. if (inputfd != stdin)
  68. fclose(inputfd);
  69. if (sscanf(data, "%s", name) != 1) {
  70. error(key, data, _("no file specified"));
  71. inputfd = stdin;
  72. }
  73. else if ((inputfd = fopen(name, "r")) == NULL) {
  74. error(key, data, _("unable to open"));
  75. inputfd = stdin;
  76. }
  77. else
  78. iflag = 1;
  79. continue;
  80. }
  81. if (KEY("paper")) {
  82. if (strlen(data) > 0) {
  83. set_paper(data);
  84. }
  85. read_paper();
  86. continue;
  87. }
  88. if (KEY("maploc")) {
  89. int n;
  90. double x, y, w, h;
  91. n = sscanf(data, "%lf %lf %lf %lf", &x, &y, &w, &h);
  92. if (n == 2 || n == 4) {
  93. PS.map_x_orig = x;
  94. PS.map_y_loc = y;
  95. if (n == 4) {
  96. PS.map_width = w;
  97. PS.map_height = h;
  98. }
  99. }
  100. else {
  101. error(key, data, _("illegal maploc request"));
  102. gobble_input();
  103. }
  104. continue;
  105. }
  106. if (KEY("copies")) {
  107. int n, copies;
  108. if (copies_set)
  109. continue;
  110. n = sscanf(data, "%d", &copies);
  111. if (n != 1 || copies < 1 || copies > 20) {
  112. ps_copies = 1;
  113. error(key, data, _("illegal copies request"));
  114. }
  115. ps_copies = copies;
  116. continue;
  117. }
  118. if (KEY("setcolor")) {
  119. int ret, r, g, b;
  120. int count;
  121. DCELL *val_list;
  122. DCELL dmin, dmax;
  123. char colorbuf[100];
  124. char catsbuf[100];
  125. if (PS.cell_fd < 0)
  126. error(key, data, _("no raster map selected yet"));
  127. if (sscanf(data, "%s %[^\n]", catsbuf, colorbuf) == 2) {
  128. ret = G_str_to_color(colorbuf, &r, &g, &b);
  129. if (ret != 1)
  130. error(key, colorbuf, _("illegal color request"));
  131. if (strncmp(catsbuf, "null", 4) == 0) {
  132. Rast_set_null_value_color(r, g, b, &(PS.colors));
  133. continue;
  134. }
  135. if (strncmp(catsbuf, "default", 7) == 0) {
  136. Rast_set_default_color(r, g, b, &(PS.colors));
  137. continue;
  138. }
  139. if ((count = parse_val_list(catsbuf, &val_list)) < 0)
  140. error(key, data, _("illegal value list"));
  141. for (i = 0; i < count; i += 2) {
  142. dmin = val_list[i];
  143. dmax = val_list[i + 1];
  144. Rast_add_d_color_rule(&dmin, r, g, b, &dmax, r, g, b,
  145. &(PS.colors));
  146. }
  147. G_free(val_list);
  148. }
  149. continue;
  150. }
  151. if (KEY("colortable")) {
  152. PS.do_colortable = 0;
  153. /*
  154. if (PS.cell_fd < 0)
  155. error(key, data, "no raster map selected yet");
  156. else
  157. */
  158. PS.do_colortable = yesno(key, data);
  159. if (PS.do_colortable)
  160. read_colortable();
  161. continue;
  162. }
  163. if (KEY("border")) {
  164. PS.do_border = yesno(key, data);
  165. if (PS.do_border)
  166. read_border();
  167. continue;
  168. }
  169. if (KEY("scalebar")) {
  170. if (G_projection() == PROJECTION_LL) {
  171. error(key, data,
  172. _("scalebar is not appropriate for this projection"));
  173. gobble_input();
  174. }
  175. PS.do_scalebar = 1;
  176. if (sscanf(data, "%s", sb.type) != 1)
  177. strcpy(sb.type, "f"); /* default to fancy scalebar */
  178. read_scalebar();
  179. if (sb.length <= 0.) {
  180. error(key, data, _("Bad scalebar length"));
  181. gobble_input();
  182. }
  183. continue;
  184. }
  185. if (KEY("text")) {
  186. double e, n;
  187. char east[50], north[50];
  188. char text[1024];
  189. if (sscanf(data, "%s %s %[^\n]", east, north, text) == 3
  190. && (scan_easting(east, &e) && scan_northing(north, &n)))
  191. read_text(east, north, text);
  192. else {
  193. gobble_input();
  194. error(key, data, _("illegal text request"));
  195. }
  196. continue;
  197. }
  198. if (KEY("point")) {
  199. double e, n;
  200. char east[50], north[50];
  201. if (sscanf(data, "%s %s", east, north) == 2
  202. && (scan_easting(east, &e) && scan_northing(north, &n)))
  203. read_point(e, n);
  204. else {
  205. gobble_input();
  206. error(key, data, _("illegal point request"));
  207. }
  208. continue;
  209. }
  210. if (KEY("eps")) {
  211. double e, n;
  212. char east[50], north[50];
  213. if (sscanf(data, "%s %s", east, north) == 2
  214. && (scan_easting(east, &e) && scan_northing(north, &n)))
  215. read_eps(e, n);
  216. else {
  217. gobble_input();
  218. error(key, data, _("illegal eps request"));
  219. }
  220. continue;
  221. }
  222. if (KEY("line")) {
  223. char east1[50], north1[50];
  224. char east2[50], north2[50];
  225. double e1, n1, e2, n2;
  226. if (sscanf(data, "%s %s %s %s", east1, north1, east2, north2) == 4
  227. && (scan_easting(east1, &e1) && scan_easting(east2, &e2)
  228. && scan_northing(north1, &n1) &&
  229. scan_northing(north2, &n2)))
  230. read_line(e1, n1, e2, n2);
  231. else {
  232. gobble_input();
  233. error(key, data, _("illegal line request"));
  234. }
  235. continue;
  236. }
  237. if (KEY("rectangle")) {
  238. char east1[50], north1[50];
  239. char east2[50], north2[50];
  240. double e1, n1, e2, n2;
  241. if (sscanf(data, "%s %s %s %s", east1, north1, east2, north2) == 4
  242. && (scan_easting(east1, &e1) && scan_easting(east2, &e2)
  243. && scan_northing(north1, &n1) &&
  244. scan_northing(north2, &n2)))
  245. read_rectangle(e1, n1, e2, n2);
  246. else {
  247. gobble_input();
  248. error(key, data, _("illegal rectangle request"));
  249. }
  250. continue;
  251. }
  252. if (KEY("comments")) {
  253. switch (sscanf(data, "%s %s", name, mapset)) {
  254. case 1:
  255. read_comment(name);
  256. break;
  257. case 2:
  258. error(key, data, _("illegal comments request"));
  259. break;
  260. default:
  261. read_comment("");
  262. break;
  263. }
  264. continue;
  265. }
  266. if (KEY("scale")) {
  267. if (!can_reset_scale)
  268. continue;
  269. if (check_scale(data))
  270. strcpy(PS.scaletext, data);
  271. else {
  272. PS.scaletext[0] = 0;
  273. error(key, data, _("illegal scale request"));
  274. }
  275. continue;
  276. }
  277. if (KEY("labels")) {
  278. if (scan_gis("paint/labels", "label", key, data, name, mapset, 1))
  279. read_labels(name, mapset);
  280. continue;
  281. }
  282. if (KEY("header")) {
  283. read_header();
  284. PS.do_header = 1;
  285. continue;
  286. }
  287. if (KEY("mapinfo")) {
  288. read_info();
  289. do_mapinfo = 1;
  290. continue;
  291. }
  292. if (KEY("vlegend")) {
  293. read_vlegend();
  294. do_vlegend = 1;
  295. continue;
  296. }
  297. if (KEY("outline")) {
  298. if (PS.cell_fd < 0) {
  299. error(key, data, _("no raster map selected yet"));
  300. gobble_input();
  301. }
  302. else
  303. read_outline();
  304. continue;
  305. }
  306. if (KEY("cell") || KEY("rast") || KEY("raster")) {
  307. if (scan_gis("cell", "raster", key, data, name, mapset, 0))
  308. read_cell(name, mapset);
  309. continue;
  310. }
  311. if (KEY("greyrast") || KEY("grayrast")) {
  312. if (scan_gis("cell", "raster", key, data, name, mapset, 0))
  313. read_cell(name, mapset);
  314. PS.grey = 1;
  315. continue;
  316. }
  317. if (KEY("group")) {
  318. G_strip(data);
  319. if (I_find_group(data)) {
  320. grp.group_name = G_store(data);
  321. grp.do_group = 1;
  322. read_group();
  323. }
  324. else
  325. error(key, data, _("group not found"));
  326. continue;
  327. }
  328. if (KEY("rgb")) {
  329. G_strip(data);
  330. grp.do_group = 1;
  331. read_rgb(key, data);
  332. continue;
  333. }
  334. if (KEY("vpoints")) {
  335. if (scan_gis("vector", "vector", key, data, name, mapset, 1))
  336. read_vpoints(name, mapset);
  337. continue;
  338. }
  339. if (KEY("vlines")) {
  340. if (scan_gis("vector", "vector", key, data, name, mapset, 1))
  341. read_vlines(name, mapset);
  342. continue;
  343. }
  344. if (KEY("vareas")) {
  345. if (scan_gis("vector", "vector", key, data, name, mapset, 1))
  346. read_vareas(name, mapset);
  347. continue;
  348. }
  349. if (KEY("window") || KEY("region")) {
  350. if (scan_gis("windows", "region definition", key, data, name,
  351. mapset, 1))
  352. read_wind(name, mapset);
  353. continue;
  354. }
  355. if (KEY("grid")) {
  356. PS.grid = -1;
  357. PS.grid_numbers = 0;
  358. sscanf(data, "%d", &(PS.grid));
  359. if (PS.grid < 0) {
  360. PS.grid = 0;
  361. error(key, data, _("illegal grid spacing"));
  362. gobble_input();
  363. }
  364. else
  365. getgrid();
  366. continue;
  367. }
  368. if (KEY("geogrid")) {
  369. if (G_projection() == PROJECTION_XY) {
  370. error(key, data,
  371. _("geogrid is not available for this projection"));
  372. gobble_input();
  373. }
  374. /* if (G_projection() == PROJECTION_LL)
  375. G_message(_("geogrid referenced to [???] ellipsoid")); */
  376. PS.geogrid = -1.;
  377. PS.geogrid_numbers = 0;
  378. sscanf(data, "%d %s", &(PS.geogrid), PS.geogridunit);
  379. if (PS.geogrid < 0) {
  380. PS.geogrid = 0;
  381. error(key, data, _("illegal geo-grid spacing"));
  382. gobble_input();
  383. }
  384. else
  385. getgeogrid();
  386. continue;
  387. }
  388. if (KEY("psfile")) {
  389. if (PS.num_psfiles >= MAX_PSFILES)
  390. continue;
  391. G_strip(data);
  392. PS.psfiles[PS.num_psfiles] = G_store(data);
  393. PS.num_psfiles++;
  394. continue;
  395. }
  396. if (KEY("maskcolor")) {
  397. int ret, r, g, b;
  398. ret = G_str_to_color(data, &r, &g, &b);
  399. if (ret == 1) {
  400. PS.mask_r = r / 255.0;
  401. PS.mask_g = g / 255.0;
  402. PS.mask_b = b / 255.0;
  403. PS.mask_color = 1;
  404. continue;
  405. }
  406. else if (ret == 2) { /* none */
  407. continue;
  408. }
  409. else {
  410. error(key, data, _("illegal color request"));
  411. }
  412. }
  413. if (*key)
  414. error(key, "", _("illegal request"));
  415. }
  416. }