You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/tutorials/numpy_integration/grass_numpy_integration.qmd
+13-16Lines changed: 13 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -45,7 +45,6 @@ and [Get started with GRASS in Google Colab](../get_started/grass_gis_in_google_
45
45
46
46
First we will import GRASS packages:
47
47
48
-
49
48
```{python}
50
49
# import standard Python packages
51
50
import os
@@ -65,26 +64,22 @@ from grass.tools import Tools
65
64
Create a new GRASS project called "landlab". Since we will be working with artifical data, we don't need to
66
65
provide any coordinate reference system, resulting in a generic cartesian system.
67
66
68
-
69
67
```{python}
70
68
gs.create_project("landlab")
71
69
```
72
70
73
71
Initialize a session in this project and create a `Tools` object we will use for calling GRASS tools:
74
72
75
-
76
73
```{python}
77
74
session = gj.init("landlab")
78
75
tools = Tools()
79
76
```
80
77
81
-
82
78
Since we are generating artificial data, we need to specify the dimensions (number of rows and columns).
83
79
We also need to let GRASS know the actual coordinates; we will do that by setting
84
80
the [computational region](https://grass.osgeo.org/grass-stable/manuals/g.region.html). Lower-left (south-west) corner of the data will be at the coordinates (0, 0),
85
81
the coordinates of the upper-right (nort-east) corner are number of rows times cell resolution and number of columns times cell resolution.
86
82
87
-
88
83
```{python}
89
84
rows = 200
90
85
cols = 250
@@ -99,7 +94,6 @@ We will create a simple fractal surface with [r.surf.fractal](https://grass.osge
99
94
{{< fa wand-magic-sparkles >}} The trick is to use the `output` parameter with the value `np.array` to request a NumPy array instead of a native GRASS raster. {{< fa wand-magic-sparkles >}}
100
95
This way GRASS provides the array as the result of the call:
::: {.callout-note title="NumPy arrays in GRASS version < 8.5"}
110
104
111
105
Directly passing NumPy arrays to GRASS tools and receiving them back is a new feature in GRASS v8.5.
112
-
If you work with older versions of GRASS, you can use
106
+
If you work with older versions of GRASS, you can use
113
107
[grass.script.array](https://grass.osgeo.org/grass-stable/manuals/libpython/grass.script.html#script.array.array) and [grass.script.array3d](https://grass.osgeo.org/grass-stable/manuals/libpython/grass.script.html#script.array.array3d):
114
108
115
109
```{python}
@@ -124,7 +118,6 @@ fractal = ga.array("fractal")
124
118
125
119
Now we can display `fractal` array e.g., using matplotlib library:
Now, let's use Landlab's modeling capabilities to burn in an initial drainage network using the
@@ -196,25 +189,29 @@ plt.xlabel('X-coordinate')
196
189
plt.ylabel('Y-coordinate')
197
190
plt.show()
198
191
```
199
-
192
+
200
193

201
-
194
+
202
195
## From Landlab to GRASS
203
196
204
197
Now we will bring the eroded topography back to GRASS for additional hydrology modeling.
205
198
We will derive streams using the [r.watershed](https://grass.osgeo.org/grass-stable/manuals/r.watershed.html) and [r.stream.extract](https://grass.osgeo.org/grass-stable/manuals/r.stream.extract.html) tools.
206
199
207
200
{{< fa wand-magic-sparkles >}} The `Tools` API allows us to directly plugin the NumPy `elevation` array into the tool call. {{< fa wand-magic-sparkles >}}
0 commit comments