Skip to content

Integrate circlecrop to vpype #780

@abey79

Description

@abey79

Used to live in https://github.com/abey79/vpype-explorations, should be integrated to vpype using this corrected version:

import click
import vpype as vp
import vpype_cli
from shapely.geometry import Polygon, LineString, MultiLineString, GeometryCollection, MultiPoint


@click.command()
@click.argument("X", type=vpype_cli.LengthType())
@click.argument("Y", type=vpype_cli.LengthType())
@click.argument("R", type=vpype_cli.LengthType())
@click.option(
    "-q",
    "--quantization",
    type=vpype_cli.LengthType(),
    default="0.1mm",
    help="Quantization used for the circular area",
)
@vpype_cli.layer_processor
def circlecrop(lines: vp.LineCollection, x: float, y: float, r: float, quantization: float):
    """Crop to a circular area."""

    # Create a circular polygon
    circle = Polygon(vp.as_vector(vp.circle(x, y, r, quantization)))

    # Convert LineCollection to MultiLineString
    mls = lines.as_mls()

    # Perform the intersection
    intersection = mls.intersection(circle)

    # Handle GeometryCollection and MultiPoint specifically
    if isinstance(intersection, (GeometryCollection, MultiPoint)):
        # Extract only LineString geometries
        line_strings = [geom for geom in intersection.geoms if isinstance(geom, LineString)]
        return vp.LineCollection(line_strings)

    # For all other types, pass directly to LineCollection
    return vp.LineCollection(intersection)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions