Skip to content

Quickstart: Python

Numpy arrays in, numpy arrays out. The extension wraps the same C library the benchmarks measure.

The Python package ships as a binary wheel in your evaluation or licence bundle (Linux x86-64/aarch64 and macOS; other platforms on request):

Terminal window
pip install ./isocurves-*.whl
import numpy as np
import isocurves
# Any 2D scalar field — here, real terrain.
z = np.load("srtm_alps_1024.npy")
result = isocurves.contour(
z,
levels=np.arange(400, 4500, 100), # contour interval: 100 m
nthreads=0, # 0 = auto-detect cores
)
print(result) # ContourResult: n curves over m levels
for curve in result:
curve.x, curve.y # ordered float32 vertex arrays
curve.level_value # e.g. 1500.0
curve.is_closed # loop vs. exits the grid
curve.is_hill # higher values inside?
# All curves at one level (e.g. for styling major contours):
major = result.by_level(5)

The curves are ordered polylines, so plotting is a plain plot call — no ContourSet machinery:

import matplotlib.pyplot as plt
for c in result:
plt.plot(c.x, c.y, lw=0.5, color="k")
plt.gca().set_aspect("equal")
plt.show()

Re-extraction is fast enough to run on every slider callback — see the scientific Python use case for measured per-update times against matplotlib and ContourPy. Physical coordinates go in via x= and y=. closed=True closes every contour along the raster edge into a ring and adds the nesting tree (parent, owner), giving result.polygons() (polygons with holes per level), result.bands() (filled contours, OGC-valid) and result.to_geojson(); isocurves.filled(z, levels) returns the filled contours in one call. dtype=np.float64 keeps georeferenced coordinates (e.g. UTM northings) exact; the default is float32. NaN (or a masked array) marks NoData: contours end half-way to the void, and with closed=True voids become holes.

isocurves.contour_stream() reads a grid in blocks of rows (a numpy memmap, an h5py or zarr dataset, or any sequence of row blocks) and yields each contour as soon as it is complete, so the grid is never held whole:

z = np.load("national_dem.npy", mmap_mode="r") # on disk, not in RAM
for curve in isocurves.contour_stream(z, levels=np.arange(0, 4000, 20)):
write(curve) # completion order

isocurves.ContourStream(shape, levels, …) is the same engine driven by hand: feed(rows) returns the curves those rows completed, finish() the rest. With filled=True (or closed=True, emit=False), finish() returns the filled bands (or rings with nesting) of the whole grid. The output equals contour() / filled() on the whole grid. transform= takes an affine (GDAL geotransform order) for rotated rasters.