如何获取从svg文件到R的路径的坐标 [英] How to get coordinates of a path from svg file into R

查看:167
本文介绍了如何获取从svg文件到R的路径的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个愚蠢的问题,但是我没有很多经验.我需要从多边形中获取坐标以在R中创建轮廓.它是大约1000个点的复杂多边形,因此手动输入坐标太疯狂了.另外,我需要提取轮廓内某些对象的xy位置. 我试图使用Illustrator和Inkscape创建一个包含所有信息的svg文件.考虑到svg文件包含所有信息,这似乎是一个不错的选择.有没有一种方法可以从路径或多边形点中提取坐标?还是有其他更简单的方法来执行此过程? 我将非常感谢您的帮助,因为我必须处理大约30张图像. 干杯

May be it is a silly question but I don't have a lot of experience doing this. I need to get the coordinates from a polygon to create a contour in R. It is a complex polygon of about 1000 points so to input the coordinates manually is crazy. Also I need to extract the xy position of some objects inside the contour. I tried to use Illustrator and Inkscape to create an svg file that contains all the information. It looks like a good option considering that the svg file contains all the information. Is there a way to extract the coordinates from the path or polygon nods? or there is any other simpler way to do this process? I will really appreciate any help because I have to do it for around 30 images. Cheers

推荐答案

您可以使用XML包提取坐标.

You can use the XML package to extract the coordinates.

# Sample data
library(RCurl)
url <- "http://upload.wikimedia.org/wikibooks/en/a/a8/XML_example_polygon.svg"
svg <- getURL(url)

# Parse the file
library(XML)
doc <- htmlParse(svg)

# Extract the coordinates, as strings
p <- xpathSApply(doc, "//polygon", xmlGetAttr, "points")

# Convert them to numbers
p <- lapply( strsplit(p, " "), function(u) 
  matrix(as.numeric(unlist(strsplit(u, ","))),ncol=2,byrow=TRUE) )
p

但是,这忽略了要应用于多边形的任何变换.

However, this ignores any transformation to be applied to the polygon.

这篇关于如何获取从svg文件到R的路径的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