Skip to content

Commit 6edcbeb

Browse files
authored
Merge branch 'OSGeo:main' into main
2 parents 0b35542 + 1fe9918 commit 6edcbeb

File tree

5 files changed

+220
-116
lines changed

5 files changed

+220
-116
lines changed

doc/interfaces_overview.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ r.slope.aspect elevation=elevation slope=slope aspect=aspect
3535

3636
## Python
3737

38-
GRASS Python interface provides libraries to create GRASS scripts and access
39-
the internal data structures of GRASS. The Python interface consists of
40-
two main libraries:
41-
*[grass.script](https://grass.osgeo.org/grass-stable/manuals/libpython/script_intro.html)*
42-
provides a Python interface to GRASS tools
38+
GRASS Python interface provides libraries to use GRASS tools, create scripts,
39+
and access the GRASS data structures. The Python interface consists of
40+
three main libraries:
41+
*[grass.tools](https://grass.osgeo.org/grass-stable/manuals/libpython/tools_index.html)*
42+
provides a Python interface to GRASS tools,
43+
*[grass.script](https://grass.osgeo.org/grass-stable/manuals/libpython/grass.script_intro.html)*
44+
handles GRASS projects and sessions in Python,
4345
and *[grass.pygrass](https://grass.osgeo.org/grass-stable/manuals/libpython/pygrass_index.html)*
44-
enables access to the internal data structures of GRASS.
46+
enables a fine-grained access to the GRASS data structures.
4547

4648
To get started with scripting, create a new project with `gs.create_project` and
4749
start a GRASS session with the `gs.setup.init` function to initialize the
@@ -57,6 +59,7 @@ sys.path.append(
5759
)
5860

5961
import grass.script as gs
62+
from grass.tools import Tools
6063

6164
# Create a new project
6265
gs.create_project(path="path/to/my_project", epsg="3358")
@@ -65,9 +68,10 @@ gs.create_project(path="path/to/my_project", epsg="3358")
6568
with gs.setup.init("path/to/my_project") as session:
6669

6770
# Run GRASS tools
68-
gs.run_command("r.import", input="/path/to/elevation.tif", output="elevation")
69-
gs.run_command("g.region", raster="elevation")
70-
gs.run_command("r.slope.aspect", elevation="elevation", slope="slope")
71+
tools = Tools(session=session)
72+
tools.r_import_(input="/path/to/elevation.tif", output="elevation")
73+
tools.g_region(raster="elevation")
74+
tools.r_slope_aspect(elevation="elevation", slope="slope")
7175
```
7276

7377
[Learn more :material-arrow-right-bold:](python_intro.md){ .md-button }

doc/jupyter_intro.md

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ features you can refer to the [Cartography](topic_cartography.md) topic page.
8585
For example, let's add a legend, barscale, and shaded relief to the map:
8686

8787
```python
88+
from grass.tools import Tools
89+
90+
# Create an object to access tools
91+
tools = Tools()
8892

8993
# Compute shaded relief
90-
gs.run_command("r.relief", input="elevation", output="relief")
94+
tools.r_relief(input="elevation", output="relief")
9195

9296
# Create a new map
9397
m = gj.Map()
@@ -176,10 +180,10 @@ maps and play a continuous loop.
176180
# Create a series of relief maps with different angles
177181
directions = [0, 90, 180, 270]
178182
for azimuth in directions:
179-
gs.run_command("r.relief",
180-
input="elevation",
181-
output=f"relief_{azimuth}",
182-
azimuth=azimuth)
183+
tools.r_relief(
184+
input="elevation",
185+
output=f"relief_{azimuth}",
186+
azimuth=azimuth)
183187
m = gj.SeriesMap()
184188
m.add_rasters(f"relief_{azimuth}" for azimuth in directions)
185189
m.d_vect(map="roads")
@@ -199,30 +203,30 @@ series map of overland water flow:
199203

200204
```python
201205
# Zoom in to the study area
202-
gs.run_command("g.region", n=226040, s=223780, e=639170, w=636190)
206+
tools.g_region(n=226040, s=223780, e=639170, w=636190)
203207
# Compute topography dx and dy derivatives
204-
gs.run_command("r.slope.aspect",
205-
elevation="elevation",
206-
dx="dx",
207-
dy="dy")
208+
tools.r_slope_aspect(elevation="elevation", dx="dx", dy="dy")
208209
# Compute overland flow
209-
gs.run_command("r.sim.water",
210-
flags="t",
211-
elevation="elevation",
212-
dx="dx",
213-
dy="dy",
214-
depth="depth",
215-
niterations=30)
210+
tools.r_sim_water(
211+
flags="t",
212+
elevation="elevation",
213+
dx="dx",
214+
dy="dy",
215+
depth="depth",
216+
niterations=30)
216217

217218
# Create a time series
218-
gs.run_command("t.create",
219-
output="depth",
220-
temporaltype="relative",
221-
title="Overland flow depth",
222-
description="Overland flow depth")
219+
tools.t_create(
220+
output="depth",
221+
temporaltype="relative",
222+
title="Overland flow depth",
223+
description="Overland flow depth")
223224
# Register the time series
224-
maps = gs.list_strings(type="raster", pattern="depth*")
225-
gs.run_command("t.register", input="depth", maps=maps)
225+
maps = [
226+
item["fullname"]
227+
for item in tools.g_list(type="raster", pattern="depth*", format="json")
228+
]
229+
tools.t_register(input="depth", maps=maps)
226230

227231
# Create a time series map
228232
flow_map = gj.TimeSeriesMap()
@@ -253,6 +257,10 @@ For complete documentation on the `grass.jupyter` package, see the
253257
[grass.jupyter](https://grass.osgeo.org/grass-stable/manuals/libpython/grass.jupyter.html)
254258
library documentation page.
255259

260+
For complete documentation on the `grass.tools` package, see the
261+
[grass.tools](https://grass.osgeo.org/grass-stable/manuals/libpython/grass.tools.html)
262+
library documentation page.
263+
256264
For complete documentation on the `grass.script` package, see the
257265
[grass.script](https://grass.osgeo.org/grass-stable/manuals/libpython/script_intro.html)
258266
library documentation page.

0 commit comments

Comments
 (0)