[GRASS-dev] v.rast.stats wxPython GUI input reading error

Glynn Clements glynn at gclements.plus.com
Tue Feb 3 23:14:43 EST 2009


Hamish wrote:

> > "r.info -s" outputs DMS, when it should probably
> > use decimal (-g uses decimal for n/s/e/w).
> 
> g.region and libgis accept res=DD:MM:SS, so why should it use decimal?

So that everything which uses it doesn't have to explicitly convert it
to decimal.

> Usually for lat/lon resolutions (e.g. 2 minutes or 30 seconds) it is
> much better & more precisely+understandably represented by DD:MM:SS than
> some endless 0.00833333333333... number.  So IMO the GUI should be
> enhanced to accommodate DMS instead of the user being inconvenienced
> because it is simpler for the programmer.

This has nothing to do with the GUI.

> as to conformity with 'r.info -g' using decimal, I guess you could say that
> it matches 'g.region -g' which prefers decimal as it is used more with e.g.
> awk math, while r.info is more for the user's eyes..?

r.info with no flags is intended for the user. I'm not convinced that
the same is true of "r.info -s":

	$ r.info -s nations
	nsres=0:04:48
	ewres=0:04:48

Is it coincidence that the output uses shell syntax?

> maybe add decimal
> resolution to 'r.info -g' so user has access to both ways if they use
> it with awk scripts.

It isn't just awk. v.rast.stats is using Python's float() function,
which doesn't understand DMS either. In fact, very few generic
string-to-number conversion functions understand DMS.

The attached patch should solve this in grass.py, but needs testing,
but it still means that everything else needs to handle both DMS and
decimal for the output from "r.info -s". Chances are that anything
using r.info -s will make the same mistake.

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

-------------- next part --------------
Index: lib/python/grass.py
===================================================================
--- lib/python/grass.py	(revision 35750)
+++ lib/python/grass.py	(working copy)
@@ -557,15 +557,20 @@
     
     warning("Unable to write history for <%s>. Raster map <%s> not found in current mapset." % (map, map))
     return False
-    
+
+def float_or_dms(s):
+    return sum(float(x) / 60 ** n for (n, x) in enumerate(s.split(':')))
+
 # run "r.info -rgstmpud ..." and parse output
 
 def raster_info(map):
     """Return information about a raster map (interface to `r.info')."""
     s = read_command('r.info', flags = 'rgstmpud', map = map)
     kv = parse_key_val(s)
-    for k in ['min', 'max', 'north', 'south', 'east', 'west', 'nsres', 'ewres']:
+    for k in ['min', 'max', 'north', 'south', 'east', 'west']:
 	kv[k] = float(kv[k])
+    for k in ['nsres', 'ewres']:
+	kv[k] = float_or_dms(kv[k])
     return kv
 
 # interface to r.mapcalc


More information about the grass-dev mailing list