How to get path coordinates from svg file to R

This may be a stupid question, but I don't have much experience with this. I need to get the coordinates from a polygon in order to create a path in R. This is a complex polygon with about 1000 points, so entering coordinates manually is crazy. I also need to extract the xy position of some objects inside the path. I tried using Illustrator and Inkscape to create an svg file that contains all the information. This seems like a good option, given that the svg file contains all the information. Is there a way to extract coordinates from paths or polygons? or is there any other easier way to make this process? I will be very grateful for any help, because I have to do this for 30 images. Greetings

+3
source share
1 answer

You can use the package XMLto retrieve 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 applied to the polygon.

+6
source

All Articles