[GRASS-SVN] r32496 - grass/branches/develbranch_6/raster/r.random

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Aug 3 14:04:50 EDT 2008


Author: marisn
Date: 2008-08-03 14:04:50 -0400 (Sun, 03 Aug 2008)
New Revision: 32496

Modified:
   grass/branches/develbranch_6/raster/r.random/main.c
   grass/branches/develbranch_6/raster/r.random/random.c
   grass/branches/develbranch_6/raster/r.random/support.c
Log:
Fixed cover map support for raster output;
Eliminated use of unitialised value

Modified: grass/branches/develbranch_6/raster/r.random/main.c
===================================================================
--- grass/branches/develbranch_6/raster/r.random/main.c	2008-08-03 18:00:51 UTC (rev 32495)
+++ grass/branches/develbranch_6/raster/r.random/main.c	2008-08-03 18:04:50 UTC (rev 32496)
@@ -33,7 +33,6 @@
     double percentage;
     long targets;
     long count;
-    int zero;
     struct rr_state  myState;
 
     struct GModule *module;
@@ -46,7 +45,7 @@
     module->keywords = _("raster");
     module->description =
         _("Creates a raster map layer and vector point map "
-        "containing randomly located sites.");
+        "containing randomly located points.");
 
     parm.input = G_define_standard_option(G_OPT_R_INPUT) ;
     parm.input->description= _("Name of input raster map") ;
@@ -73,7 +72,7 @@
 
     flag.zero = G_define_flag() ;
     flag.zero->key         = 'z' ;
-    flag.zero->description = _("Generate vector points also for NULL category");
+    flag.zero->description = _("Generate points also for NULL category");
 
     flag.info = G_define_flag() ;
     flag.info->key         = 'i' ;
@@ -105,12 +104,12 @@
     if (myState.mapset == NULL)
 	G_fatal_error (_("Raster map <%s> not found"), myState.inraster);
 
