[GRASS-SVN] r60497 - grass/trunk/raster/r.distance

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 26 13:15:26 PDT 2014


Author: hcho
Date: 2014-05-26 13:15:26 -0700 (Mon, 26 May 2014)
New Revision: 60497

Modified:
   grass/trunk/raster/r.distance/defs.h
   grass/trunk/raster/r.distance/edges.c
   grass/trunk/raster/r.distance/main.c
   grass/trunk/raster/r.distance/parse.c
   grass/trunk/raster/r.distance/report.c
Log:
r.distance: Added -n flag to report null as *. Otherwise, no nulls reported. Only CELL maps allowed

Modified: grass/trunk/raster/r.distance/defs.h
===================================================================
--- grass/trunk/raster/r.distance/defs.h	2014-05-26 19:19:21 UTC (rev 60496)
+++ grass/trunk/raster/r.distance/defs.h	2014-05-26 20:15:26 UTC (rev 60497)
@@ -51,6 +51,7 @@
     int labels;			/* boolean: report includes cat labels */
     char *fs;			/* report field separator     */
     int overlap;		/* checking for overlapping, than distance is 0 */
+    int null;			/* report null objects as * */
 };
 
 /* distance.c */
@@ -61,7 +62,7 @@
 
 /* edges.c */
 void print_edge_info(struct Map *);
-void find_edge_cells(struct Map *);
+void find_edge_cells(struct Map *, int);
 void add_edge_cell(struct Map *, CELL, int, int);
 void init_edge_list(struct Map *);
 void sort_edge_list(struct Map *);

Modified: grass/trunk/raster/r.distance/edges.c
===================================================================
--- grass/trunk/raster/r.distance/edges.c	2014-05-26 19:19:21 UTC (rev 60496)
+++ grass/trunk/raster/r.distance/edges.c	2014-05-26 20:15:26 UTC (rev 60497)
@@ -33,7 +33,7 @@
     fprintf(stdout, "\n");
 }
 
-void find_edge_cells(struct Map *map)
+void find_edge_cells(struct Map *map, int null)
 {
     void init_edge_list();
     void add_edge_cell();
@@ -74,11 +74,12 @@
 	Rast_get_c_row(fd, &buf1[1], row);
 
 	for (col = 1; col <= ncols; col++) {
-	    if (buf1[col]	/* is a valid category */
+	    if ((buf1[col]	/* is a valid category */
 		&&(buf1[col - 1] != buf1[col]	/* 4 neighbors not the same? */
 		   ||buf1[col + 1] != buf1[col]
 		   || buf0[col] != buf1[col]
-		   || buf2[col] != buf1[col]))
+		   || buf2[col] != buf1[col])) &&
+		(null || !Rast_is_c_null_value(&buf1[col])))
 		add_edge_cell(map, buf1[col], row, col - 1);
 	}
     }

Modified: grass/trunk/raster/r.distance/main.c
===================================================================
--- grass/trunk/raster/r.distance/main.c	2014-05-26 19:19:21 UTC (rev 60496)
+++ grass/trunk/raster/r.distance/main.c	2014-05-26 20:15:26 UTC (rev 60497)
@@ -46,8 +46,8 @@
 	read_labels(&parms.map2);
     }
 
-    find_edge_cells(&parms.map1);
-    find_edge_cells(&parms.map2);
+    find_edge_cells(&parms.map1, parms.null);
+    find_edge_cells(&parms.map2, parms.null);
 
     report(&parms);
 

Modified: grass/trunk/raster/r.distance/parse.c
===================================================================
--- grass/trunk/raster/r.distance/parse.c	2014-05-26 19:19:21 UTC (rev 60496)
+++ grass/trunk/raster/r.distance/parse.c	2014-05-26 20:15:26 UTC (rev 60497)
@@ -25,7 +25,7 @@
 void parse(int argc, char *argv[], struct Parms *parms)
 {
     struct Option *maps, *fs;
-    struct Flag *labels, *overlap;
+    struct Flag *labels, *overlap, *null;
     const char *name, *mapset;
 
     maps = G_define_standard_option(G_OPT_R_MAPS);
@@ -44,6 +44,10 @@
     overlap->description =
 	_("Report zero distance if rasters are overlapping");
 
+    null = G_define_flag();
+    null->key = 'n';
+    null->description = _("Report null objects as *");
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -51,6 +55,8 @@
     mapset = parms->map1.mapset = G_find_raster2(name, "");
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"), name);
+    if (Rast_map_type(name, mapset) != CELL_TYPE)
+	G_fatal_error(_("Raster map <%s> is not CELL"), name);
 
     parms->map1.fullname = G_fully_qualified_name(name, mapset);
 
@@ -58,10 +64,13 @@
     mapset = parms->map2.mapset = G_find_raster2(name, "");
     if (mapset == NULL)
 	G_fatal_error(_("Raster map <%s> not found"), name);
+    if (Rast_map_type(name, mapset) != CELL_TYPE)
+	G_fatal_error(_("Raster map <%s> is not CELL"), name);
 
     parms->map2.fullname = G_fully_qualified_name(name, mapset);
 
     parms->labels = labels->answer ? 1 : 0;
     parms->fs = fs->answer;
     parms->overlap = overlap->answer ? 1 : 0;
+    parms->null = null->answer ? 1 : 0;
 }

Modified: grass/trunk/raster/r.distance/report.c
===================================================================
--- grass/trunk/raster/r.distance/report.c	2014-05-26 19:19:21 UTC (rev 60496)
+++ grass/trunk/raster/r.distance/report.c	2014-05-26 20:15:26 UTC (rev 60497)
@@ -43,17 +43,32 @@
     G_message(_("Processing..."));
 
     for (i1 = 0; i1 < map1->edges.ncats; i1++) {
+	int isnull1;
+
 	list1 = &map1->edges.catlist[i1];
+	isnull1 = parms->null ? Rast_is_c_null_value(&(list1->cat)) : 0;
+
 	for (i2 = 0; i2 < map2->edges.ncats; i2++) {
+	    int isnull2;
+
 	    list2 = &map2->edges.catlist[i2];
+	    isnull2 = parms->null ? Rast_is_c_null_value(&(list2->cat)) : 0;
+
 	    find_minimum_distance(list1, list2,
 				  &east1, &north1, &east2, &north2, &distance,
 				  &region, parms->overlap, map1->name,
 				  map2->name);
 
 	    /* print cat numbers */
-	    fprintf(stdout, "%ld%s%ld", (long)list1->cat, fs,
-		    (long)list2->cat);
+	    if (isnull1 && isnull2)
+	        fprintf(stdout, "*%s*", fs);
+	    else if (isnull1)
+	        fprintf(stdout, "*%s%ld", fs, (long)list2->cat);
+	    else if (isnull2)
+	        fprintf(stdout, "%ld%s*", (long)list1->cat, fs);
+	    else
+	        fprintf(stdout, "%ld%s%ld", (long)list1->cat, fs,
+		        (long)list2->cat);
 
 	    /* print distance */
 	    sprintf(temp, "%.10f", distance);



More information about the grass-commit mailing list