Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate circlecrop to vpype #780

Open
abey79 opened this issue Nov 24, 2024 · 0 comments
Open

Integrate circlecrop to vpype #780

abey79 opened this issue Nov 24, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@abey79
Copy link
Owner

abey79 commented Nov 24, 2024

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)
@abey79 abey79 added the enhancement New feature or request label Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant