Layer#

class zeroheliumkit.src.anchors.Layer(name: str, polygons: Polygon | MultiPolygon = <MULTIPOLYGON EMPTY>, color: tuple = ('#ff3333', 1), enable_grid_snap: bool = True)[source]#

Bases: object

Methods

add(geom)

Adds a polygon or multipolygon to the layer.

add_text([text, size, loc])

Converts text into polygons and adds them to a layer.

buffer(offset, **kwargs)

Creates a new layer by buffering the current layer's polygons.

clear()

Clears all polygons from the layer.

copy()

Creates a deep copy of the Layer instance.

crop(geom[, loc])

Crops the layer with a polygon or multipolygon.

cut(geom[, loc])

Cuts the layer with a polygon or multipolygon.

mirror(aroundaxis[, keep_original])

Mirror all objects in layer around a specified axis.

modify_points(poly_id, ext_points[, ...])

Updates the point coordinates of a polygon in a layer.

move(dx, dy)

Move objects either by an (x, y) offset OR by snapping one anchor to another point.

plot([ax, size, show_idx, show_line_idx, ...])

Plots a layer polygons on the given axes.

remove([polygon_id])

Removes a polygon from a layer.

remove_holes([cut_position])

Removes any holes from a multipolygon in a layer by vertically cutting along the centroid of each hole and piecing together the remaining Polygon geometries.

rotate([angle, origin])

Rotates the layer by the given angle around the origin.

scale([xfact, yfact, origin])

Scales all objects in layer by the specified factors along the x and y axes.

simplify([tolerance])

Simplify polygons in a layer

slice(slice_line)

Slices polygons in a layer using a given line.

snap_to(point_from, point_to)

Snaps the layer from one point to another point by calculating the required offset.

Attributes

name

polygons

color

enable_grid_snap

area

Calculates the total area of all polygons in the layer.

is_empty

Checks if the layer is empty (i.e., has no polygons).

property is_empty: bool#

Checks if the layer is empty (i.e., has no polygons).

Returns:

True if the layer is empty, False otherwise.

Return type:

bool

property area: float#

Calculates the total area of all polygons in the layer.

Returns:

The total area of the polygons.

Return type:

float

copy() Layer[source]#

Creates a deep copy of the Layer instance.

Returns:

A new instance of Layer with the same polygons.

clear() Layer[source]#

Clears all polygons from the layer.

Returns:

Updated instance (self) of the class with no polygons.

rotate(angle: float = 0, origin=(0, 0)) Layer[source]#

Rotates the layer by the given angle around the origin.

Parameters:
  • angle (float, optional) – rotation angle. Defaults to 0.

  • origin (str, optional) – rotations are made around this point. Defaults to (0,0).

Returns:

Updated instance (self) of the class with all objects rotated.

move(dx: float | int, dy: float | int)[source]#

Move objects either by an (x, y) offset OR by snapping one anchor to another point.

Parameters:
  • dx (float | int) – The x offset to move by.

  • dy (float | int) – The y offset to move by.

Returns:

Updated instance with all polygons in layer moved.

Return type:

self

snap_to(point_from: tuple | Point, point_to: tuple | Point) Layer[source]#

Snaps the layer from one point to another point by calculating the required offset. :param point_from: The point to snap from. :type point_from: tuple | Point :param point_to: The point to snap to. :type point_to: tuple | Point

Returns:

Updated instance (self) of the class with all polygons moved.

scale(xfact=1.0, yfact=1.0, origin=(0, 0)) Layer[source]#

Scales all objects in layer by the specified factors along the x and y axes.

Parameters:
  • xfact (float, optional) – scale along x-axis. Defaults to 1.0.

  • yfact (float, optional) – scale along y-axis. Defaults to 1.0.

  • origin ((x,y), optional) – scale with respect to an origin (x,y). Defaults to (0,0).

Returns:

Updated instance (self) of the class with all polygons scaled.

mirror(aroundaxis: str, keep_original: bool = True) Layer[source]#

Mirror all objects in layer around a specified axis.

Parameters:
  • aroundaxis (str) – Defines the mirror axis. Only ‘x’ or ‘y’ are supported.

  • update_labels (bool, optional) – Whether to update the labels after mirroring. Defaults to False.

  • keep_original (bool, optional) – Whether to keep the original objects after mirroring. Defaults to False.

Raises:

TypeError – If the ‘aroundaxis’ is not ‘x’ or ‘y’.

