[GRASS-SVN] r64020 - in grass/branches/releasebranch_7_0: . lib/python/docs/src
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jan 8 14:50:24 PST 2015
Author: neteler
Date: 2015-01-08 14:50:24 -0800 (Thu, 08 Jan 2015)
New Revision: 64020
Added:
grass/branches/releasebranch_7_0/lib/python/docs/src/script_intro.rst
Modified:
grass/branches/releasebranch_7_0/
grass/branches/releasebranch_7_0/lib/python/docs/src/index.rst
Log:
pythonlib: add basic intro for grass.script (trunk, r63369)
Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Modified: svn:mergeinfo
- /grass/trunk:60497,60563-60566,60817,60976,61046,61096,61141,61994,62085,62105,62179-62180,62182,62190,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62691,62705,62709,62723,62730,62739,62741,62743,62746,62750-62752,62757,62762,62785,62798,62800-62801,62803,62805,62812,62822,62824,62828,62831,62838,62845,62847,62850,62856,62879,62881,62886,62904,62907-62908,62910,62912,62914,62916,62918,62920,62925,62932-62933,62935,62940,62942,62944-62946,62949,62955-62956,62958,62960,62962,62964,62966-62968,62970,62973,62975,62977,62981,62983,62985,62987,62989,62991,62993,62995,62997,62999-63000,63003,63005,63007,63009,63011,63013,63015,63017,63020,63022,63024,63026,63028-63031,63033,63035,63037,63040,63043-63044,63047,63049,63051,63053,63055,63057,63060,63062-
63064,63066,63068,63070-63071,63074,63076,63079,63081,63083,63085,63087,63089,63091,63093,63095,63098,63100,63102,63105,63107,63109,63111,63113-63114,63116,63119,63121,63123,63125,63130,63132-63133,63135,63137,63140,63143,63145,63147,63149,63151,63153-63154,63157,63160,63165,63170,63173,63175,63187,63192-63193,63196,63199-63200,63202,63209,63216,63220-63221,63224,63227,63240,63246,63250,63255,63259,63261,63275-63276,63279,63281,63283,63287,63290,63292,63297,63302,63304-63307,63315,63319,63327,63330,63332,63339,63342,63345,63361-63363,63367,63379,63389,63391,63393,63402,63408-63409,63416-63417,63425,63427,63429,63431,63433,63448,63451,63453,63457,63459,63464-63470,63473,63482,63497,63505,63508,63510,63515,63521-63524,63526,63536-63537,63551-63552,63554,63556,63558-63559,63562,63570,63576,63582,63589-63593,63602,63604,63608-63609,63616,63618,63624,63626,63628,63630,63634,63639,63642,63644,63650,63652,63656,63679,63685,63689,63691,63693,63695,63698,63700-63702,63704,63706-63707
,63709,63711,63713,63715,63717-63718,63721-63727,63731,63733,63736,63740-63742,63744-63749,63751-63752,63756,63758,63760-63761,63767,63777,63780,63783,63787,63793,63796-63798,63801,63803,63807-63808,63812,63814,63816,63821,63823-63824,63827-63828,63830-63831,63834-63835,63837-63839,63841,63843-63844,63846-63847,63849,63851,63853,63855-63856,63858-63859,63862,63865,63869,63871,63882,63884,63889-63890,63892-63895,63897-63898,63900,63902-63903,63905,63907,63909-63911,63913-63914,63927,63930-63931,63940-63941,63945,63954-63955,63960,63965,63967,63976-63983,63995
+ /grass/trunk:60497,60563-60566,60817,60976,61046,61096,61141,61994,62085,62105,62179-62180,62182,62190,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62691,62705,62709,62723,62730,62739,62741,62743,62746,62750-62752,62757,62762,62785,62798,62800-62801,62803,62805,62812,62822,62824,62828,62831,62838,62845,62847,62850,62856,62879,62881,62886,62904,62907-62908,62910,62912,62914,62916,62918,62920,62925,62932-62933,62935,62940,62942,62944-62946,62949,62955-62956,62958,62960,62962,62964,62966-62968,62970,62973,62975,62977,62981,62983,62985,62987,62989,62991,62993,62995,62997,62999-63000,63003,63005,63007,63009,63011,63013,63015,63017,63020,63022,63024,63026,63028-63031,63033,63035,63037,63040,63043-63044,63047,63049,63051,63053,63055,63057,63060,63062-
63064,63066,63068,63070-63071,63074,63076,63079,63081,63083,63085,63087,63089,63091,63093,63095,63098,63100,63102,63105,63107,63109,63111,63113-63114,63116,63119,63121,63123,63125,63130,63132-63133,63135,63137,63140,63143,63145,63147,63149,63151,63153-63154,63157,63160,63165,63170,63173,63175,63187,63192-63193,63196,63199-63200,63202,63209,63216,63220-63221,63224,63227,63240,63246,63250,63255,63259,63261,63275-63276,63279,63281,63283,63287,63290,63292,63297,63302,63304-63307,63315,63319,63327,63330,63332,63339,63342,63345,63361-63363,63367,63369,63379,63389,63391,63393,63402,63408-63409,63416-63417,63425,63427,63429,63431,63433,63448,63451,63453,63457,63459,63464-63470,63473,63482,63497,63505,63508,63510,63515,63521-63524,63526,63536-63537,63551-63552,63554,63556,63558-63559,63562,63570,63576,63582,63589-63593,63602,63604,63608-63609,63616,63618,63624,63626,63628,63630,63634,63639,63642,63644,63650,63652,63656,63679,63685,63689,63691,63693,63695,63698,63700-63702,63704,63706
-63707,63709,63711,63713,63715,63717-63718,63721-63727,63731,63733,63736,63740-63742,63744-63749,63751-63752,63756,63758,63760-63761,63767,63777,63780,63783,63787,63793,63796-63798,63801,63803,63807-63808,63812,63814,63816,63821,63823-63824,63827-63828,63830-63831,63834-63835,63837-63839,63841,63843-63844,63846-63847,63849,63851,63853,63855-63856,63858-63859,63862,63865,63869,63871,63882,63884,63889-63890,63892-63895,63897-63898,63900,63902-63903,63905,63907,63909-63911,63913-63914,63927,63930-63931,63940-63941,63945,63954-63955,63960,63965,63967,63976-63983,63995
Modified: grass/branches/releasebranch_7_0/lib/python/docs/src/index.rst
===================================================================
--- grass/branches/releasebranch_7_0/lib/python/docs/src/index.rst 2015-01-08 22:48:51 UTC (rev 64019)
+++ grass/branches/releasebranch_7_0/lib/python/docs/src/index.rst 2015-01-08 22:50:24 UTC (rev 64020)
@@ -28,7 +28,7 @@
.. toctree::
:maxdepth: 3
- script
+ script_intro
pygrass_index
temporal_framework
exceptions
Copied: grass/branches/releasebranch_7_0/lib/python/docs/src/script_intro.rst (from rev 63369, grass/trunk/lib/python/docs/src/script_intro.rst)
===================================================================
--- grass/branches/releasebranch_7_0/lib/python/docs/src/script_intro.rst (rev 0)
+++ grass/branches/releasebranch_7_0/lib/python/docs/src/script_intro.rst 2015-01-08 22:50:24 UTC (rev 64020)
@@ -0,0 +1,138 @@
+GRASS GIS Python scripting with script package
+==============================================
+
+Parts of the API
+----------------
+
+.. toctree::
+ :maxdepth: 2
+
+ script
+
+
+Syntax
+------
+
+First, try a standard command in Command console in Layer Manager::
+
+ r.info map=elevation -g
+
+We are running *r.info* with an option ``map`` set to ``elevation`` Now,
+switch to Python shell and type the same command but in Python syntax::
+
+ grass.read_command('r.info', map='elevation', flags='g')
+
+We used function ``read_command()`` from the ``grass.script`` package
+which is imported under the name ``grass`` in the Python shell in GRASS
+GUI. There are also other functions besides ``read_command()`` most
+notably ``run_command()``, ``write_command()`` and ``parse_command()``.
+The first parameter for functions from this group is the name of the
+GRASS module as string. Other parameters are options of the module.
+Python keyword arguments syntax is used for the options. Flags can be
+passed in a parameter ``flags`` where value of the parameter is a string
+containing all the flags we want to set. The general syntax is the
+following::
+
+ function_name('module.name', option1=value1, option2=..., flags='flagletters')
+
+The function parameters are the same as module options, so you can just
+use standard module manual page to learn about the interface.
+
+Most of the GRASS functionality is available through modules and all of
+them can be called using the functions above. However, in some cases, it
+is more advantageous to use specialized Python functions. This is the
+case for ``mapcalc()`` function (wrapper for *r.mapcalc* module) and
+``list_strings()`` function (wrapper for *g.list* module).
+
+
+Combining multiple modules
+--------------------------
+
+To launch a Python script from GUI, use File -> Launch Python script.
+
+::
+
+ import grass.script as gscript
+
+ def main():
+ input_raster = 'elevation'
+ output_raster = 'high_areas'
+ stats = gscript.parse_command('r.univar', map='elevation', flags='g')
+ raster_mean = float(stats['mean'])
+ raster_stddev = float(stats['stddev'])
+ raster_high = raster_mean + raster_stddev
+ gscript.mapcalc('{r} = {a} > {m}'.format(r=output_raster, a=input_raster,
+ m=raster_high))
+
+ if __name__ == "__main__":
+ main()
+
+
+Processing many maps
+--------------------
+
+::
+
+ import grass.script as gscript
+
+ def main():
+ rasters = ['lsat7_2002_10', 'lsat7_2002_20', 'lsat7_2002_30', 'lsat7_2002_40']
+ max_min = None
+ for raster in rasters:
+ stats = gscript.parse_command('r.univar', map=raster, flags='g')
+ if max_min is None or max_min < stats['min']:
+ max_min = stats['min']
+ print max_min
+
+ if __name__ == "__main__":
+ main()
+
+Providing GRASS module interface to a script
+---------------------------------------------
+
+
+::
+
+ #!/usr/bin/env python
+
+ #%module
+ #% description: Adds the values of two rasters (A + B)
+ #% keywords: raster
+ #% keywords: algebra
+ #% keywords: sum
+ #%end
+ #%option G_OPT_R_INPUT
+ #% key: araster
+ #% description: Name of input raster A in an expression A + B
+ #%end
+ #%option G_OPT_R_INPUT
+ #% key: braster
+ #% description: Name of input raster B in an expression A + B
+ #%end
+ #%option G_OPT_R_OUTPUT
+ #%end
+
+
+ import sys
+
+ import grass.script as gscript
+
+
+ def main():
+ options, flags = gscript.parser()
+ araster = options['araster']
+ braster = options['braster']
+ output = options['output']
+
+ gscript.mapcalc('{r} = {a} + {b}'.format(r=output, a=araster, b=braster))
+
+ return 0
+
+
+ if __name__ == "__main__":
+ sys.exit(main())
+
+Note that the previous code samples were missing some whitespace which
+Python PEP8 style guide requires but this last sample fulfills all the
+requirements. You should always use *pep8* tool to check your syntax and
+style or set your editor to do it for you.
More information about the grass-commit
mailing list