【发布时间】:2014-07-07 16:52:23
【问题描述】:
我想使用 R 读取 SVG 文件,根据数据值更改 SVG 文件,然后将 SVG 导出为光栅图像,例如 png。
通过一些研究,我已经确定 SVGMapping 包本来可以帮助我到达那里,但是这个包已从 CRAN 存储库中删除,因此我对将它用于我可能依赖的任何东西都持谨慎态度。
因为 SVG 是 XML,所以我的计划是使用 XML 包来更改我的 SVG 文件的属性。然后我想使用 shell 函数调用 inkscape 将 SVG 转换为 PNG。
我在导航 XML 结构时遇到问题。正如您在下面的 SVG 中看到的,我创建了三个不同颜色的矩形。我想用 R 改变颜色。
我可以阅读 SVG 并使用以下内容导航到属性:
doc <- xmlTreeParse("c:\\Temp\\drawing.svg", getDTD = F)
doc$children$svg$children$g$children[1]$rect$attributes
然后我可以使用以下方法提取所有样式信息:
doc$children$svg$children$g$children[1]$rect$attributes["style"]
但是这样的结果只是一个包含填充、描边、不透明度等值的字符串。
"fill:#00ffff;fill-opacity:1;stroke:#000000;stroke-width:2;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
有没有办法选择一个样式属性,例如填充,这样我就可以输入一个新的颜色代码?
我猜我最终可能会使用基于正则表达式的东西,但我想我会问是否有替代(即更简单!)的方式来浏览样式属性。
SVG 文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="744.09448819"
height="1052.3622047"
id="svg2"
version="1.1"
inkscape:version="0.48.2 r9819"
sodipodi:docname="New document 1">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.53183594"
inkscape:cx="375"
inkscape:cy="520"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="739"
inkscape:window-height="558"
inkscape:window-x="175"
inkscape:window-y="175"
inkscape:window-maximized="0" />
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<rect
style="fill:#ff0000;fill-opacity:1;stroke:#000000;stroke-width:2;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="rect2985"
width="180.50679"
height="165.46455"
x="30.084465"
y="225.03938"
rx="0"
ry="0" />
<rect
style="fill:#00ff00;fill-opacity:1;stroke:#000000;stroke-width:2;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="rect2985-1"
width="180.50679"
height="165.46455"
x="312.12634"
y="230.68022"
rx="0"
ry="0" />
<rect
style="fill:#00ffff;fill-opacity:1;stroke:#000000;stroke-width:2;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="rect2985-7"
width="180.50679"
height="165.46455"
x="118.45758"
y="456.31369"
rx="0"
ry="0" />
</g>
</svg>
【问题讨论】:
-
您愿意修改 SVG 文件吗?您可以将样式替换为更易于解析的属性。