Annotation¶

class Annotation(geometry=None, properties=None, wkb=None)[source]¶

An annotation: a geometry and associated properties.

geometry¶

The geometry of the annotation as a Shapely object.

Type:

Geometry

properties¶

The properties of the annotation.

Type:

dict

wkb¶

The WKB representation of the geometry.

Type:

bytes

Create a new annotation.

Must be initialized with either a Shapely geometry object or WKB bytes.

Parameters:
  • geometry (Geometry) – The geometry of the annotation.

  • properties (dict) – The properties of the annotation. Optional, defaults to {}.

  • wkb (bytes) – The WKB representation of a geometry. Optional.

Methods

decode_wkb

Decode WKB to a NumPy array of flat (alternating x, y) coordinates.

to_feature

Return a feature representation of this annotation.

to_geojson

Return a GeoJSON string representation of this annotation.

to_wkb

Returns the geometry as Well-Known Binary (WKB).

to_wkt

Returns the geometry as Well-Know Text (WKT).

Attributes

properties

coords

The annotation geometry as a flat array of 2D coordinates.

geometry

Return the shapely geometry of the annotation.

geometry_type

Return the geometry type of the annotation.

wkb

Return the WKB representation of the annotation.

property coords: np.array | list[np.array] | list[list[np.array]]¶

The annotation geometry as a flat array of 2D coordinates.

Returns a numpy array of coordinates for point and line string. For polygons, returns a list of numpy arrays, one for each ring. For multi-geometries, returns a list with one element for each geometry.

Returns:

The coordinates of the annotation geometry.

Return type:

np.array or list

static decode_wkb(wkb, geom_type)[source]¶

Decode WKB to a NumPy array of flat (alternating x, y) coordinates.

Polygons return a list of NumPy arrays (one array per ring) and multi-part geometries return a list with one element per child geometry e.g. a list of arrays for Multi-Line and a list of lists of arrays for multi-polyon.

Parameters:
  • wkb (bytes) – The WKB representation of a geometry.

  • geom_type (int) – The type of geometry to decode. Where 1 = point, 2 = line, 3 = polygon, 4 = multi-point, 5 = multi-line, 6 = multi-polygon.

Return type:

ndarray | list[ndarray] | list[list[ndarray]]

Examples

>>> from tiatoolbox.annotation.storage import AnnotationStore
>>> # Point(1, 2).wkb
>>> wkb = (
...     b"\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
...     b"\xf0?\x00\x00\x00\x00\x00\x00\x00@"
... )
>>> AnnotationStore.decode_wkb(wkb, 1)
array([0., 0.])
>>> from tiatoolbox.annotation.storage import AnnotationStore
>>> # Polygon([[0, 0], [1, 1], [1, 0]]).wkb
>>> wkb = (
...     b"\x01\x03\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00"
...     b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
...     b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00"
...     b"\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00"
...     b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
...     b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
... )
>>> AnnotationStore.decode_wkb(wkb, 3)
array([[0., 0.],
    [1., 1.],
    [1., 0.],
    [0., 0.]])
Raises:

ValueError – If the geometry type is not supported.

Returns:

An array of coordinates, a list of coordinates, or a list of lists of coordinates.

Return type:

np.ndarray or list

Parameters:
property geometry: Geometry¶

Return the shapely geometry of the annotation.

property geometry_type: GeometryType¶

Return the geometry type of the annotation.

to_feature()[source]¶

Return a feature representation of this annotation.

A feature representation is a Python dictionary with the same schema as a geoJSON feature.

Returns:

A feature representation of this annotation.

Return type:

dict

Parameters:

self (Annotation)

to_geojson()[source]¶

Return a GeoJSON string representation of this annotation.

Returns:

A GeoJSON representation of this annotation.

Return type:

str

Parameters:

self (Annotation)

to_wkb()[source]¶

Returns the geometry as Well-Known Binary (WKB).

Returns:

The annotation as a WKB geometry.

Return type:

Annotation

Parameters:

self (Annotation)

to_wkt()[source]¶

Returns the geometry as Well-Know Text (WKT).

Returns:

The annotation as a WKT geometry.

Return type:

Annotation

Parameters:

self (Annotation)

property wkb: bytes¶

Return the WKB representation of the annotation.