[GRASS5] r.profile again

Jachym Cepicky jachym.cepicky at centrum.cz
Thu Apr 28 11:07:50 EDT 2005


Hallo developers,
I have rewritten the patch for r.profile, so I don't need to shame for it any
more.

I don't have the access to cvs tree, so if you find it usefull, 
you will have to commit it by yourself.

Have a nice day

Jachym
-- 
Jachym Cepicky
e-mail: jachym.cepicky at centrum.cz
URL: http://les-ejk.cz
GPG: http://www.fle.czu.cz/~jachym/gnupg_public_key/
-------------- next part --------------
Index: main.c
===================================================================
RCS file: /home/grass/grassrepository/grass51/raster/r.profile/main.c,v
retrieving revision 2.1
diff -u -r2.1 main.c
--- main.c	15 Jan 2005 11:18:13 -0000	2.1
+++ main.c	28 Apr 2005 14:53:00 -0000
@@ -17,11 +17,15 @@
 #include "raster.h"
 #include "glocale.h"
 
+#ifndef MAXFILE         /* maximum number of files */
+#define MAXFILE 10
+#endif
+
 static int move(int, int);
 static int cont(int, int);
-int do_profile(double, double, double, double, char *, int, double, int, int, FILE *, char *);
+int do_profile(double, double, double, double, char *[], int, double, int [], int [], FILE *, char *, int);
 /* read_rast.c */
-int read_rast(double, double, double, int, int, RASTER_MAP_TYPE, FILE *, char *);
+int read_rast(double, double, double, int [], int, RASTER_MAP_TYPE [], FILE *, char *, int);
 
 static int min_range[5], max_range[5];
 static int which_range;
@@ -30,15 +34,15 @@
 
 int main(int argc, char *argv[])
 {
-    char *name, *outfile, *mapset, msg[256];
-    int fd, projection;
+    char *name[MAXFILE], *outfile, *mapset[MAXFILE], msg[256];
+    int fd[MAXFILE], projection;
     FILE *fp;
     int screen_x, screen_y, button;
     double res;
     char errbuf[256], *null_string;
     int coords = 0, i, k = -1;
     double e1, e2, n1, n2;
-    RASTER_MAP_TYPE data_type;
+    RASTER_MAP_TYPE data_type[MAXFILE];
     struct Cell_head window;
     struct
     {
@@ -47,6 +51,8 @@
     }
     parm;
     struct GModule *module;
+    int files;          /* number of files */
+    char **inputs;      /* input rasters */
 
     G_gisinit(argv[0]);
 
@@ -59,7 +65,7 @@
     parm.opt1->key = "input";
     parm.opt1->type = TYPE_STRING;
     parm.opt1->required = YES;
-    parm.opt1->multiple = NO;
+    parm.opt1->multiple = YES;
     parm.opt1->gisprompt = "old,cell,raster";
     parm.opt1->description = _("Name of existing raster map");
 
@@ -136,18 +142,28 @@
 
     /* Open Input File for reading */
     /* Get Input Name */
-    name = parm.opt1->answer;
+    inputs = parm.opt1->answers;
+    for (files = 0; *inputs != NULL; files++, inputs++) {
+        if (files >= MAXFILE)
+            G_fatal_error("%s - too many files. only %d allowed",
+                    G_program_name(), MAXFILE);
+
+        name[files] = *inputs;
+    }
+    
     if (parm.g->answer)
 	coords = 1;
 
     /* Open Raster File */
-    if (NULL == (mapset = G_find_cell2(name, ""))) {
-	sprintf(msg, "Cannot find mapset for %s \n", name);
-	G_fatal_error(msg);
-    }
-    if (0 > (fd = G_open_cell_old(name, mapset))) {
-	sprintf(msg, "Cannot open File %s\n", name);
-	G_fatal_error(msg);
+    for (i = 0; i < files; i++) {
+        if (NULL == (mapset[i] = G_find_cell2(name[i], ""))) {
+            sprintf(msg, "Cannot find mapset for %s \n", name[i]);
+            G_fatal_error(msg);
+        }
+        if (0 > (fd[i] = G_open_cell_old(name[i], mapset[i]))) {
+	    sprintf(msg, "Cannot open File %s\n", name[i]);
+    	    G_fatal_error(msg);
+        }
     }
 
     /* Open ASCII file for output or stdout */
@@ -162,7 +178,8 @@
     }
 
     /* Get Raster Type */
-    data_type = G_raster_map_type(name, mapset);
+    for (i = 0; i < files; i++) 
+        data_type[i] = G_raster_map_type(name[i], mapset[i]);
     /* Done with file */
 
     /* Get coords */
@@ -217,7 +234,7 @@
 	    G_plot_line(e1, n1, e2, n2);
 
 	    /* Get profile info */
-	    do_profile(e1, e2, n1, n2, name, coords, res, fd, data_type, fp, null_string);
+	    do_profile(e1, e2, n1, n2, name, coords, res, fd, data_type, fp, null_string, files);
 
 	    n1 = n2;
 	    e1 = e2;
@@ -240,7 +257,7 @@
 	    e2 = e1;
 	    n2 = n1;
 	    /* Get profile info */
-	    do_profile(e1, e2, n1, n2, name, coords, res, fd, data_type, fp, null_string);
+	    do_profile(e1, e2, n1, n2, name, coords, res, fd, data_type, fp, null_string, files);
 	}
 	else {
 	    for (i = 0; i <= k - 2; i += 2) {
@@ -250,13 +267,14 @@
 		sscanf(parm.profile->answers[i + 3], "%lf", &n2);
 		/* Get profile info */
 		do_profile(e1, e2, n1, n2, name, coords, res, fd, data_type,
-			   fp, null_string);
+			   fp, null_string, files);
 
 	    }
 	}
     }
 
