[GRASS-user] Python script: focal filter based on r.cost

Johannes Radinger JRadinger at gmx.at
Tue May 29 01:28:20 PDT 2012


Hi,

I try to create a tiny script that allows to create a focal filter based on r.cost. This means that rather using a moving window I use r.cost to create a "buffer" around a cell which is then used for getting certain statistics of the neighborhood of that cell (e.g. mean, max, sum) and create a new raster based on these statistical values. I'd like to use r.cost as it also allows to create distance buffers along rasterized lines (the main purpose for me). One example can be: "Get the maximum raster value which is up- and downstream  from a cell in a river distance of 500 m"...

So far I think I have a plan how it can work and I tried to write the script in python. Anyway there are some problems where I am not sure how I can solve them...

1) I don't really know how I can loop over the points (raster cells) and update the points with the statistical value (e.g. mean). I try following:

grass.write_command("db.execute", stdin = 'UPDATE point_tmp%s SET univar=%d WHERE cat = %d' % (str(os.getpid()),stat,cat))

but I get a "time out" (WARNING: Busy SQLITE db, already waiting for 10 seconds, 20 sec...). Probably it is related to the fact the two processes are using the same database table (loop over the points and update)... 

2) I don't know what is the best way to get a variable based on the input, which is like defining a global variable in a if-loop, but I am not sure if that is a good way?

Best is to look at the script which is attached. Theoretically it should be possible to be used on every raster. But as it loops over each cell it takes really long time to process large maps.

Maybe anyone wants to help me further developing this tiny python script. Any help is very much appreciated. Thank you!

Cheers
/johannes
-- 
NEU: FreePhone 3-fach-Flat mit kostenlosem Smartphone!                                  
Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a
-------------- next part --------------
A non-text attachment was scrubbed...
Name: distance_filter.py
Type: text/x-python-script
Size: 5631 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20120529/820a6bde/attachment.bin>


More information about the grass-user mailing list