Franklin, J.F., Forman, R.T.T. Creating landscape patterns by forest cutting: Ecological consequences and principles. Landscape Ecology 1, 5–18 (1987). https://doi.org/10.1007/BF02275261
Setup
# Import modules
import sys
import subprocess
from pathlib import Path
# Find GRASS Python packages
sys.path.append(
subprocess.check_output(
["grass", "--config", "python_path"],
text=True
).strip()
)
# Import GRASS packages
import grass.script as gs
import grass.jupyter as gj
from grass.tools import Tools
# Create GRASS project
gs.create_project("forestry", overwrite=True)
gj.init("forestry")
tools = Tools(overwrite=True)
# Print projection
projection = tools.g_proj(format="shell", flags="p").keyval
print(projection.get("name"))
Set region
# Set region
tools.g_region(n=100, s=0, e=100, w=0, res=1)
Create grid
v.mkgrid
# Generate grid
tools.v_mkgrid(map="grid", grid=(10,10), type="area")
# Visualize
m = gj.Map(width=500, use_region=True)
m.d_vect(map="grid", color="black", fill="none", width=2)
m.show()
Model cuts
Model forest cuts with the vector digitizer g.gui.vdigit.
# Digitize
tools.g_gui_vdigit(map="grid")
# Visualize
m = gj.Map(width=500)
m.d_vect(map="grid", fill=(28, 95, 44))
m.show()
Vectorize
# Vectorize
tools.v_to_rast(input="grid", output="landcover", use="value", value=42)
# Model landcover
tools.r_mapcalc(expression="landcover = if(isnull(landcover), 31, 42)")
# Set colors
tools.r_colors(map="landcover", color="nlcd")
# Visualize
m = gj.Map(width=500, use_region=True)
m.d_rast(map="landcover")
m.show()
Forest fragmentation
Calculate forest fragmentation with r.forestfrag.
# Install extension
tools.g_extension(extension="r.forestfrag")
# Extract forest
tools.r_mapcalc(expression="forest = if(landcover == 42, 1, 0)")
# Calculate forest fragmentation
fragmentation = tools.r_forestfrag(
input="forest",
output="fragmentation",
size=5,
flags="s"
)
# Visualize
m = gj.Map(width=500)
m.d_rast(map="fragmentation")
m.d_legend(raster="fragmentation", color="white", at=(50, 95, 1, 3))
m.show()
# Print report
print(fragmentation.text)
+------------------------------------------------+
| RASTER MAP CATEGORY REPORT |
|PROJECT: forestry Thu Nov 19 20:46:51 2026|
|------------------------------------------------|
| north: 100 east: 100 |
|REGION south: 0 west: 0 |
| res: 1 res: 1 |
|------------------------------------------------|
|MASK: none |
|------------------------------------------------|
|MAP: Forest fragmentation (fragmentation in PERM|
|------------------------------------------------|
| Category Information | | % |
|#|description | hectares| cover|
|------------------------------------------------|
|0|exterior . . . . . . . . . .| 0.160000| 16.00|
|3|edge . . . . . . . . . . . .| 0.147200| 14.72|
|4|perforated . . . . . . . . .| 0.006400| 0.64|
|5|interior . . . . . . . . . .| 0.686400| 68.64|
|------------------------------------------------|
|TOTAL | 1.000000|100.00|
+------------------------------------------------+