svg2psp: Convert a SVG file to a spatstat psp object.

View source: R/svg2psp.R

svg2pspR Documentation

Convert a SVG file to a spatstat psp object.

Description

Convert a SVG file to a spatstat psp object.

Usage

svg2psp(
  file,
  bezier = 5,
  owin = NULL,
  marks = 0,
  connect = FALSE,
  upward = FALSE,
  rightward = FALSE,
  reverse = TRUE,
  rescale = TRUE,
  ...
)

Arguments

file

File path of the svg file to convert.

bezier

Parameters for approximating bezier curves (see Details).

owin

Specify a window for the psp. If NULL, use details in the SVG file.

marks

Add marks to segments (see details).

connect

Assign segments to sets depending on their distance and orientation.

upward, rightward

directions of the segments (see details).

reverse

Define the position of the origin of the psp.

rescale

rescale psp to the dimensions given in the svg.

...

Arguments passed to cut.psp or connectedsets.psp.

Details

This functions provide a way to import SVG files in R to use with spatstat. Only absolute and relative SVG paths moveto, lineto, and curveto (quadratic and cubic bezier) are implemented at the moment. There seem to be a wide range of interpretations of the W3C SVG specifications, so import is not completely guaranteed. Package tested with SVG produced by autotrace (http://autotrace.sourceforge.net/) and Inkscape (https://inkscape.org/, Save as Plain SVG).

Bezier quadratic and cubic curves are approximated using the De Casteljau algorithm. Quadratic bezier curves are first approximated by a cubic bezier curves. If bezier = 0, bezier curves are converted to bezier polygons (i.e. goes through all control points). If bezier>0, Bezier curves are approximated by linear segments. The value of the parameter is the number of iterations used in the approximation (see https://en.wikipedia.org/wiki/De_Casteljau's_algorithm for details).

The resulting psp can have marks attached. If marks = 1, segments of the psp have a numeric mark depending on the SGV path they belong to. If marks = 2, all segments have a numeric mark depending on the SVG command that created them. If marks = 3, all segments have a unique mark (for debug purposes). Defaults to marks = 0 which results in an unmarked psp. If marks is a character, then marks will be read from the corresponding field in the svg file.

If connect = TRUE, the resulting psp is processed through connectedsets.psp. The resulting psp will be a marked psp with each mark corresponding to a set.

Parameters upward and rightward allow to alter the orientation of segments of the psp. If both are FALSE, detected segments have the orientation they have in the SVG file. If upward = TRUE, the direction of of segments is flipped so that all segments will point towards increasing values of the y direction. If rightward = TRUE, the direction of of segments is flipped so that all segments will point towards increasing values of the x direction.

The reverse parameter determines the position of the origin of the psp. reverse = F (default) considers the origin at the bottom left of the SVG. reverse = TRUE puts the origin of the image at the top left, as in SVG files.

Some SVG file contains size information. rescale determines whether the resulting psp dimensions are expressed in pixel units (rescale = FALSE, default) or in the units of the SVG file (rescale = TRUE, if available).

Value

A (marked) psp object

Examples

# Get file path of example data.
svgfile <- system.file("extdata", "SVG.svg", package = "svg2psp")

# Reverse and rescale are set to true to use the "paper" dimension of the
# SVG.
data <- svg2psp(svgfile, reverse = TRUE, rescale = TRUE)
plot(data)

xraynaud/svg2psp documentation built on April 7, 2024, 9:18 p.m.