T1 - Shape subdivision¶
This tutorial shows how to use split shapes into Voronoi sub-shapes
In [1]:
Copied!
import time
import rastertoolkit
from rastertoolkit import shape_subdivide
from rastertoolkit.shape import plot_shapes
from pathlib import Path
print(f"rastertoolkit version v{rastertoolkit.__version__}")
import time
import rastertoolkit
from rastertoolkit import shape_subdivide
from rastertoolkit.shape import plot_shapes
from pathlib import Path
print(f"rastertoolkit version v{rastertoolkit.__version__}")
rastertoolkit version v0.3.4
RasterToolkit includes a function to subdivide a shept based on a target area. Here is how you call it:
In [2]:
Copied!
def subdivide_example(area: int = None, shape_file=Path("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,
verbose=False,
)
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=Path("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,
verbose=False,
)
print(f"Completed subdivision in {round(time.time() - start_time)}s")
return shape_file, new_shape_stem
Running this with the default (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)