main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /***************************************************************************
  2. *
  3. * MODULE: g.region (commandline)
  4. * AUTHOR(S): Michael Shapiro, CERL
  5. * datum added by Andreas Lange <andreas.lange@rhein-main.de>
  6. * PURPOSE: Program to manage and print the boundary definitions for the
  7. * geographic region.
  8. *
  9. * COPYRIGHT: (C) 2000-2021 by the GRASS Development Team
  10. *
  11. * This program is free software under the GPL (>=v2)
  12. * Read the file COPYING that comes with GRASS for details.
  13. ****************************************************************************/
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include <grass/gis.h>
  18. #include <grass/raster.h>
  19. #include <grass/raster3d.h>
  20. #include <grass/vector.h>
  21. #include <grass/glocale.h>
  22. #include "local_proto.h"
  23. static int nsew(const char *, const char *, const char *, const char *);
  24. static void die(struct Option *);
  25. int main(int argc, char *argv[])
  26. {
  27. int i;
  28. int print_flag = 0;
  29. int flat_flag;
  30. double x, xs, ys, zs;
  31. int ival;
  32. int row_flag = 0, col_flag = 0;
  33. struct Cell_head window, temp_window;
  34. const char *value;
  35. const char *name;
  36. const char *mapset;
  37. char **rast_ptr, **vect_ptr;
  38. int pix;
  39. bool update_file = false;
  40. struct GModule *module;
  41. struct
  42. {
  43. struct Flag
  44. *noupdate, *force, *print, *gprint, *flprint, *lprint, *eprint, *nangle,
  45. *center, *res_set, *dist_res, *dflt, *z, *savedefault,
  46. *bbox, *gmt_style, *wms_style;
  47. } flag;
  48. struct
  49. {
  50. struct Option
  51. *north, *south, *east, *west, *top, *bottom,
  52. *res, *nsres, *ewres, *res3, *tbres, *rows, *cols,
  53. *save, *region, *raster, *raster3d, *align,
  54. *zoom, *vect, *grow;
  55. } parm;
  56. G_gisinit(argv[0]);
  57. module = G_define_module();
  58. G_add_keyword(_("general"));
  59. G_add_keyword(_("settings"));
  60. G_add_keyword(_("computational region"));
  61. G_add_keyword(_("extent"));
  62. G_add_keyword(_("resolution"));
  63. G_add_keyword(_("level1"));
  64. module->description =
  65. _("Manages the boundary definitions for the " "geographic region.");
  66. /* flags */
  67. flag.dflt = G_define_flag();
  68. flag.dflt->key = 'd';
  69. flag.dflt->description = _("Set from default region");
  70. flag.dflt->guisection = _("Existing");
  71. flag.savedefault = G_define_flag();
  72. flag.savedefault->key = 's';
  73. flag.savedefault->label = _("Save as default region");
  74. flag.savedefault->description = _("Only possible from the PERMANENT mapset");
  75. flag.savedefault->guisection = _("Existing");
  76. flag.print = G_define_flag();
  77. flag.print->key = 'p';
  78. flag.print->description = _("Print the current region");
  79. flag.print->guisection = _("Print");
  80. flag.lprint = G_define_flag();
  81. flag.lprint->key = 'l';
  82. flag.lprint->description = _("Print the current region in lat/long "
  83. "using the current ellipsoid/datum");
  84. flag.lprint->guisection = _("Print");
  85. flag.eprint = G_define_flag();
  86. flag.eprint->key = 'e';
  87. flag.eprint->description = _("Print the current region extent");
  88. flag.eprint->guisection = _("Print");
  89. flag.center = G_define_flag();
  90. flag.center->key = 'c';
  91. flag.center->description =
  92. _("Print the current region map center coordinates");
  93. flag.center->guisection = _("Print");
  94. flag.gmt_style = G_define_flag();
  95. flag.gmt_style->key = 't';
  96. flag.gmt_style->description =
  97. _("Print the current region in GMT style");
  98. flag.gmt_style->guisection = _("Print");
  99. flag.wms_style = G_define_flag();
  100. flag.wms_style->key = 'w';
  101. flag.wms_style->description =
  102. _("Print the current region in WMS style");
  103. flag.wms_style->guisection = _("Print");
  104. flag.dist_res = G_define_flag();
  105. flag.dist_res->key = 'm';
  106. flag.dist_res->description =
  107. _("Print region resolution in meters (geodesic)");
  108. flag.dist_res->guisection = _("Print");
  109. flag.nangle = G_define_flag();
  110. flag.nangle->key = 'n';
  111. flag.nangle->label = _("Print the convergence angle (degrees CCW)");
  112. flag.nangle->description =
  113. _("The difference between the projection's grid north and true north, "
  114. "measured at the center coordinates of the current region.");
  115. flag.nangle->guisection = _("Print");
  116. flag.z = G_define_flag();
  117. flag.z->key = '3';
  118. flag.z->description = _("Print also 3D settings");
  119. flag.z->guisection = _("Print");
  120. flag.bbox = G_define_flag();
  121. flag.bbox->key = 'b';
  122. flag.bbox->description =
  123. _("Print the maximum bounding box in lat/long on WGS84");
  124. flag.bbox->guisection = _("Print");
  125. flag.gprint = G_define_flag();
  126. flag.gprint->key = 'g';
  127. flag.gprint->description = _("Print in shell script style");
  128. flag.gprint->guisection = _("Print");
  129. flag.flprint = G_define_flag();
  130. flag.flprint->key = 'f';
  131. flag.flprint->description = _("Print in shell script style, but in one line (flat)");
  132. flag.flprint->guisection = _("Print");
  133. flag.res_set = G_define_flag();
  134. flag.res_set->key = 'a';
  135. flag.res_set->description =
  136. _("Align region to resolution (default = align to bounds, "
  137. "works only for 2D resolution)");
  138. flag.res_set->guisection = _("Bounds");
  139. flag.noupdate = G_define_flag();
  140. flag.noupdate->key = 'u';
  141. flag.noupdate->description = _("Do not update the current region");
  142. flag.noupdate->guisection = _("Effects");
  143. flag.force = G_define_flag();
  144. flag.force->key = 'o';
  145. flag.force->description = _("Force update of the current region");
  146. flag.force->guisection = _("Effects");
  147. /* parameters */
  148. parm.region = G_define_standard_option(G_OPT_M_REGION);
  149. parm.region->description = _("Set current region from named region");
  150. parm.region->guisection = _("Existing");
  151. parm.raster = G_define_standard_option(G_OPT_R_MAP);
  152. parm.raster->key = "raster";
  153. parm.raster->required = NO;
  154. parm.raster->multiple = YES;
  155. parm.raster->description = _("Set region to match raster map(s)");
  156. parm.raster->guisection = _("Existing");
  157. parm.raster3d = G_define_standard_option(G_OPT_R3_MAP);
  158. parm.raster3d->key = "raster_3d";
  159. parm.raster3d->required = NO;
  160. parm.raster3d->multiple = NO;
  161. parm.raster3d->description =
  162. _("Set region to match 3D raster map(s) (both 2D and 3D "
  163. "values)");
  164. parm.raster3d->guisection = _("Existing");
  165. parm.vect = G_define_standard_option(G_OPT_V_MAP);
  166. parm.vect->key = "vector";
  167. parm.vect->required = NO;
  168. parm.vect->multiple = YES;
  169. parm.vect->label = _("Set region to match vector map(s)");
  170. parm.vect->description = NULL;
  171. parm.vect->guisection = _("Existing");
  172. parm.north = G_define_option();
  173. parm.north->key = "n";
  174. parm.north->key_desc = "value";
  175. parm.north->required = NO;
  176. parm.north->multiple = NO;
  177. parm.north->type = TYPE_STRING;
  178. parm.north->description = _("Value for the northern edge");
  179. parm.north->guisection = _("Bounds");
  180. parm.south = G_define_option();
  181. parm.south->key = "s";
  182. parm.south->key_desc = "value";
  183. parm.south->required = NO;
  184. parm.south->multiple = NO;
  185. parm.south->type = TYPE_STRING;
  186. parm.south->description = _("Value for the southern edge");
  187. parm.south->guisection = _("Bounds");
  188. parm.east = G_define_option();
  189. parm.east->key = "e";
  190. parm.east->key_desc = "value";
  191. parm.east->required = NO;
  192. parm.east->multiple = NO;
  193. parm.east->type = TYPE_STRING;
  194. parm.east->description = _("Value for the eastern edge");
  195. parm.east->guisection = _("Bounds");
  196. parm.west = G_define_option();
  197. parm.west->key = "w";
  198. parm.west->key_desc = "value";
  199. parm.west->required = NO;
  200. parm.west->multiple = NO;
  201. parm.west->type = TYPE_STRING;
  202. parm.west->description = _("Value for the western edge");
  203. parm.west->guisection = _("Bounds");
  204. parm.top = G_define_option();
  205. parm.top->key = "t";
  206. parm.top->key_desc = "value";
  207. parm.top->required = NO;
  208. parm.top->multiple = NO;
  209. parm.top->type = TYPE_STRING;
  210. parm.top->description = _("Value for the top edge");
  211. parm.top->guisection = _("Bounds");
  212. parm.bottom = G_define_option();
  213. parm.bottom->key = "b";
  214. parm.bottom->key_desc = "value";
  215. parm.bottom->required = NO;
  216. parm.bottom->multiple = NO;
  217. parm.bottom->type = TYPE_STRING;
  218. parm.bottom->description = _("Value for the bottom edge");
  219. parm.bottom->guisection = _("Bounds");
  220. parm.rows = G_define_option();
  221. parm.rows->key = "rows";
  222. parm.rows->key_desc = "value";
  223. parm.rows->required = NO;
  224. parm.rows->multiple = NO;
  225. parm.rows->type = TYPE_INTEGER;
  226. parm.rows->description = _("Number of rows in the new region");
  227. parm.rows->guisection = _("Resolution");
  228. parm.cols = G_define_option();
  229. parm.cols->key = "cols";
  230. parm.cols->key_desc = "value";
  231. parm.cols->required = NO;
  232. parm.cols->multiple = NO;
  233. parm.cols->type = TYPE_INTEGER;
  234. parm.cols->description = _("Number of columns in the new region");
  235. parm.cols->guisection = _("Resolution");
  236. parm.res = G_define_option();
  237. parm.res->key = "res";
  238. parm.res->key_desc = "value";
  239. parm.res->required = NO;
  240. parm.res->multiple = NO;
  241. parm.res->type = TYPE_STRING;
  242. parm.res->description =
  243. _("2D grid resolution (north-south and east-west)");
  244. parm.res->guisection = _("Resolution");
  245. parm.res3 = G_define_option();
  246. parm.res3->key = "res3";
  247. parm.res3->key_desc = "value";
  248. parm.res3->required = NO;
  249. parm.res3->multiple = NO;
  250. parm.res3->type = TYPE_STRING;
  251. parm.res3->description =
  252. _("3D grid resolution (north-south, east-west and top-bottom)");
  253. parm.res3->guisection = _("Resolution");
  254. parm.nsres = G_define_option();
  255. parm.nsres->key = "nsres";
  256. parm.nsres->key_desc = "value";
  257. parm.nsres->required = NO;
  258. parm.nsres->multiple = NO;
  259. parm.nsres->type = TYPE_STRING;
  260. parm.nsres->description = _("North-south 2D grid resolution");
  261. parm.nsres->guisection = _("Resolution");
  262. parm.ewres = G_define_option();
  263. parm.ewres->key = "ewres";
  264. parm.ewres->key_desc = "value";
  265. parm.ewres->required = NO;
  266. parm.ewres->multiple = NO;
  267. parm.ewres->type = TYPE_STRING;
  268. parm.ewres->description = _("East-west 2D grid resolution");
  269. parm.ewres->guisection = _("Resolution");
  270. parm.tbres = G_define_option();
  271. parm.tbres->key = "tbres";
  272. parm.tbres->key_desc = "value";
  273. parm.tbres->required = NO;
  274. parm.tbres->multiple = NO;
  275. parm.tbres->type = TYPE_STRING;
  276. parm.tbres->description = _("Top-bottom 3D grid resolution");
  277. parm.tbres->guisection = _("Resolution");
  278. parm.zoom = G_define_option();
  279. parm.zoom->key = "zoom";
  280. parm.zoom->key_desc = "name";
  281. parm.zoom->required = NO;
  282. parm.zoom->multiple = NO;
  283. parm.zoom->type = TYPE_STRING;
  284. parm.zoom->description =
  285. _("Shrink region until it meets non-NULL data from this raster map");
  286. parm.zoom->gisprompt = "old,cell,raster";
  287. parm.zoom->guisection = _("Bounds");
  288. parm.align = G_define_option();
  289. parm.align->key = "align";
  290. parm.align->key_desc = "name";
  291. parm.align->required = NO;
  292. parm.align->multiple = NO;
  293. parm.align->type = TYPE_STRING;
  294. parm.align->description =
  295. _("Adjust region cells to cleanly align with this raster map");
  296. parm.align->gisprompt = "old,cell,raster";
  297. parm.align->guisection = _("Bounds");
  298. parm.grow = G_define_option();
  299. parm.grow->key = "grow";
  300. parm.grow->key_desc = "value";
  301. parm.grow->required = NO;
  302. parm.grow->multiple = NO;
  303. parm.grow->type = TYPE_INTEGER;
  304. parm.grow->label =
  305. _("Number of cells to add to each side of the current region extent");
  306. parm.grow->description =
  307. _("A negative number shrinks the current region extent");
  308. parm.grow->guisection = _("Bounds");
  309. parm.save = G_define_option();
  310. parm.save->key = "save";
  311. parm.save->key_desc = "name";
  312. parm.save->required = NO;
  313. parm.save->multiple = NO;
  314. parm.save->type = TYPE_STRING;
  315. parm.save->description =
  316. _("Save current region settings in named region file");
  317. parm.save->gisprompt = "new,windows,region";
  318. parm.save->guisection = _("Effects");
  319. G_option_required(flag.dflt, flag.savedefault, flag.print, flag.lprint,
  320. flag.eprint, flag.center, flag.gmt_style, flag.wms_style,
  321. flag.dist_res, flag.nangle, flag. z, flag.bbox, flag.gprint,
  322. flag.res_set, flag.noupdate, parm.region, parm.raster,
  323. parm.raster3d, parm.vect, parm.north, parm.south, parm.east,
  324. parm.west, parm.top, parm.bottom, parm.rows, parm.cols,
  325. parm.res, parm.res3, parm.nsres, parm.ewres, parm.tbres,
  326. parm.zoom, parm.align, parm.save, parm.grow, NULL);
  327. G_option_exclusive(flag.noupdate, flag.force, NULL);
  328. G_option_requires(flag.noupdate, flag.savedefault, flag.print, flag.lprint,
  329. flag.eprint, flag.center, flag.gmt_style, flag.wms_style,
  330. flag.dist_res, flag.nangle, flag.z, flag.bbox, flag.gprint, parm.save, NULL);
  331. if (G_parser(argc, argv))
  332. exit(EXIT_FAILURE);
  333. G_get_default_window(&window);
  334. flat_flag = flag.flprint->answer;
  335. if (flag.print->answer)
  336. print_flag |= PRINT_REG;
  337. if (flag.gprint->answer)
  338. print_flag |= PRINT_SH;
  339. if (flag.lprint->answer)
  340. print_flag |= PRINT_LL;
  341. if (flag.eprint->answer)
  342. print_flag |= PRINT_EXTENT;
  343. if (flag.center->answer)
  344. print_flag |= PRINT_CENTER;
  345. if (flag.gmt_style->answer)
  346. print_flag |= PRINT_GMT;
  347. if (flag.wms_style->answer)
  348. print_flag |= PRINT_WMS;
  349. if (flag.nangle->answer)
  350. print_flag |= PRINT_NANGLE;
  351. if (flag.dist_res->answer)
  352. print_flag |= PRINT_METERS;
  353. if (flag.z->answer)
  354. print_flag |= PRINT_3D;
  355. if (flag.bbox->answer)
  356. print_flag |= PRINT_MBBOX;
  357. if (print_flag == PRINT_METERS)
  358. print_flag |= PRINT_SH;
  359. if (print_flag == PRINT_SH ||
  360. print_flag & PRINT_3D || print_flag == PRINT_METERS + PRINT_SH) {
  361. print_flag |= PRINT_REG;
  362. }
  363. if (!flag.dflt->answer)
  364. G_get_window(&window);
  365. /* region= */
  366. if ((name = parm.region->answer)) {
  367. update_file = true;
  368. mapset = G_find_file2("windows", name, "");
  369. if (!mapset)
  370. G_fatal_error(_("Region <%s> not found"), name);
  371. G_get_element_window(&window, "windows", name, mapset);
  372. }
  373. /* raster= */
  374. if (parm.raster->answer) {
  375. int first = 0;
  376. update_file = true;
  377. rast_ptr = parm.raster->answers;
  378. for (; *rast_ptr != NULL; rast_ptr++) {
  379. char rast_name[GNAME_MAX];
  380. strcpy(rast_name, *rast_ptr);
  381. mapset = G_find_raster2(rast_name, "");
  382. if (!mapset)
  383. G_fatal_error(_("Raster map <%s> not found"), rast_name);
  384. Rast_get_cellhd(rast_name, mapset, &temp_window);
  385. if (!first) {
  386. window = temp_window;
  387. first = 1;
  388. }
  389. else {
  390. window.north = (window.north > temp_window.north) ?
  391. window.north : temp_window.north;
  392. window.south = (window.south < temp_window.south) ?
  393. window.south : temp_window.south;
  394. window.east = (window.east > temp_window.east) ?
  395. window.east : temp_window.east;
  396. window.west = (window.west < temp_window.west) ?
  397. window.west : temp_window.west;
  398. }
  399. }
  400. G_adjust_Cell_head3(&window, 0, 0, 0);
  401. }
  402. /* raster3d= */
  403. if ((name = parm.raster3d->answer)) {
  404. RASTER3D_Region win;
  405. update_file = true;
  406. if ((mapset = G_find_raster3d(name, "")) == NULL)
  407. G_fatal_error(_("3D raster map <%s> not found"), name);
  408. if (Rast3d_read_region_map(name, mapset, &win) < 0)
  409. G_fatal_error(_("Unable to read header of 3D raster map <%s@%s>"),
  410. name, mapset);
  411. Rast3d_region_to_cell_head(&win, &window);
  412. }
  413. /* vector= */
  414. if (parm.vect->answer) {
  415. int first = 0;
  416. update_file = true;
  417. vect_ptr = parm.vect->answers;
  418. for (; *vect_ptr != NULL; vect_ptr++) {
  419. int ret;
  420. struct Map_info Map;
  421. struct bound_box box;
  422. char vect_name[GNAME_MAX];
  423. struct Cell_head map_window;
  424. strcpy(vect_name, *vect_ptr);
  425. mapset = G_find_vector2(vect_name, "");
  426. if (!mapset)
  427. G_fatal_error(_("Vector map <%s> not found"), vect_name);
  428. temp_window = window;
  429. /* try to open head-only on level 2 */
  430. if (Vect_open_old_head(&Map, vect_name, mapset) < 2) {
  431. /* force level 1, open fully */
  432. Vect_close(&Map);
  433. Vect_set_open_level(1); /* no topology */
  434. if (Vect_open_old(&Map, vect_name, mapset) < 1)
  435. G_fatal_error(_("Unable to open vector map <%s>"),
  436. vect_name);
  437. }
  438. ret = 0;
  439. if (Vect_level(&Map) > 1) /* level 2 - topology available */
  440. ret = Vect_get_map_box(&Map, &box);
  441. else /* level 1 */
  442. ret = Vect_get_map_box1(&Map, &box);
  443. if (ret != 1)
  444. G_fatal_error(_("Unable to get map bounding box"));
  445. map_window = window;
  446. map_window.north = box.N;
  447. map_window.south = box.S;
  448. map_window.west = box.W;
  449. map_window.east = box.E;
  450. map_window.top = box.T;
  451. map_window.bottom = box.B;
  452. if (!first) {
  453. window = map_window;
  454. first = 1;
  455. }
  456. else {
  457. window.north = (window.north > map_window.north) ?
  458. window.north : map_window.north;
  459. window.south = (window.south < map_window.south) ?
  460. window.south : map_window.south;
  461. window.east = (window.east > map_window.east) ?
  462. window.east : map_window.east;
  463. window.west = (window.west < map_window.west) ?
  464. window.west : map_window.west;
  465. if (map_window.top > window.top)
  466. window.top = map_window.top;
  467. if (map_window.bottom < window.bottom)
  468. window.bottom = map_window.bottom;
  469. }
  470. if (window.north == window.south) {
  471. window.north = window.north + 0.5 * temp_window.ns_res;
  472. window.south = window.south - 0.5 * temp_window.ns_res;
  473. }
  474. if (window.east == window.west) {
  475. window.west = window.west - 0.5 * temp_window.ew_res;
  476. window.east = window.east + 0.5 * temp_window.ew_res;
  477. }
  478. if (window.top == window.bottom) {
  479. window.bottom = (window.bottom - 0.5 * temp_window.tb_res);
  480. window.top = (window.top + 0.5 * temp_window.tb_res);
  481. }
  482. if (flag.res_set->answer)
  483. Rast_align_window(&window, &temp_window);
  484. Vect_close(&Map);
  485. }
  486. }
  487. /* n= */
  488. if ((value = parm.north->answer)) {
  489. update_file = true;
  490. if ((i = nsew(value, "n+", "n-", "s+"))) {
  491. if (!G_scan_resolution(value + 2, &x, window.proj))
  492. die(parm.north);
  493. switch (i) {
  494. case 1:
  495. window.north += x;
  496. break;
  497. case 2:
  498. window.north -= x;
  499. break;
  500. case 3:
  501. window.north = window.south + x;
  502. break;
  503. }
  504. }
  505. else if (G_scan_northing(value, &x, window.proj))
  506. window.north = x;
  507. else
  508. die(parm.north);
  509. if (flag.res_set->answer)
  510. window.north = ceil(window.north / window.ns_res) * window.ns_res;
  511. }
  512. /* s= */
  513. if ((value = parm.south->answer)) {
  514. update_file = true;
  515. if ((i = nsew(value, "s+", "s-", "n-"))) {
  516. if (!G_scan_resolution(value + 2, &x, window.proj))
  517. die(parm.south);
  518. switch (i) {
  519. case 1:
  520. window.south += x;
  521. break;
  522. case 2:
  523. window.south -= x;
  524. break;
  525. case 3:
  526. window.south = window.north - x;
  527. break;
  528. }
  529. }
  530. else if (G_scan_northing(value, &x, window.proj))
  531. window.south = x;
  532. else
  533. die(parm.south);
  534. if (flag.res_set->answer)
  535. window.south = floor(window.south / window.ns_res) * window.ns_res;
  536. }
  537. /* e= */
  538. if ((value = parm.east->answer)) {
  539. update_file = true;
  540. if ((i = nsew(value, "e+", "e-", "w+"))) {
  541. if (!G_scan_resolution(value + 2, &x, window.proj))
  542. die(parm.east);
  543. switch (i) {
  544. case 1:
  545. window.east += x;
  546. break;
  547. case 2:
  548. window.east -= x;
  549. break;
  550. case 3:
  551. window.east = window.west + x;
  552. break;
  553. }
  554. }
  555. else if (G_scan_easting(value, &x, window.proj))
  556. window.east = x;
  557. else
  558. die(parm.east);
  559. if (flag.res_set->answer)
  560. window.east = ceil(window.east / window.ew_res) * window.ew_res;
  561. }
  562. /* w= */
  563. if ((value = parm.west->answer)) {
  564. update_file = true;
  565. if ((i = nsew(value, "w+", "w-", "e-"))) {
  566. if (!G_scan_resolution(value + 2, &x, window.proj))
  567. die(parm.west);
  568. switch (i) {
  569. case 1:
  570. window.west += x;
  571. break;
  572. case 2:
  573. window.west -= x;
  574. break;
  575. case 3:
  576. window.west = window.east - x;
  577. break;
  578. }
  579. }
  580. else if (G_scan_easting(value, &x, window.proj))
  581. window.west = x;
  582. else
  583. die(parm.west);
  584. if (flag.res_set->answer)
  585. window.west = floor(window.west / window.ew_res) * window.ew_res;
  586. }
  587. /* t= */
  588. if ((value = parm.top->answer)) {
  589. update_file = true;
  590. if ((i = nsew(value, "t+", "t-", "b+"))) {
  591. if (sscanf(value + 2, "%lf", &x) != 1)
  592. die(parm.top);
  593. switch (i) {
  594. case 1:
  595. window.top += x;
  596. break;
  597. case 2:
  598. window.top -= x;
  599. break;
  600. case 3:
  601. window.top = window.bottom + x;
  602. break;
  603. }
  604. }
  605. else if (sscanf(value, "%lf", &x) == 1)
  606. window.top = x;
  607. else
  608. die(parm.top);
  609. }
  610. /* b= */
  611. if ((value = parm.bottom->answer)) {
  612. update_file = true;
  613. if ((i = nsew(value, "b+", "b-", "t-"))) {
  614. if (sscanf(value + 2, "%lf", &x) != 1)
  615. die(parm.bottom);
  616. switch (i) {
  617. case 1:
  618. window.bottom += x;
  619. break;
  620. case 2:
  621. window.bottom -= x;
  622. break;
  623. case 3:
  624. window.bottom = window.top - x;
  625. break;
  626. }
  627. }
  628. else if (sscanf(value, "%lf", &x) == 1)
  629. window.bottom = x;
  630. else
  631. die(parm.bottom);
  632. }
  633. /* res= */
  634. if ((value = parm.res->answer)) {
  635. update_file = true;
  636. if (!G_scan_resolution(value, &x, window.proj))
  637. die(parm.res);
  638. window.ns_res = x;
  639. window.ew_res = x;
  640. if (flag.res_set->answer) {
  641. window.north = ceil(window.north / x) * x;
  642. window.south = floor(window.south / x) * x;
  643. window.east = ceil(window.east / x) * x;
  644. window.west = floor(window.west / x) * x;
  645. }
  646. }
  647. /* res3= */
  648. if ((value = parm.res3->answer)) {
  649. update_file = true;
  650. if (!G_scan_resolution(value, &x, window.proj))
  651. die(parm.res);
  652. window.ns_res3 = x;
  653. window.ew_res3 = x;
  654. window.tb_res = x;
  655. }
  656. /* nsres= */
  657. if ((value = parm.nsres->answer)) {
  658. update_file = true;
  659. if (!G_scan_resolution(value, &x, window.proj))
  660. die(parm.nsres);
  661. window.ns_res = x;
  662. if (flag.res_set->answer) {
  663. window.north = ceil(window.north / x) * x;
  664. window.south = floor(window.south / x) * x;
  665. }
  666. }
  667. /* ewres= */
  668. if ((value = parm.ewres->answer)) {
  669. update_file = true;
  670. if (!G_scan_resolution(value, &x, window.proj))
  671. die(parm.ewres);
  672. window.ew_res = x;
  673. if (flag.res_set->answer) {
  674. window.east = ceil(window.east / x) * x;
  675. window.west = floor(window.west / x) * x;
  676. }
  677. }
  678. /* tbres= */
  679. if ((value = parm.tbres->answer)) {
  680. update_file = true;
  681. if (sscanf(value, "%lf", &x) != 1)
  682. die(parm.tbres);
  683. window.tb_res = x;
  684. if (flag.res_set->answer) {
  685. window.top = ceil(window.top / x) * x;
  686. window.bottom = floor(window.bottom / x) * x;
  687. }
  688. }
  689. /* rows= */
  690. if ((value = parm.rows->answer)) {
  691. update_file = true;
  692. if (sscanf(value, "%i", &ival) != 1)
  693. die(parm.rows);
  694. window.rows = ival;
  695. row_flag = 1;
  696. }
  697. /* cols= */
  698. if ((value = parm.cols->answer)) {
  699. update_file = true;
  700. if (sscanf(value, "%i", &ival) != 1)
  701. die(parm.cols);
  702. window.cols = ival;
  703. col_flag = 1;
  704. }
  705. /* zoom= */
  706. if ((name = parm.zoom->answer)) {
  707. update_file = true;
  708. mapset = G_find_raster2(name, "");
  709. if (!mapset)
  710. G_fatal_error(_("Raster map <%s> not found"), name);
  711. zoom(&window, name, mapset);
  712. }
  713. /* align= */
  714. if ((name = parm.align->answer)) {
  715. update_file = true;
  716. mapset = G_find_raster2(name, "");
  717. if (!mapset)
  718. G_fatal_error(_("Raster map <%s> not found"), name);
  719. Rast_get_cellhd(name, mapset, &temp_window);
  720. Rast_align_window(&window, &temp_window);
  721. }
  722. /* grow by number of cells */
  723. if ((value = parm.grow->answer)){
  724. update_file = true;
  725. if (sscanf(value, "%i", &pix)){
  726. xs = window.ns_res * pix;
  727. if (window.north + xs > window.south - xs){
  728. if (G_projection() == PROJECTION_LL && (
  729. window.north + xs <= 90.0 + 0.5 * window.ns_res ||
  730. window.south - xs >= -90.0 - 0.5 * window.ns_res)) {
  731. G_warning(_("'%s' option not used with <%s> because a coordinate would become invalid"),
  732. parm.grow->key, "latitude");
  733. } else {
  734. window.north += xs;
  735. window.south -= xs;
  736. }
  737. } else {
  738. G_warning(_("'%s' option not used with <%s> because <%s> would become minor than <%s>"),
  739. parm.grow->key, "latitude", "north", "south");
  740. }
  741. ys = window.ew_res * pix;
  742. if (window.east + ys > window.west - ys){
  743. window.west -= ys;
  744. window.east += ys;
  745. } else {
  746. G_warning(_("'%s' option not used with <%s> because <%s> would become minor than <%s>"),
  747. parm.grow->key, "longitude", "east", "west");
  748. }
  749. zs = window.tb_res * pix;
  750. if (window.top + zs > window.bottom - zs){
  751. window.top += zs;
  752. window.bottom -= zs;
  753. } else {
  754. G_warning(_("'%s' option not used with <%s> because <%s> would become minor than <%s>"),
  755. parm.grow->key, "3D", "top", "bottom");
  756. }
  757. }
  758. }
  759. /* save= */
  760. if ((name = parm.save->answer)) {
  761. update_file = false;
  762. temp_window = window;
  763. G_adjust_Cell_head3(&temp_window, 0, 0, 0);
  764. if (G_put_element_window(&temp_window, "windows", name) < 0)
  765. G_fatal_error(_("Unable to set region <%s>"), name);
  766. }
  767. G_adjust_Cell_head3(&window, row_flag, col_flag, 0);
  768. if (flag.force->answer || (update_file && !flag.noupdate->answer)) {
  769. if (G_put_window(&window) < 0)
  770. G_fatal_error(_("Unable to update current region"));
  771. }
  772. if (flag.savedefault->answer) {
  773. if (strcmp(G_mapset(), "PERMANENT") == 0) {
  774. G_put_element_window(&window, "", "DEFAULT_WIND");
  775. }
  776. else {
  777. G_fatal_error(_("Unable to change default region. "
  778. "The current mapset is not <PERMANENT>."));
  779. }
  780. } /* / flag.savedefault->answer */
  781. if (print_flag)
  782. print_window(&window, print_flag, flat_flag);
  783. exit(EXIT_SUCCESS);
  784. }
  785. static void die(struct Option *parm)
  786. {
  787. /*
  788. G_usage();
  789. */
  790. G_fatal_error(_("Invalid input <%s=%s>"), parm->key, parm->answer);
  791. }
  792. static int nsew(const char *value, const char *a, const char *b, const char *c)
  793. {
  794. if (strncmp(value, a, strlen(a)) == 0)
  795. return 1;
  796. if (strncmp(value, b, strlen(b)) == 0)
  797. return 2;
  798. if (strncmp(value, c, strlen(c)) == 0)
  799. return 3;
  800. return 0;
  801. }