Michael -<br><br>I often couple GRASS to numerical models or complex grid-based computations using the following method:<br><br>Step 1: Export the environmental grids needed for the model using r.out.arc (an easy format to parse in Python) or a simple binary format.
<br>Step 2: Read the data into a Python class that represents the grid as a 2D array (numeric is great for this). The class also has a few helper methods such as: read_grid(), write_grid(), get(x, y), set(x,y), etc.<br>Step 3: Run the model
<br>Step 4: Dump the python grid back to the ASCII Grid format (or raw binary)<br>Step 5: Read the data back into GRASS using r.in.arc (etc)<br><br>Most of the work in the model occurs in Steps 2-4 where there is no overhead on the model due to carrying around the GIS baggage. You could use python, R, Matlab, whatever.
<br><br>As an unsolicited aside, C is actually a pretty good language for grid-based models when one uses a numerical library such as the GNU Scientific Library to handle the matrix (grid) stuff. Two bonuses to using C are the increase in performance of the model and by the end of the project, you have learned enough C to read and start using the GRASS source code directly. Not only that, but there are lots of GIS-related libraries (
i.e., GDAL, Proj4, Shapelib) that are easy to use from C once you learn the basics.<br><br>I resisted C for years because Python was good enough and C was &quot;hard&quot;.&nbsp; Today I think that was being penny-wise and pound foolish.&nbsp; There are many projects in GIS where speed really matters. Whether you are filtering a very large Lidar data set, or running a very small model millions of times (such as in a Monte Carlo simulation), speed really matters in these cases. The huge library of existing C code is real boon, too. I find that for small projects (&lt; 10k lines of code) C is a pretty good language; its warts don&#39;t show up until project get really large. And like I said, it&#39;s nice to be able to customize GRASS source when you need to.
<br><br>Cheers,<br><br>David<br><br><br><br><div><span class="gmail_quote">On 6/14/07, <b class="gmail_sendername">Michael Barton</b> &lt;<a href="mailto:michael.barton@asu.edu">michael.barton@asu.edu</a>&gt; wrote:</span>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Thanks very much. I&#39;ll need to digest all this useful information later<br>today, but it is helpful. I&#39;ll check 
r.in.xyz and your update to r.in.poly<br>and get back to you.<br><br>Michael<br><br><br>On 6/14/07 2:45 AM, &quot;Hamish&quot; &lt;<a href="mailto:hamish_nospam@yahoo.com">hamish_nospam@yahoo.com</a>&gt; wrote:<br><br>&gt; Michael Barton wrote:
<br>&gt;&gt; We have finally had an initial production test of methods to create a<br>&gt;&gt; raster point using xy coordinates. If you remember, I&#39;ve periodically<br>&gt;&gt; whined a bit about the lack of a way to create and change a raster
<br>&gt;&gt; cell based on its xy coordinates instead of its cat value.<br>&gt;&gt;<br>&gt;&gt; The 2 suggestions I got to work around this were to...<br>&gt;&gt;<br>&gt;&gt; 1) use r.in.poly to create the raster map; make an input file such
<br>&gt;&gt; that for each point, the input is set to line (L) with 2 points that<br>&gt;&gt; are identical.<br>&gt;<br>&gt; To make that method easier I have just added a &quot;P&quot; instruction to<br>&gt; r.in.poly in 
6.3cvs. That uses G_plot_point(), which draws a 0 length<br>&gt; line using the same method, so probably it doesn&#39;t fix your 2 cell if<br>&gt; on bound problem (?? try it and see).<br>&gt;<br>&gt;&gt; 2) use v.in.ascii
 and then use v.to.rast to create the raster map.<br>&gt;<br>&gt; Note the v.to.rast code is ~95% a clone of the r.in.poly code, so results<br>&gt; (and problems) should be mostly the same. Do you see the same multiple<br>
