[GRASS-dev] New wiki page summarising GRASS APIs

Pietro peter.zamb at gmail.com
Sat Jan 31 02:06:59 PST 2015


Hi Moritz,

On Fri, Jan 30, 2015 at 5:02 PM, Moritz Lennert
<mlennert at club.worldonline.be> wrote:
> In preparation of a talk at the geospatial devroom at the FOSSDEM this
> weekend, I've elaborated a wiki page on the current GRASS GIS APIs:
>
> http://grasswiki.osgeo.org/wiki/GRASS_GIS_APIs

Thank you for the work that you have done! Some brief notes:

Why os.system?

{{{
import os
os.system('g.region rast=elevation')
}}}

and not subprocess:

{{{
import subprocess
subprocess.call('g.region rast=elevation', shell=True)

# or

subprocess.Popen('g.region rast=elevation', shell=True)
}}}

Concerning the pygrass Module API, may be we can use the shortcut version:

{{{
>>> from grass.pygrass.modules.shortcuts import general as g
>>> gregion = g.region(flags='p')   # return a Module class instance
projection: 99 (Lambert Conformal Conic)
zone:       0
datum:      nad83
ellipsoid:  a=6378137 es=0.006694380022900787
north:      318500
south:      -16000
west:       124000
east:       963000
nsres:      500
ewres:      500
rows:       669
cols:       1678
cells:      1122582
>>> gregion
Module('g.region')
>>> gregion.name
'g.region'
>>> gregion.description
'Manages the boundary definitions for the geographic region.'
>>> gregion.flags.p = False  # change previous flag to False
>>> gregion.flags.g =True    # set the g flag to True
>>> gregion.run()
n=318500
s=-16000
w=124000
e=963000
nsres=500
ewres=500
rows=669
cols=1678
cells=1122582
Module('g.region')
>>> gregion.inputs.raster = 'elevation'
>>> gregion.run()
n=318500
s=-16000
w=124000
e=963000
nsres=500
ewres=500
rows=669
cols=1678
cells=1122582
}}}

In the Census example, we should add census.close() to close the
vector map after the use, or use the with statement:

{{{
>>> with VectorTopo('census', mode='r') as census:
...    print('numb. areas:', census.number_of('areas'))
...    for area in census.viter('areas'):
...        if area.area()>4000000:
...            print(area.id, area.area())
}}}

All the best

Pietro


More information about the grass-dev mailing list