>>> from env_helper import info; info()
页面更新时间: 2022-05-06 20:40:54
运行环境:
Linux发行版本: Debian GNU/Linux 11 (bullseye)
操作系统内核: Linux-5.10.0-14-amd64-x86_64-with-glibc2.31
Python版本: 3.9.2
13.2. 使用 Python 调用 GRASS 模块¶
>>> import os
>>> import sys
>>> import subprocess
>>> from IPython.display import Image
gisbase 是系统中安装 GRASS 的目录
>>> gisbase = subprocess.check_output(['grass','--config','path']).decode().strip()
>>>
>>> os.environ['GISBASE'] = gisbase
>>> sys.path.append(os.path.join(gisbase,'etc','python'))
>>>
>>> import grass.script as gs
>>> import grass.script.setup as gsetup
>>> help(gsetup.init)
Help on function init in module grass.script.setup: init(path, location=None, mapset=None, grass_path=None) Initialize system variables to run GRASS modules This function is for running GRASS GIS without starting it with the standard main executable grass. No GRASS modules shall be called before call of this function but any module or user script can be called afterwards because a GRASS session has been set up. GRASS Python libraries are usable as well in general but the ones using C libraries throughctypes
are not (which is caused by library path not being updated for the current process which is a common operating system limitation). When the path or specified mapset does not exist, ValueError is raised. Theget_install_path()
function is used to determine where the rest of GRASS files is installed. The grass_path parameter is passed to it if provided. If the path cannot be determined, ValueError is raised. Exceptions from the underlying function are propagated. To create a GRASS session a session file (aka gisrc file) is created. Caller is responsible for deleting the file which is normally done with the functionfinish()
. Basic usage:: # ... setup GISBASE and sys.path before import import grass.script as gs gs.setup.init( "~/grassdata/nc_spm_08/user1", grass_path="/usr/lib/grass", ) # ... use GRASS modules here # end the session gs.setup.finish() The returned object is a context manager, so thewith
statement can be used to ensure that the session is finished (closed) at the end:: # ... setup sys.path before import import grass.script as gs with gs.setup.init("~/grassdata/nc_spm_08/user1") # ... use GRASS modules here :param path: path to GRASS database :param location: location name :param mapset: mapset within given location (default: 'PERMANENT') :param grass_path: path to GRASS installation or executable :returns: reference to a session handle object which is a context manager
设置各个配置参数:
>>> gisdbase = '/home/bk/grassws'
>>> location = '/home/bk/grassws/nc_spm_08_grass7'
>>> mapset = "user1"
>>>
>>> # os.environ['GISBASE'] = gisbase
>>>
>>> rcfile = gsetup.init(gisdbase,
>>> location = 'nc_spm_08_grass7',
>>> mapset = mapset)
>>>
>>> os.environ['GRASS_FONT']='sans'
>>> os.environ['GRASS_OVERWRITE']='1'
>>> gs.set_raise_on_error(True)
>>> gs.set_capture_stderr(True)
>>> os.environ['GRASS_RENDER_IMMEDIATE']='cairo'
>>> os.environ['GRASS_RENDER_FILE_READ']='TRUE'
>>> os.environ['GRASS_LEGEND_FILE']='legend.txt'
>>> gs.gisenv()['MAPSET']
'user1'
>>> gs.parse_command('g.gisenv')
{'GISDBASE': "'/home/bk/grassws';",
'LOCATION_NAME': "'nc_spm_08_grass7';",
'MAPSET': "'user1';"}
>>> # gs.gisenv()['GISDBASE'] = '/home/bk/grassws';
>>> !g.gisenv
GISDBASE=/home/bk/grassws
LOCATION_NAME=nc_spm_08_grass7
MAPSET=user1
>>> gs.parse_command('g.region', region="swwake_30m", flags='pg')
{'projection': '99',
'zone': '0',
'n': '228500',
's': '215000',
'w': '630000',
'e': '645000',
'nsres': '30',
'ewres': '30',
'rows': '450',
'cols': '500',
'cells': '225000'}
>>> gs.run_command('d.rast', map="elevation")
>>> Image(filename="map.png")

>>> gs.run_command('d.shade', shade="elevation_shade", color="elevation", brighten="30")
>>> Image(filename="map.png")

>>> gs.parse_command('r.info', map="elevation", flags='r')
{'min': '55.57879', 'max': '156.3299'}
>>> gs.run_command('d.vect', map="streets_wake")
>>> Image(filename="map.png")
