मुख्य सामग्री पर जाएँ

raygeo.svg

SVG path data parsed into geometries

SVG path data parsed into geometries SVG parsing and geometry extraction.

Extracts Geometry objects from SVG documents — either as a flat list or grouped by layer or by color — and provides parsers for path data and transforms, length handling, metadata extraction and path export.

Functions​

filter_svg_by_color()​

filter_svg_by_color(
svg_str: str,
color_key: str,
color_attr: svg.color.ColorAttr = raygeo.svg.color.ColorAttr.ANY,
) -> str

Return a copy of the SVG containing only shapes of one color.

Non-matching shapes are removed, preserving the rest of the document (groups, defs, namespaces) verbatim. Useful for rendering a color layer's base image.

ParameterTypeDescription
svg_strstrSVG document as a string.
color_keystrColor bucket key to keep (e.g. '#ff0000' or '_no_color').
color_attrsvg.color.ColorAttr = raygeo.svg.color.ColorAttr.ANYColor attribute to bucket by.
ReturnsstrThe filtered SVG document as a string.
ComplexityO(n) where n = size of SVG document

geometry_to_svg_path()​

geometry_to_svg_path(geometry: geo.Geometry, width: int, height: int) -> str

Convert a normalized Geometry to an SVG path d attribute string.

The geometry coordinates should be in normalized [0, 1] space. Coordinates are scaled to pixel dimensions via width and height, with the Y axis flipped (SVG Y increases downward).

ParameterTypeDescription
geometrygeo.GeometryA Geometry object with normalized coordinates.
widthintTarget pixel width.
heightintTarget pixel height.
ReturnsstrSVG path d attribute string.
ComplexityO(n) where n = number of commands

parse_svg_path_data()​

parse_svg_path_data(
path_data: str,
transform: numpy.NDArray[numpy.float64] | None = None,
scale_x: float = 1,
scale_y: float = 1,
) -> list[geo.Geometry]

Parse an SVG path d attribute into a list of Geometry objects.

Supports M/m, L/l, H/h, V/v, C/c, Z/z commands. Cubic Bezier curves are flattened to line segments (20 steps).

ParameterTypeDescription
path_datastrSVG path d attribute string.
transformnumpy.NDArray[numpy.float64] | None = None3x3 affine transformation matrix, or None for identity.
scale_xfloat = 1X-axis scale factor for coordinate transform.
scale_yfloat = 1Y-axis scale factor for coordinate transform.
Returnslist[geo.Geometry]List of Geometry objects, one per subpath.
ComplexityO(n) where n = length of path data

svg_string_to_geometries()​

svg_string_to_geometries(
svg_str: str,
scale_x: float = 1,
scale_y: float = 1,
) -> list[geo.Geometry]

Parse an SVG string and extract all path elements as Geometry objects.

Recursively traverses the SVG XML tree, extracting d attributes from path elements and converting them to Geometry.

ParameterTypeDescription
svg_strstrSVG document as a string.
scale_xfloat = 1X-axis scale factor for coordinate transform.
scale_yfloat = 1Y-axis scale factor for coordinate transform.
Returnslist[geo.Geometry]List of Geometry objects from all path elements.
ComplexityO(n) where n = size of SVG document

svg_string_to_geometries_by_color()​

svg_string_to_geometries_by_color(
svg_str: str,
scale_x: float = 1,
scale_y: float = 1,
color_attr: svg.color.ColorAttr = raygeo.svg.color.ColorAttr.FILL,
) -> list[tuple[str, list[geo.Geometry]]]

Extract geometries grouped by color.

Walks the entire SVG tree and buckets shapes by their resolved fill/stroke color, applying SVG inheritance for presentation attributes. Bucket keys are lowercase #rrggbb hex strings; shapes with no usable color go into a '_no_color' bucket.

ParameterTypeDescription
svg_strstrSVG document as a string.
scale_xfloat = 1X-axis scale factor for coordinate transform.
scale_yfloat = 1Y-axis scale factor for coordinate transform.
color_attrsvg.color.ColorAttr = raygeo.svg.color.ColorAttr.FILLColor attribute to bucket by.
Returnslist[tuple[str, list[geo.Geometry]]]List of (color_key, geometry_list) tuples.
ComplexityO(n) where n = size of SVG document

svg_string_to_geometries_by_layer()​

svg_string_to_geometries_by_layer(
svg_str: str,
scale_x: float = 1,
scale_y: float = 1,
) -> list[tuple[str, list[geo.Geometry]]]

Extract geometries grouped by top-level layer.

Returns a list of (layer_id, geometries) tuples. Only top-level elements with an id attribute are treated as layers.

ParameterTypeDescription
svg_strstrSVG document as a string.
scale_xfloat = 1X-axis scale factor for coordinate transform.
scale_yfloat = 1Y-axis scale factor for coordinate transform.
Returnslist[tuple[str, list[geo.Geometry]]]List of (layer_id, geometry_list) tuples.
ComplexityO(n) where n = size of SVG document

svg_string_to_geometry()​

svg_string_to_geometry(
svg_str: str,
scale_x: float = 1,
scale_y: float = 1,
) -> geo.Geometry

Parse an SVG string and merge all subpaths into a single Geometry.

Like svg_string_to_geometries but returns one combined Geometry instead of a list, avoiding a Python-side merge loop.

ParameterTypeDescription
svg_strstrSVG document as a string.
scale_xfloat = 1X-axis scale factor for coordinate transform.
scale_yfloat = 1Y-axis scale factor for coordinate transform.
Returnsgeo.GeometryA single Geometry containing all paths.
ComplexityO(n) where n = size of SVG document

svg_string_to_geometry_by_color()​

svg_string_to_geometry_by_color(
svg_str: str,
scale_x: float = 1,
scale_y: float = 1,
color_attr: svg.color.ColorAttr = raygeo.svg.color.ColorAttr.FILL,
) -> list[tuple[str, geo.Geometry]]

Extract geometries grouped by color, merged into one Geometry each.

Like svg_string_to_geometries_by_color but merges each color bucket's subpaths into a single Geometry, avoiding a Python merge loop.

ParameterTypeDescription
svg_strstrSVG document as a string.
scale_xfloat = 1X-axis scale factor for coordinate transform.
scale_yfloat = 1Y-axis scale factor for coordinate transform.
color_attrsvg.color.ColorAttr = raygeo.svg.color.ColorAttr.FILLColor attribute to bucket by.
Returnslist[tuple[str, geo.Geometry]]List of (color_key, merged_geometry) tuples.
ComplexityO(n) where n = size of SVG document

svg_string_to_geometry_by_layer()​

svg_string_to_geometry_by_layer(
svg_str: str,
scale_x: float = 1,
scale_y: float = 1,
) -> list[tuple[str, geo.Geometry]]

Extract geometries grouped by layer, merged into one Geometry each.

Like svg_string_to_geometries_by_layer but merges each layer's subpaths into a single Geometry, avoiding a Python merge loop.

ParameterTypeDescription
svg_strstrSVG document as a string.
scale_xfloat = 1X-axis scale factor for coordinate transform.
scale_yfloat = 1Y-axis scale factor for coordinate transform.
Returnslist[tuple[str, geo.Geometry]]List of (layer_id, merged_geometry) tuples.
ComplexityO(n) where n = size of SVG document