[GRASS-dev] [bug #4451] (grass) New options for g.region

this bug's URL: http://intevation.de/rt/webrt?serial_num=4451
-------------------------------------------------------------------------

Subject: New options for g.region

Platform: GNU/Linux/x86
grass obtained from: CVS
grass binary for platform: Compiled from Sources
GRASS Version: CVS updated 20060511

The following patch adds options to g.region to set the number of rows and columns of the region. Options are:

rows=
cols=
size=

rows= and cols= are self-explanatory, size= sets both rows and cols to the same value

Index: main.c

RCS file: /home/grass/grassrepository/grass6/general/g.region/cmd/main.c,v
retrieving revision 1.23
diff -u -r1.23 main.c
--- main.c 6 May 2006 23:13:46 -0000 1.23
+++ main.c 11 May 2006 19:41:27 -0000
@@ -34,6 +34,8 @@
   int print_flag, dist_flag;
   int set_flag;
   double x;
+ int ival;
+ int row_flag=0, col_flag=0, depth_flag=0;
   struct Cell_head window, temp_window;
   char *value;
   char *name;
@@ -63,7 +65,7 @@
       {
     struct Option
     *north,*south,*east,*west,*top,*bottom,
- *res, *nsres, *ewres, *res3, *tbres,
+ *res, *nsres, *ewres, *res3, *tbres, *rows, *cols, *size,
     *save, *region, *view,
     *raster, *raster3d, *align, *zoom, *vect;
   } parm;
@@ -324,6 +326,33 @@
   parm.save->gisprompt = "new,windows,region";
   parm.save->guisection = _("Effects");

+ parm.rows = G_define_option();
+ parm.rows->key = "rows";
+ parm.rows->key_desc = "value";
+ parm.rows->required = NO;
+ parm.rows->multiple = NO;
+ parm.rows->type = TYPE_STRING;
+ parm.rows->description = _("Number of rows in the new region");
+ parm.rows->guisection = _("Resolution");
+
+ parm.cols = G_define_option();
+ parm.cols->key = "cols";
+ parm.cols->key_desc = "value";
+ parm.cols->required = NO;
+ parm.cols->multiple = NO;
+ parm.cols->type = TYPE_STRING;
+ parm.cols->description = _("Number of columns in the new region");
+ parm.cols->guisection = _("Resolution");
+
+ parm.size = G_define_option();
+ parm.size->key = "size";
+ parm.size->key_desc = "value";
+ parm.size->required = NO;
+ parm.size->multiple = NO;
+ parm.size->type = TYPE_STRING;
+ parm.size->description = _("The size (number of rows an columns) in the new region");
+ parm.size->guisection = _("Resolution");
+
   if (G_parser(argc,argv))
     exit(1);

@@ -726,6 +755,33 @@
     }
   }

+ /* rows= */
+ if ((value = parm.rows->answer))
+ {
+ if (sscanf (value, "%i", &ival) != 1 )
+ die(parm.rows);
+ window.rows = ival;
+ row_flag = 1;
+ }
+
+ /* cols= */
+ if ((value = parm.cols->answer))
+ {
+ if (sscanf (value, "%i", &ival) != 1 )
+ die(parm.cols);
+ window.cols = ival;
+ col_flag = 1;
+ }
+
+ /* size= */
+ if ((value = parm.size->answer))
+ {
+ if (sscanf (value, "%i", &ival) != 1 )
+ die(parm.size);
+ window.rows = window.cols = ival;
+ row_flag = col_flag = 1;
+ }
+
   /* zoom= */
   if ((name = parm.zoom->answer))
   {
@@ -754,12 +810,12 @@
     if (G_legal_filename (name) < 0)
       G_fatal_error (_("<%s> - illegal region name"), name);
     G_copy (&temp_window, &window, sizeof(window));
- adjust_window (&temp_window);
+ adjust_window (&temp_window,0,0,0);
     if (G__put_window (&temp_window, "windows", name) < 0)
       G_fatal_error (_("can't write region <%s>"), name);
   }

- adjust_window (&window);
+ adjust_window (&window,row_flag,col_flag,0);
   if (set_flag)
   {
     if (G_put_window (&window) < 0)
Index: local_proto.h

RCS file: /home/grass/grassrepository/grass6/general/g.region/cmd/local_proto.h,v
retrieving revision 2.3
diff -u -r2.3 local_proto.h
--- local_proto.h 19 Dec 2005 16:14:24 -0000 2.3
+++ local_proto.h 11 May 2006 19:41:27 -0000
@@ -1,7 +1,7 @@
#ifndef GREGION_LOCAL_PROTO_H
#define GREGION_LOCAL_PROTO_H
/* adjust.c */
-int adjust_window(struct Cell_head *);
+int adjust_window(struct Cell_head *,int row_flag,int col_flag, int depth_flag);
/* zoom.c */
int zoom(struct Cell_head *, char *, char *);
/* printwindow.c */
Index: adjust.c

RCS file: /home/grass/grassrepository/grass6/general/g.region/cmd/adjust.c,v
retrieving revision 2.2
diff -u -r2.2 adjust.c
--- adjust.c 9 Feb 2006 03:08:52 -0000 2.2
+++ adjust.c 11 May 2006 19:41:27 -0000
@@ -1,11 +1,11 @@
#include <grass/gis.h>

int
-adjust_window (struct Cell_head *window)
+adjust_window (struct Cell_head *window,int row_flag,int col_flag, int depth_flag)
{
     char *err;

- if (err = G_adjust_Cell_head3(window,0,0,0))
+ if (err = G_adjust_Cell_head3(window, row_flag, col_flag, depth_flag))
     {
   char msg[100];
   sprintf (msg, "Invalid region: %s", err);
Index: zoom.c

RCS file: /home/grass/grassrepository/grass6/general/g.region/cmd/zoom.c,v
retrieving revision 2.2
diff -u -r2.2 zoom.c
--- zoom.c 9 Feb 2006 03:08:52 -0000 2.2
+++ zoom.c 11 May 2006 19:41:28 -0000
@@ -12,7 +12,7 @@
     int top,bottom,left,right,mark;
     double north, south, east, west;

- adjust_window (window);
+ adjust_window (window,0,0,0);
     G_set_window (window);
     nrows = window->rows;
     ncols = window->cols;

-------------------------------------------- Managed by Request Tracker

this bug's URL: http://intevation.de/rt/webrt?serial_num=4451
---------------------------------------------------------------------

Subject: New options for g.region

..

The following patch adds options to g.region to set the number of rows
and columns of the region. Options are:

rows=
cols=
size=

rows= and cols= are self-explanatory, size= sets both rows and cols to
the same value

..

+++ main.c 11 May 2006 19:41:27 -0000

..

+ int ival;
+ int row_flag=0, col_flag=0, depth_flag=0;

..

+ parm.rows->key_desc = "value";

..

+ parm.rows->type = TYPE_STRING;

why not use ->type = TYPE_INTEGER; and then skip key_desc?

Then the parser will ensure an integer was given and give a helpful
error message if not. Also you need to check that the value is >=1.

Will it adjust the region bounds or the resolution?

Hamish