[GRASSLIST:174] Re: Reading/writing maps
David Finlayson
dfinlays at u.washington.edu
Sun May 25 04:34:13 EDT 2003
I find that I have to script cell-by-cell processing frequently, and
occasionally, I want to store the results back as a new raster.
I have written a couple of primitive Python classes that read and write
the r.out.arc ascii format. I basically stored the whole raster in a
numeric array along with its header information as a short dictionary.
Then I overloaded most of Python's operators so that this "raster"
object could be used in math statements. Add a get(row, col) and a
put(row, col) and you have a fairly complete re-implementation of
r.map.calc in about 300 lines of Python (try that in C!). This even
gives you the full power of Python's numeric library for additional fun.
Now the down side is the initial dump from grass and reload is really a
waist of time. And there is a definite disconnect between the grass
code and the Python code (even if they do work together nicely)
Ideally, we should run SWIG over the Grass API and build some Python
wrapper classes that encapsulate the C function library. I would
envision a 2 stage process: 1) get Python talking to the C Api, then 2)
build some convenience classes that make it easy to work with the data
structures from within Python. The idea is to get the power of the C
api into a package that can be easily learned and utilized by Grass
users. Ease of use is the number one goal, performance is important but
secondary (of course, performance is where real programmers are
important rather than a half-baked geologist!)
We could call it the PyGRASS API!
Now, what will my comittee think of this distraction?
David
Half-baked geologist
Soil & Water Lab. wrote:
>>>And now, reverse question : is it possible to create a raster from stdin
>>>? I was having in mind to send the output of "r.stats -1", play with it,
>>>and send it back to...to...to this mysterious command that may not even
>>>exist, without having to write any file on the hard disk. Of course, you
>>>have guessed that I do not speak C.
>>
>>r.in.ascii and r.in.{pbm,pgm,ppm} all accept text (non-binary) input.
>
>
> OK, a r.in.ascii commands would work, provided that I don't forget to put the
> proper header before the processed stream (and I guess I can grab the header
> from lines 3-8 of an adequate WIND file fairly easily). I wonder if it's
> worth, eventually, computer-time-wise, to process/compute maps with a script
> instead of a C prog... For fairly simple calculations, "r.mapcalc" gives
> results fast enough. For more complex computations, that can be an issue,
> though, I'm afraid I'll have to learn C. Or at least Perl
> Still, thanks for the r.in.ascii trick.
> P.
> PS: the link to the HTML version of the GRASS5.0 Programmer manual is broken.
>
--
David Finlayson
School of Oceanography
Box 357940
University of Washington
Seattle, WA 98195-7940
USA
Office: Marine Sciences Building, Room 112
Phone: (206) 616-9407
Web: http://students.washington.edu/dfinlays
More information about the grass-user
mailing list