&gt; cell trouble if the point falls on a bound using this method?<br>&gt;<br>&gt; It probably needs to be noted in the v.to.rast help page if it does, at<br>&gt; minimum.<br>&gt;<br>&gt;<br>&gt; 3) use r.in.xyz (I expect this will be the fastest method)
<br>&gt;<br>&gt; 4) r.mapcalc outmap=&#39;if( (abs(x() - $x)) &lt; ewres()) \<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; (abs(y() - $y) &lt; nsres(), $val, null() )&#39;<br>&gt; [or something like that, for a single point; yuk]<br>&gt;<br>
&gt; 5) do the gridding yourself and create a r.in.ascii file.<br>&gt;<br>&gt; all these would need to be followed by a call to r.patch.<br>&gt; (see d.rast.edit.tcl method)<br>&gt;<br>&gt;<br>&gt;&gt; We are coupling an agent-based simulation to GRASS, such that the
<br>&gt;&gt; simulated land use activities will affect land cover, which in turn<br>&gt;&gt; will affect a landscape evolution routine.<br>&gt;<br>&gt; are you updating a sequential string of cells, blocks of cells, or<br>
&gt; random cells at each update?<br>&gt;<br>&gt;<br>&gt;&gt; Optimizing the speed of this is critical, and so we prefer to reduce<br>&gt;&gt; the number of steps needed to accomplish this. This means that the<br>&gt;&gt; 
r.in.poly approach is the one we prefer. However, in tests conducted<br>&gt;&gt; today, we ran into an unexpected problem.<br>&gt;&gt;<br>&gt;&gt; If the coordinate of the point to be created happens to fall on a cell<br>
&gt;&gt; boundary, we get a 2-cell &#39;line&#39; instead of a point even though the<br>&gt;&gt; beginning and end points are identical. Apparently, r.in.poly looks<br>&gt;&gt; for a cell center closest to end point one and then searches for a
<br>&gt;&gt; cell center for end point 2 that is different from the first cell,<br>&gt;&gt; resulting in a 2 cell line. This does not happen if the point does not<br>&gt;&gt; fall on a cell boundary. Attached is the raster file created from the
<br>&gt;&gt; following r.in.poly input. The first input creates the 2 yellow cells,<br>&gt;&gt; the other 2 create the red and green single cells.<br>&gt;<br>&gt; r.in.poly/raster.c defines the &quot;cont&quot; continue line fn used by
<br>&gt; G_setup_plot() (and thus all following G_plot_*() commands) with<br>&gt; G_bresenham_line(). That in turn uses the &quot;dot&quot; fn to draw the points;<br>&gt; which &quot;dot&quot; is used depends on the result of the getformat() scan of
<br>&gt; the cat values (&amp; I&#39;m about foggy about what that&#39;s all about).<br>&gt;<br>&gt; for info on the Bresenham line rasterization method see:<br>&gt;&nbsp;&nbsp; <a href="http://en.wikipedia.org/wiki/Bresenham&#39;s_line_algorithm">
http://en.wikipedia.org/wiki/Bresenham&#39;s_line_algorithm</a><br>&gt;<br>&gt;<br>&gt;&gt; This is a potential problem, since an agent might not know that the<br>&gt;&gt; the coordinates it chooses for a field to farm or graze like on a cell
<br>&gt;&gt; boundary. It is OK if r.in.poly substitutes the nearest grid cell<br>&gt;&gt; center to make the raster point for the xy coordinates that the agent<br>&gt;&gt; chooses, but not OK if it picks 2 different cells instead of one.
<br>&gt;<br>&gt; Try r.in.xyz, it shouldn&#39;t suffer from this: on one side of the cell it<br>&gt; tests for &lt;= and on the other side &lt;. So a point on a bound always fall<br>&gt; in the &lt;= cell. The exception is the outer E,W raster boundaries which
<br>&gt; will make values fall &quot;into&quot; the raster if on the bound.<br>&gt;<br>&gt; r.in.xyz doesn&#39;t let you add cat labels, but you can do that manually by<br>&gt; creating a cats/ file. See spearfish60/PERMANENT/cats/landuse for an
<br>&gt; example, it&#39;s very simple.<br>&gt;<br>&gt;<br>&gt;&gt; The simplest solution is to allow r.in.poly to make points as well as<br>&gt;&gt; lines and areas. I&#39;m not sure why it doesn&#39;t allow points in the first
<br>&gt;&gt; place. It seems like an oversight. Is this difficult to implement?<br>&gt;<br>&gt; done, but I don&#39;t think it will help any due to the way G_plot_point()<br>&gt; works.<br>&gt;<br>&gt;<br>&gt;&gt; * * * *
<br>&gt;&gt;<br>&gt;&gt; Also, there is still no one-step way to change a cell value based on<br>&gt;&gt; its xy coordinates.<br>&gt;<br>&gt; So you want a non-interactive d.rast.edit, like v.edit?<br>&gt;<br>&gt; A couple of ways to do that, 1) get input x1,y1,cat1 ... from stdin,
<br>&gt; qsort() based on y then x, read each line and if there&#39;s an updated<br>&gt; cell in it replace it before writing the row back out. (as r.patch)<br>&gt; 2) calculate the array column,row which contains each x,y and update
<br>&gt; each in the order they arrive (as r.in.xyz). Probably the fastest way is<br>&gt; a combination of the two. Sort by y, and for each row update the column<br>&gt; values in the order they arrive, then write out the finished line and
<br>&gt; move on to the next row.<br>&gt;<br>&gt;<br>&gt;&gt; However, if there is interest in doing agent and cellular modeling<br>&gt;&gt; within a GRASS platform, it will become necessary to be able to do<br>&gt;&gt; this to simulate dynamics. Patching may simply not be practical for
<br>&gt;&gt; this kind of work. As Python becomes more used as a scripting<br>&gt;&gt; environment, this is an attractive kind of application to build in<br>&gt;&gt; Python, given its object oriented structure, and couple with GRASS.
<br>&gt;<br>&gt; The module probably isn&#39;t so hard to write, in C. r.example should give<br>&gt; a nice start.<br>&gt;<br>&gt; It would be interesting to rewrite r.example.c using the SWIG Python<br>&gt; interface, and see if that gains any simplicity over C, or if it just
<br>&gt; adds complexity without simplicity -- as you still need to go through<br>&gt; all the same chores.<br>&gt;<br>&gt;<br>&gt; Hamish<br><br>__________________________________________<br>Michael Barton, Professor of Anthropology
<br>School of Human Evolution &amp; Social Change<br>Center for Social Dynamics &amp; Complexity<br>Arizona State University<br><br>phone: 480-965-6213<br>fax: 480-965-7671<br>www: <a href="http://www.public.asu.edu/~cmbarton">
http://www.public.asu.edu/~cmbarton</a><br><br><br>_______________________________________________<br>grass-dev mailing list<br><a href="mailto:grass-dev@grass.itc.it">grass-dev@grass.itc.it</a><br><a href="http://grass.itc.it/mailman/listinfo/grass-dev">
http://grass.itc.it/mailman/listinfo/grass-dev</a><br></blockquote></div><br><br clear="all"><br>-- <br>David Finlayson, Ph.D.<br>Operational Geologist<br><br>U.S. Geological Survey<br>Pacific Science Center<br>400 Natural Bridges Drive
<br>Santa Cruz, CA&nbsp;&nbsp;95060, USA<br><br>Tel: 831-427-4757, Fax: 831-427-4748, E-mail: <a href="mailto:dfinlayson@usgs.gov">dfinlayson@usgs.gov</a>