-    G_close_cell(fd);
+    for (i = 0; i < files; i++)
+        G_close_cell(fd[i]);
     fclose(fp);
 
     return 0;
@@ -264,8 +282,8 @@
 
 /* Calculate the Profile Now */
 /* Establish parameters */
-int do_profile(double e1, double e2, double n1, double n2, char *name, int coords, 
-		double res, int fd, int data_type, FILE * fp, char *null_string)
+int do_profile(double e1, double e2, double n1, double n2, char *name[], int coords, 
+		double res, int fd[], int data_type[], FILE * fp, char *null_string, int files)
 {
     float rows, cols, LEN;
     double Y, X, AZI;
@@ -281,7 +299,8 @@
 	/* Special case for no movement */
 	e = e1;
 	n = n1;
-	read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
+	read_rast(e, n, dist, fd, coords, data_type, fp, null_string, files);
+        fprintf(fp, "\n");
     }
 
     if (rows >= 0 && cols < 0) {
@@ -297,7 +316,8 @@
 	    dist -= G_distance(e, n, e1, n1);
 	}
 	for (e = e1, n = n1; e < e2 || n > n2; e += X, n -= Y) {
-	    read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
+	    read_rast(e, n, dist, fd, coords, data_type, fp, null_string, files);
+            fprintf(fp, "\n");
 	    /* d+=res; */
 	    dist += G_distance(e - X, n + Y, e, n);
 	}
@@ -319,7 +339,8 @@
 	     */
 	}
 	for (e = e1, n = n1; e < e2 || n < n2; e += X, n += Y) {
-	    read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
+	    read_rast(e, n, dist, fd, coords, data_type, fp, null_string, files);
+            fprintf(fp, "\n");
 	    /* d+=res; */
 	    dist += G_distance(e - X, n - Y, e, n);
 	}
@@ -338,7 +359,8 @@
 	    dist -= G_distance(e, n, e1, n1);
 	}
 	for (e = e1, n = n1; e > e2 || n > n2; e -= X, n -= Y) {
-	    read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
+	    read_rast(e, n, dist, fd, coords, data_type, fp, null_string, files);
+            fprintf(fp, "\n");
 	    /* d+=res; */
 	    dist += G_distance(e + X, n + Y, e, n);
 	}
@@ -357,7 +379,8 @@
 	    dist -= G_distance(e, n, e1, n1);
 	}
 	for (e = e1, n = n1; e > e2 || n < n2; e -= X, n += Y) {
-	    read_rast(e, n, dist, fd, coords, data_type, fp, null_string);
+	    read_rast(e, n, dist, fd, coords, data_type, fp, null_string, files);
+            fprintf(fp, "\n");
 	    /* d+=res; */
 	    dist += G_distance(e + X, n - Y, e, n);
 	}
-------------- next part --------------
Index: read_rast.c
===================================================================
RCS file: /home/grass/grassrepository/grass51/raster/r.profile/read_rast.c,v
retrieving revision 2.0
diff -u -r2.0 read_rast.c
--- read_rast.c	9 Nov 2004 13:51:36 -0000	2.0
+++ read_rast.c	28 Apr 2005 14:53:18 -0000
@@ -17,8 +17,8 @@
 #include "gis.h"
 #include "display.h"
 
