[GRASS-user] integration of sample.c algorithms into r.resample

Dylan Beaudette dylan.beaudette at gmail.com
Wed Aug 9 17:01:04 EDT 2006


On Wednesday 09 August 2006 13:12, Dylan Beaudette wrote:
> On Wednesday 09 August 2006 12:42, Glynn Clements wrote:
> > Dylan Beaudette wrote:
> > > A quick follow-up:
> > >
> > > in the source for r.resample, it looks like the resampling is done in
> > > this loop:
> > >
> > > for (row = 0; row < nrows; row++)
> > > 	{
> > > 		if (verbose)
> > > 			G_percent (row, nrows, 2);
> > > 		if (G_get_raster_row (infd, rast, row, data_type) < 0)
> > > 			exit(1);
> > > 		if (G_put_raster_row (outfd, rast, out_type) < 0)
> > > 			exit(1);
> > > 		G_mark_raster_cats (rast, ncols, &cats, data_type);
> > > 	}
> > >
> > >
> > > ...via the NN style resampling associated with converting between
> > > regions of differing resolution.. (?)
> > >
> > > Is this a correct interpretation?
> >
> > Yes. G_get_raster_row() etc automatically resample the raster data
> > (using nearest-neighbour) to match the current region resolution,
> > while G_put_raster_row() creates a map which matches the current
> > region.
>
> Thanks for the quick reply Glynn.
>
> > If you want any other conversion, you have to keep switching regions,
> > so that input maps are read at their native resolution, while output
> > maps are written at the desired resolution.
>
> Yikes. That does complicate things quite a bit. I wonder if
> G_get_raster_row() could be modified such that it can perform these three
> resampling strategies as well. i.e. such that if one wanted a different
> resampling approach in any function that uses G_get_raster_row(), it would
> be possible to specify bilinear or cubic convolution... Although this might
> be a monumental task.
>
> > For this reason, you may want to keep your new resampling program
> > separate from r.resample, as it will require a fundamentally different
> > structure.
>
> Sure. Perhaps a look at r.bilinear would be helpful. What I am envisioning
> is something like r.resample / r.bilinear that would integrate the three
> methods- as there doesn't appear to be an r.resamp.cubic solution.
>
> Cheers,

replying to myself again...

Ok, it looks like it won't be _too_ difficult to get the generalized sampling 
routines into a derivative of r.bilinear ! 


Quickly hacking the r.bilinear source to include support for the above 
mentioned routines:

/* old method */
new = (1.0 - t) * (1.0 - u) * c1 +
			t * (1.0 - u) * c2 +
			t * u * c4 +
			u * (1.0 - t) * c3;

outbuf[col] =  (new + .5);

		    
/* generalized sampling */
new_sample = G_get_raster_sample(infile, &w, NULL, north, east, 0, BILINEAR);

  /* new method */
outbuf[col] =  new_sample;


However, this does not quite work, as the _new_ method is ignoring issues 
associated with edge effects on moving window calculations. for example:

u and t are set as follows:

/* on edges? */
	    if (north >= (mapw.north - mapw.ns_res/2.)){
		maprow1 = 0;
		maprow2 = 1;
	        u = 0.0;
	    }
	    else if (north <= (mapw.south + mapw.ns_res/2.)){
		maprow2 = mapw.rows - 1;
		maprow1 = maprow2 - 1;
	        u = 1.0;
	    }
 ... else ...
u = (mapnorth - north)/mapw.ns_res;

variable t is set in a similar fashion. 

Perhaps a bit more work with this approach will yield a workable solution...

see attached figures for a graphical description of the edge effects 
introduced:

1. comparison 1: note main point cloud and 1:1 line where the two methods are 
quite close. also note the second cloud suggesting edge effects

2. comparison 2: simple display of residuals based on: 
lm(sample.c::bilinear ~ r.bilinear)


Dylan


-- 
Dylan Beaudette
Soils and Biogeochemistry Graduate Group
University of California at Davis
530.754.7341
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sample_comparison_1.png
Type: image/png
Size: 8253 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/grass-user/attachments/20060809/281cd6e0/sample_comparison_1.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sample_comparison_2.png
Type: image/png
Size: 36162 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/grass-user/attachments/20060809/281cd6e0/sample_comparison_2.png


More information about the grass-user mailing list