Returns:

Updated instance (self) of the class with all objects mirrored.

add(geom: Polygon | MultiPolygon) Layer[source]#

Adds a polygon or multipolygon to the layer.

Parameters:

geom (Polygon | MultiPolygon) – The polygon or multipolygon to add.

Returns:

Updated instance (self) of the class with the added polygon.

cut(geom: Polygon | MultiPolygon, loc: tuple[float, float] = None) Layer[source]#

Cuts the layer with a polygon or multipolygon.

Parameters:
  • geom (Polygon | MultiPolygon) – The polygon to be cut.

  • loc (tuple[float, float], optional) – The location where the polygon will be cut. Defaults to None.

Returns:

Updated instance (self) of the class with the cut geometry.

crop(geom: Polygon | MultiPolygon, loc: tuple[float, float] = None) Layer[source]#

Crops the layer with a polygon or multipolygon.

Parameters:
  • geom (Polygon | MultiPolygon) – The polygon to be used for cropping.

  • loc (tuple[float, float], optional) – The location where the polygon will be applied. Defaults to None.

Returns:

Updated instance (self) of the class with the cropped geometry.

simplify(tolerance: float = 0.1) Layer[source]#

Simplify polygons in a layer

Parameters:

tolerance (float, optional) – The tolerance value for simplification. Defaults to 0.1.

Returns:

Updated instance (self) of the class with the specified layer simplified.

modify_points(poly_id: int, ext_points: dict, int_points: dict = None, int_idx: int = 0)[source]#

Updates the point coordinates of a polygon in a layer. Can optionally modify a single interior’s coordinates. Moves first and last points together.

Parameters:
  • layer (str) – layer name

  • poly_id (int) – polygon index in multipolygon list

  • ext_points (dict) – point indices to change in a polygon. Keys: corrresponds to the point idx in polygon exterior coord list. Value: tuple of new [x,y] coordinates

  • int_points (dict, optional) – point indices to change in a polygon. Keys: corrresponds to the point idx in polygon interiors coord list. Value: tuple of new [x,y] coordinates Defaults to None.

  • int_idx (int, optional) – interior index in the polygon’s interiors list. Defaults to 0.

remove_holes(cut_position: float = None)[source]#

Removes any holes from a multipolygon in a layer by vertically cutting along the centroid of each hole and piecing together the remaining Polygon geometries. Vertical cut is made with a default length of 1e6.

Parameters:

cut_position (float) – The x-coordinate where the vertical cut is made. If None, the cut is made at the centroid of each hole.

slice(slice_line: LineString | list[LineString])[source]#

Slices polygons in a layer using a given line.

Parameters:

slice_line (LineString) – The line used for slicing.

remove(polygon_id: int | tuple | list = None)[source]#

Removes a polygon from a layer.

Parameters:

polygon_id (int | tuple | list) – The index of the polygon to be removed.

add_text(text: str = 'abcdef', size: float = 1000, loc: tuple = (0, 0))[source]#

Converts text into polygons and adds them to a layer.

Parameters:
  • text (str, optional) – The text to be converted into polygons. Defaults to “abcdef”.

  • size (float, optional) – The size of the text. Defaults to 1000.

  • loc (tuple, optional) – The location where the text polygons will be placed. Defaults to (0,0).

buffer(offset: float, **kwargs) Layer[source]#

Creates a new layer by buffering the current layer’s polygons.

Parameters:
  • offset (float) – The distance to buffer the polygons.

  • **kwargs – Additional keyword arguments to be passed to the buffer method.

Returns:

A new Layer instance with the buffered polygons.

Return type:

Layer

plot(ax=None, size: tuple = (8, 8), show_idx: bool = False, show_line_idx: bool = False, show_grid: bool = False, add_points: bool = False, labels: bool = False, edgecolor: str = '#000000', **kwargs) Axes[source]#

Plots a layer polygons on the given axes.

Parameters:
  • ax (plt.Axes, optional) – The axes object on which to plot the layer. If None, a default axes object will be used.

  • show_idx (bool, optional) – Whether to show the index of the layer object.

  • show_line_idx (bool, optional) – Whether to show the line index of the layer object.

  • add_points (bool, optional) – Whether to add points to the layer object.

  • edgecolor (str, optional) – The edge color of the layer object.

  • **kwargs – Additional keyword arguments to be passed to the plotting functions.