-int read_rast(double east, double north, double dist, int fd, int coords, 
-		RASTER_MAP_TYPE data_type, FILE * fp, char *null_string)
+int read_rast(double east, double north, double dist, int fd[], int coords, 
+		RASTER_MAP_TYPE data_type[], FILE * fp, char *null_string, int files)
 {
     int row, col, nrows, ncols;
     struct Cell_head window;
@@ -26,6 +26,8 @@
     char buf[1024] = "";
     FCELL *fcell;
     DCELL *dcell;
+    int i;
+
 
     G_get_window(&window);
     nrows = window.rows;
@@ -37,51 +39,77 @@
     if (row < 0)
 	G_fatal_error("Coordinate request outsite current region settings");
 
-    if (data_type == CELL_TYPE) {
-	cell = G_allocate_c_raster_buf();
-	if (G_get_c_raster_row(fd, cell, row) < 0)
-	    exit(1);
-
-	if (G_is_c_null_value(&cell[col]))
-	    sprintf(buf, null_string);
-	else
-	    sprintf(buf, "%d", cell[col]);
-
-	if (coords == 1)
-	    fprintf(fp, "%f %f %f %s\n", east, north, dist, buf);
-	else
-	    fprintf(fp, "%f %s\n", dist, buf);
-    }
-
-    if (data_type == FCELL_TYPE) {
-	fcell = G_allocate_f_raster_buf();
-	if (G_get_f_raster_row(fd, fcell, row) < 0)
-	    exit(1);
-
-	if (G_is_f_null_value(&fcell[col]))
-	    sprintf(buf, null_string);
-	else
-	    sprintf(buf, "%f", fcell[col]);
-
-	if (coords == 1)
-	    fprintf(fp, "%f %f %f %s\n", east, north, dist, buf);
-	else
-	    fprintf(fp, "%f %s\n", dist, buf);
-    }
-
-    if (data_type == DCELL_TYPE) {
-	dcell = G_allocate_d_raster_buf();
-	if (G_get_d_raster_row(fd, dcell, row) < 0)
-	    exit(1);
-	if (G_is_d_null_value(&dcell[col]))
-	    sprintf(buf, null_string);
-	else
-	    sprintf(buf, "%f", dcell[col]);
-
-	if (coords == 1)
-	    fprintf(fp, "%f %f %f %s\n", east, north, dist, buf);
-	else
-	    fprintf(fp, "%f %s\n", dist, buf);
+    /* for each raster file */
+    for (i = 0; i < files; i++) {
+
+        if (data_type[i] == CELL_TYPE) {
+            cell = G_allocate_c_raster_buf();
+            if (G_get_c_raster_row(fd[i], cell, row) < 0)
+                exit(1);
+
+            if (G_is_c_null_value(&cell[col]))
+                sprintf(buf, null_string);
+            else
+                sprintf(buf, "%d ", cell[col]);
+
+            if (coords == 1) {
+                if (i == 0)     /* print coordninates and 
+                                   distance only for the first raster */
+                    fprintf(fp, "%f %f %f ", east, north, dist);
+                fprintf(fp, "%s ", buf);
+            }
+            else {
+                if (i == 0)     /* print coordinates and 
+                                   distance only for the first raster */
+                    fprintf(fp, "%f ", dist);
+                fprintf(fp, "%s ", buf);
+            }
+        }
+
+        if (data_type[i] == FCELL_TYPE) {
+            fcell = G_allocate_f_raster_buf();
+            if (G_get_f_raster_row(fd[i], fcell, row) < 0)
+                exit(1);
+
+            if (G_is_f_null_value(&fcell[col]))
+                sprintf(buf, null_string);
+            else
+                sprintf(buf, "%f ", fcell[col]);
+
+            if (coords == 1) {
+                if (i == 0)  
+                    fprintf(fp, "%f %f %f ", east, north, dist);
+                fprintf(fp, "%s ", buf);
+            }
+            else {
+                if (i == 0)  
+                    fprintf(fp, "%f ", dist);
+                fprintf(fp, "%s ", buf);
+            }
+        }
+
+        if (data_type[i] == DCELL_TYPE) {
+            dcell = G_allocate_d_raster_buf();
+            
+            if (G_get_d_raster_row(fd[i], dcell, row) < 0)
+                exit(1);
+
+            if (G_is_d_null_value(&dcell[col]))
+                sprintf(buf, null_string);
+            else
+                sprintf(buf, "%f ", dcell[col]);
+
+            if (coords == 1) {
+                if (i == 0)  
+                    fprintf(fp, "%f %f %f ", east, north, dist);
+                fprintf(fp, "%s ", buf);
+            }
+            else {
+                if (i == 0)  
+                    fprintf(fp, "%f ", dist);
+                fprintf(fp, "%s ", buf);
+            }
+        }
     }
 
     return 0;


More information about the grass-dev mailing list