T1 - Shape subdivision¶
This tutorial shows how to use split shapes into Voronoi sub-shapes
In [1]:
Copied!
import rastertoolkit
from importlib.metadata import version
from rastertoolkit import shape_subdivide
from rastertoolkit.shape import plot_shapes
import time
print(f"rastertoolkit version v{version("rastertoolkit")}\n")
import rastertoolkit
from importlib.metadata import version
from rastertoolkit import shape_subdivide
from rastertoolkit.shape import plot_shapes
import time
print(f"rastertoolkit version v{version("rastertoolkit")}\n")
rastertoolkit version v0.5.3
RasterToolkit includes a function to subdivide a shape based on a target area. Here is how you call it:
In [2]:
Copied!
def subdivide_example(area: int = None, shape_file: str = "data/COD_LEV02_ZONES"):
start_time = time.time()
print(f"Starting {area or 'default'} subdivision...")
new_shape_stem = shape_subdivide(shape_stem=shape_file, out_dir="results", box_target_area_km2=area)
print(f"Completed subdivision in {round(time.time() - start_time)}s")
return shape_file, new_shape_stem
def subdivide_example(area: int = None, shape_file: str = "data/COD_LEV02_ZONES"):
start_time = time.time()
print(f"Starting {area or 'default'} subdivision...")
new_shape_stem = shape_subdivide(shape_stem=shape_file, out_dir="results", box_target_area_km2=area)
print(f"Completed subdivision in {round(time.time() - start_time)}s")
return shape_file, new_shape_stem
Running this with a target patch area of 900 square km (default is 100 square km):
In [3]:
Copied!
shape_file, new_shape_stem = subdivide_example(900) # default is 100 km2
shape_file, new_shape_stem = subdivide_example(900) # default is 100 km2
Starting 900 subdivision...
Completed subdivision in 16s
RasterToolkit also includes a function you can use to plot shapefiles:
In [4]:
Copied!
fig, ax = plot_shapes(shape_file, alpha=1.0, edgecolor="k", facecolor="gray")
plot_shapes(new_shape_stem, ax=ax, alpha=1.0, linewidth=0.2, edgecolor="k", facecolor="None")
ax.set_aspect(1)
fig, ax = plot_shapes(shape_file, alpha=1.0, edgecolor="k", facecolor="gray")
plot_shapes(new_shape_stem, ax=ax, alpha=1.0, linewidth=0.2, edgecolor="k", facecolor="None")
ax.set_aspect(1)
In [ ]:
Copied!