<b><u>1) if you want to use the GRASS modules from outside in the shell (terminal) without opening the GRASS application<br></u></b><br><a href="http://grass.osgeo.org/wiki/GRASS_and_Python">GRASS_and_Python</a> gives examples to set the environment variables to call the GRASS modules from outside for Windows and Linux, but nothing for Mac OS X. T<span id="result_box" class="" lang="en"><span class="hps">he solution</span> <span class="hps">is easy if</span> <span class="hps">you know the structure</span> <span class="hps">of</span> the applications<span class="hps"> of </span></span><span class="gD">William Kyngesburye</span><span id="result_box" class="" lang="en"><span class="hps">:<br>
<br></span></span><div style="margin-left:40px"><b><span id="result_box" class="" lang="en"><span class="hps">export GISBASE="/Applications/GRASS-6.4.app/Contents/MacOS"</span></span></b><br><span id="result_box" class="" lang="en"><span class="hps">export PATH="$PATH:$GISBASE/bin:$GISBASE/script:$GISBASE/lib"</span></span><br>
<span id="result_box" class="" lang="en"><span class="hps">export PYTHONPATH="${PYTHONPATH}:$GISBASE/etc/python/"</span></span><br><span id="result_box" class="" lang="en"><span class="hps">export PYTHONPATH="${PYTHONPATH}:$GISBASE/etc/python/grass"</span></span><br>
<span id="result_box" class="" lang="en"><span class="hps">export PYTHONPATH="${PYTHONPATH}:$GISBASE/etc/python/grass/script"</span></span><br><span id="result_box" class="" lang="en"><span class="hps">export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$GISBASE/lib"</span></span><br>
<span id="result_box" class="" lang="en"><span class="hps">export GIS_LOCK=$$</span></span><br><span id="result_box" class="" lang="en"><span class="hps">export GISRC="/Users/username/.grassrc6"</span></span> <span class="Apple-style-span" style="color:red"></span><br>
<br></div><span id="result_box" class="short_text" lang="en"><span class="hps">if</span> <span class="hps">you have not</span> <span class="hps">previously</span> <span class="hps">defined </span></span><span id="result_box" class="" lang="en"><span class="hps">what is in </span></span><span id="result_box" class="short_text" lang="en"><span class="hps">PATH </span></span><span id="result_box" class="" lang="en"><span class="hps"> in .bash_profile (</span></span><span id="result_box" class="" lang="en"><span class="hps">as</span> <span class="hps">advocated by</span> <span class="hps">William </span></span><span class="gD">Kyngesburye for the frameworks</span><span id="result_box" class="" lang="en"><span class="hps">)</span></span><span id="result_box" class="" lang="en"><span class="hps"> </span></span><span id="result_box" class="" lang="en"><span class="hps"> you can use the solution </span></span><span id="result_box" class="" lang="en"><span class="hps"></span><span class="hps">given by ijufuy </span></span>in <a href="http://yfujimoto.blogspot.be/2011/06/grass-gis-programming-with-python-on.html">GRASS GIS programming with Python on Mac OS X</a> <span id="result_box" class="" lang="en"><span class="hps">, </span></span><span id="result_box" class="" lang="en"><span class="hps">adapting it to</span> <span class="hps">your versions</span> <span class="hps">of</span> <span class="hps">GDAL</span> <span class="hps">or</span> <span class="hps">PROJ</span> <span class="hps">frameworks</span></span><br>
<span id="result_box" class="" lang="en"><span class="hps">If you want to</span> <span class="hps">use the applications</span> <span class="hps"></span></span>of <span class="gD">Michael Barton</span>:<br>
<br>
<div style="margin-left:40px"><b><span id="result_box" class="" lang="en"><span class="hps">export GISBASE="/Applications/GRASS/GRASS-x.x.app/Contents/MacOS" </span></span></b></div><br><br><span id="result_box" class="" lang="en"><span class="hps">After, you can call the GRASS modules with Python and grass.script in the shell without opening GRASS-6.x.app:</span></span><br>
<br><div style="margin-left:40px">$ python<br>Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) <br>[GCC 4.2.1 (Apple Inc. build 5646)] on darwin<br>Type "help", "copyright", "credits" or "license" for more information.<br>
</div><div style="margin-left:40px">>>> import os<br></div><div style="margin-left:40px">>>> import grass.script as grass<br>>>> import grass.script.setup as gsetup<br>>>> gisbase = os.environ['GISBASE']<br>
>>> gisdb="/Users/username/grassdata"<br>>>> location="geol"<br>>>> mapset="mymapset"<br>>>> gsetup.init(gisbase, gisdb, location, mymapset)<br>>>> # <u>table</u><br>
>>> desc = grass.parse_command('db.describe', flags='c', table="bxltot")<br>>>> dict.keys(desc)<br>['Column 10: Z10:INTEGER:11', 'Column 9: epbxl:INTEGER:11', 'Column 14: BXL_COR:INTEGER:11', 'Column 4: IDENT:INTEGER:11', 'Column 8: KOR:INTEGER:11', 'Column 1: cat:INTEGER:11', 'Column 6: LED:INTEGER:11', 'ncols: 14', 'nrows: 47', 'Column 5: Z:INTEGER:11', 'Column 13: Z_BXL:INTEGER:11', 'Column 2: IGN:DOUBLE PRECISION:20', 'Column 12: KOR10:INTEGER:11', 'Column 3: AFFLEUREME:DOUBLE PRECISION:20', 'Column 11: BXL10:INTEGER:11', 'Column 7: BXL:INTEGER:11']<br>
>>> # <u>geometry</u><br>>>> vector = "bxltot"<br>>>> points = grass.read_command("v.to.db", flags="p", map=vector, type="point", option="coor", units="meters", quiet="True")<br>
>>> pt = points.split("\n")<br>>>>> xyz = []<br>>>> for i in pt:<br>... xyz.append(pt[0].split("|"))<br>>>> xyz<br>[['1', '114718.535582253', '119568.077575195', '0'], ['1', '114718.535582253', '119568.077575195', '0'], ...<br>
<br></div><span id="result_box" class="" lang="en"><span class="hps"></span></span><br><div style="margin-left:40px"><br></div><b><u><span id="result_box" class="" lang="en"><span class="hps">2) If you</span> <span class="hps">only want to have</span> <span class="hps">access to</span> the <span class="hps">layers of</span> <span class="hps">GRASS, use the GDAL/OGR Python bindings</span></span></u></b><span id="result_box" class="" lang="en"><span class="hps"><br>
</span></span><br><div style="margin-left:40px">>>> from osgeo import ogr<br>>>> # open grass vector layer<br>>>> ds = ogr.Open('/Users/username/grassdata/geol/mymapset/vector/bxltot/head')<br>
>>> # vector layer <br>>>> layer = ds.GetLayer(0)<br>>>> layer.GetName()<br>>>> 'bxltot'<br>>>> # <u>table</u><br>>>> ldefn = layer.GetLayerDefn()<br>>>> schema = []<br>
>>> for n in range(ldefn.GetFieldCount()):<br>... fdefn = ldefn.GetFieldDefn(n)<br>... schema.append((<a href="http://fdefn.name">fdefn.name</a>, fdefn.type))<br>>>> schema<br>[('cat', 0), ('IGN', 2), ('AFFLEUREME', 2), ('IDENT', 0), ('Z', 0), ('LED', 0), ('BXL', 0), ('KOR', 0), ('epbxl', 0), ('Z10', 0), ('BXL10', 0), ('KOR10', 0), ('Z_BXL', 0), ('BXL_COR', 0)]<br>
>>> # <u>geometry</u><br>>>> points = []<br>>>> for index in xrange(layer.GetFeatureCount()):<br>... feature = layer.GetFeature(index)<br>... geometry = feature.GetGeometryRef()<br>... points.append((geometry.GetX(), geometry.GetY()))<br>
>>> points<br>[(114718.535582253,119568.077575195), (114718.535582253,119568.077575195), ....<br><br></div><b><u>3) </u><u>advant<span class="b1">ag</span><span class="b2">es and disadvantages of</span><span class="b3"> the</span><span class="b4"> </span><span class="b5">approach</span></u></b><br>
<br>If you know Python, you can combine grass.script with all the others geospatial or scientific modules (and matplotlib, for example, even in the GRASS Python shell).<br><br><br><div style="text-align:center"><img alt="Images intégrées 1" src="cid:ii_13866b6434f88348" height="239" width="420"><br>
<div style="text-align:left">You can also combine grass.script with qgis.core or r2py (R) or serve locally the GRASS layers with Django/GeoDjango.<br></div></div><br>The only disadvantages of the method is when I want to insert the results in GRASS GIS. <span id="result_box" class="" lang="en"><span class="hps">If</span> <span class="hps">I modify or create</span> <span class="hps">geometries</span><span>,</span> <span class="hps">the solutions</span> <span class="hps">I found</span> <span class="hps">were<br>
</span></span><ul><li><span id="result_box" class="" lang="en"><span class="hps">to use </span></span><span>GRASS temporary files as in <a href="http://osgeo-org.1560.n6.nabble.com/GRASS-and-the-Python-geospatial-modules-Shapely-PySAL-td4985075.html">GRASS and the Python geospatial modules (Shapely, PySAL,...) </a></span></li>
<li>to create a new shapefile and insert it into GRASS. </li></ul><br><span id="result_box" class="" lang="en"><span class="hps">We should not</span> <span class="hps">neglect these</span> <span class="hps">other</span> <span class="hps">Python modules.</span> <span class="hps">They can</span> <span class="hps">bring more to</span> <span class="hps">GRASS GIS, </span></span><span id="result_box" class="" lang="en"><span class="hps">especially</span> <span class="hps">for vector layers.</span></span><br>
<br><br><br><span> </span><br>