[GRASS-dev] Test if raster map and current region match?

Glynn Clements glynn at gclements.plus.com
Tue Jun 24 23:13:49 EDT 2008


Roger Bivand wrote:

> >>> but an internal match (where r.resample would be a no-op or not) would be
> >>> helpful.
> >>
> >> Any hints for Roger?
> >
> > "r.info -gs" will print both the bounds and resolution in shell-script
> > style:
> >
> > $ r.info -gs elevation.dem
> > north=4928000
> > south=4914020
> > east=609000
> > west=590010
> > nsres=30
> > ewres=30
> >
> > Although the output of the two commands is slightly different, it's
> > close enough that the keys will sort the same, so you can use e.g.:
> >
> > map=map127
> > ( g.region -g ; r.info -gs $map ) | sort | grep '^[nsew]' | sed 's/^.*=//' | uniq -u | grep . &> /dev/null
> > if [ $? = 0 ] ; then
> >    echo different
> > else
> >    echo same
> > fi
> >
> 
> Thanks, elegant, I know. But it isn't cross-platform. All I need is an 
> answer TRUE/FALSE for the comparison of the 6 values. I assume that the 
> printf()s are using the same rounding, right? Both r.info and g.region use 
> G_format_easting(), G_format_northing(), G_format_resolution() in 
> lib/gis/wind_format.c, using format_double(), which is hard-wired to 
> "%.8f". Are we going to pick up numerical fuzz anywhere from conversion 
> to text (high longlat resolution)?

Yeah, lat/lon could be a problem (r.info uses d:m:s while g.region
uses decimal), as could fractional coordinates, due to precision
issues.

> I guess I'll have to trust the text representation, read it into R, 
> convert to doubles, and ask abs(rast$w - wind$w) < .Machine$double.eps for 
> each of the six comparisons, and perhaps handle longlat with a slacker 
> precision.

If .Machine$double.eps is what I think it is, it's too small. You
would need to use the formatting precision (i.e. 1e-8 for %.8f).

Actually, if you're trying to determine whether r.resample is a no-op,
the ideal test would be to use the region definition to convert the
centres of the corner cells to row/col indices, and check that the
results are (0, 0, cols-1, rows-1). That will catch the case where the
region isn't quite the same as the raster, but close enough that
resampling is actually a no-op.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list