|
| 1 | +# --8<-- [start:init] |
| 2 | +import numpy as np |
| 3 | +from rich import print |
| 4 | + |
| 5 | +from siapy.entities import Shape, SpectralImage |
| 6 | +from siapy.entities.shapes import GeometricShapes |
| 7 | + |
| 8 | +# Create a mock spectral image |
| 9 | +rng = np.random.default_rng(seed=42) |
| 10 | +array = rng.random((100, 100, 10)) # height, width, bands |
| 11 | +image = SpectralImage.from_numpy(array) |
| 12 | + |
| 13 | +# SpectralImage automatically initializes GeometricShapes |
| 14 | +assert isinstance(image.geometric_shapes, GeometricShapes) |
| 15 | +# You can access the underlying list via the shapes property |
| 16 | +assert isinstance(image.geometric_shapes.shapes, list) |
| 17 | + |
| 18 | +# GeometricShapes implements common list operations directly, i.e. number of shapes: |
| 19 | +length_via_geometric_shapes = len(image.geometric_shapes) |
| 20 | +length_via_raw_list = len(image.geometric_shapes.shapes) |
| 21 | +# --8<-- [end:init] |
| 22 | + |
| 23 | +# --8<-- [start:operations] |
| 24 | +# Create two Shape objects with distinct spatial coordinates and semantic labels |
| 25 | +coords1 = [(1, 2), (3, 4), (2, 4)] |
| 26 | +shape1 = Shape.from_multipoint(coords1, label="coords1") |
| 27 | +coords2 = [(19, 20), (21, 22), (20, 22)] |
| 28 | +shape2 = Shape.from_multipoint(coords2, label="coords2") |
| 29 | + |
| 30 | +# Add multiple shapes to the SpectralImage's geometric_shapes container at once |
| 31 | +image.geometric_shapes.extend([shape1, shape2]) |
| 32 | + |
| 33 | +# Display the current collection of shapes stored in the image |
| 34 | +# Each shape will be shown with its type and label |
| 35 | +print(f"Shapes in GeometricShapes: {image.geometric_shapes}") |
| 36 | + |
| 37 | +# --8<-- [end:operations] |
0 commit comments