-    if (parm.cover->answer) {
+    if (myState.docover == 1) {
 	myState.cmapset = G_find_cell (myState.inrcover, "");
 	if (myState.cmapset == NULL)
 	    G_fatal_error (_("Raster map <%s> not found"), myState.inrcover);
     }
-
+    
     /* If they only want info we ignore the rest */
     get_stats(&myState);
 
@@ -169,7 +168,7 @@
     {
         if (targets > count)
         {
-            if (zero)
+            if (myState.use_nulls)
                 G_fatal_error (_("There aren't [%ld] cells in the current region"),
 			       targets);
             else

Modified: grass/branches/develbranch_6/raster/r.random/random.c
===================================================================
--- grass/branches/develbranch_6/raster/r.random/random.c	2008-08-03 18:00:51 UTC (rev 32495)
+++ grass/branches/develbranch_6/raster/r.random/random.c	2008-08-03 18:04:50 UTC (rev 32496)
@@ -28,6 +28,7 @@
     struct line_pnts *Points;
     struct line_cats *Cats;
     int cat;
+    RASTER_MAP_TYPE type;
 
     G_get_window (&window);
 
@@ -45,10 +46,15 @@
     }
 
     if (theState->outraster != NULL) {
-        if ((outfd = G_open_raster_new (theState->outraster, theState->buf.type)) < 0)
+        if (theState->docover == 1) 
+            type = theState->cover.type;
+        else
+            type = theState->buf.type;
+        if ((outfd = G_open_raster_new (theState->outraster, type)) < 0)
             G_fatal_error (_("Cannot create raster map <%s>"), 
 			   theState->outraster);
         theState->fd_new = outfd;
+        
     }
 
     if (theState->outvector) {
@@ -122,16 +128,14 @@
                 G_fatal_error (_("Cannot read raster row [%d] from cover raster map <%s>"),
 		    	       row, theState->inrcover);
         }
+        
         for (col = 0; col < ncols && nt ; col++)
         {
 	    if (!theState->use_nulls && is_null_value(theState->buf, col) )
                 continue;
             if (theState->docover == 1) {  /* skip no data cover points */
-		if (!theState->use_nulls && is_null_value(theState->cover, col) ) {
-			/* If cover is NULL, then output aso must be NULL */
-			set_to_null(&theState->buf, col); 
-			continue;
-		}
+		if (!theState->use_nulls && is_null_value(theState->cover, col) ) 
+                    continue;
             }
 
             if (make_rand() % nc < nt)
@@ -193,22 +197,34 @@
         }
 
         while (col < ncols) {
-                set_to_null(&theState->buf, col++);
+                set_to_null(&theState->buf, col);
                 if (theState->docover == 1)
-		    set_to_null(&theState->cover, col++);
+		    set_to_null(&theState->cover, col);
+                col++;
 	}
 
-        if (theState->outraster) /* nothing to do in case of cover map */
-            G_put_raster_row(outfd, theState->buf.data.v, theState->buf.type);
+        if (theState->outraster) { 
+            if (theState->docover == 1)  
+                G_put_raster_row(outfd, theState->cover.data.v, theState->cover.type);
+            else 
+                G_put_raster_row(outfd, theState->buf.data.v, theState->buf.type);
+        }
     }
 
     /* Catch any remaining rows in the window*/
     if (theState->outraster && row < nrows) {
-        for ( col = 0 ; col < ncols; col++)
-            set_to_null(&theState->buf, col);
-        
-        for ( ; row < nrows ; row++)
-            G_put_raster_row(outfd, theState->buf.data.v, theState->buf.type);
+        for ( col = 0 ; col < ncols; col++) {
+            if (theState->docover == 1) 
+                set_to_null(&theState->cover, col);
+            else
+                set_to_null(&theState->buf, col);
+        }
+        for ( ; row < nrows ; row++) {
+            if (theState->docover == 1)
+                G_put_raster_row(outfd, theState->cover.data.v, theState->cover.type);
+            else
+                G_put_raster_row(outfd, theState->buf.data.v, theState->buf.type);
+        }
     }
             
     if (nt > 0)

Modified: grass/branches/develbranch_6/raster/r.random/support.c
===================================================================
--- grass/branches/develbranch_6/raster/r.random/support.c	2008-08-03 18:00:51 UTC (rev 32495)
+++ grass/branches/develbranch_6/raster/r.random/support.c	2008-08-03 18:04:50 UTC (rev 32496)
@@ -9,19 +9,33 @@
     struct History hist;
     struct Categories cats;
     struct Colors clr;
+    char *inraster, *mapset;
+    struct RASTER_MAP_PTR nulls;
     
-    /* write categories for output raster */
-    if(G_read_raster_cats (theState->inraster, theState->mapset, &cats) >= 0)
+    /* write categories for output raster 
+    	use values from input or cover map
+    */
+    if (theState->docover == 1) {
+        inraster = theState->inrcover;
+        mapset = theState->cmapset;
+        nulls = theState->cnulls;
+    }
+    else {
+        inraster = theState->inraster;
+        mapset = theState->mapset;
+        nulls = theState->nulls;
+    }
+    if(G_read_raster_cats (inraster, mapset, &cats) >= 0)
     {
-	sprintf (title, "Random sites on [%s in %s]", 
-                theState->inraster, theState->mapset);
+	sprintf (title, "Random points on [%s in %s]", 
+                inraster, mapset);
 	G_set_cats_title (title, &cats);
 	if (theState->use_nulls)
-	    G_set_raster_cat (theState->nulls.data.v,
-                    theState->nulls.data.v,  
-                    "Sites with NULL values in original",
+	    G_set_raster_cat (nulls.data.v,
+                    nulls.data.v,  
+                    "Points with NULL values in original",
                     &cats,
-                    theState->nulls.type);
+                    nulls.type);
 	G_write_raster_cats (theState->outraster, &cats);
     }
 
@@ -29,14 +43,14 @@
     if (G_read_history (theState->outraster, G_mapset(), &hist) >= 0)
     {
         G_short_history(theState->outraster, "raster", &hist);
-	sprintf (hist.datsrc_1, "Based on map [%s in %s]", 
-                theState->inraster, theState->outraster);
+	sprintf (hist.datsrc_1, "Based on map <%s@%s>", 
+                inraster, mapset);
 	if (percent)
-	    sprintf (hist.datsrc_2, "Random sites over %.2f percent of the base map <%s>",
-                    percentage, theState->inraster);
+	    sprintf (hist.datsrc_2, "Random points over %.2f percent of the base map <%s>",
+                    percentage, inraster);
 	else
-	    sprintf (hist.datsrc_2, "%ld random sites on the base map <%s>", 
-                    theState->nRand, theState->inraster);
+	    sprintf (hist.datsrc_2, "%ld random points on the base map <%s@%s>", 
+                     theState->nRand, theState->inraster, theState->mapset);
 	G_write_history (theState->outraster, &hist);
 
     }
@@ -52,13 +66,13 @@
     }
 
     /* set colors for output raster */
-    if (G_read_colors (theState->inraster, theState->mapset, &clr) >= 0)
+    if (G_read_colors (inraster, mapset, &clr) >= 0)
     {
         if (theState->use_nulls)
         {
-            G_add_raster_color_rule (theState->nulls.data.v, 127, 127, 127, 
-                    theState->nulls.data.v, 127, 127, 127, &clr,
-                    theState->nulls.type);
+            G_add_raster_color_rule (nulls.data.v, 127, 127, 127, 
+                    nulls.data.v, 127, 127, 127, &clr,
+                    nulls.type);
         }
         G_write_colors (theState->outraster, G_mapset(), &clr);
     }



More information about the grass-commit mailing list