-1

I have the following SVG path object in R, with stroke-width 300:

[\"M2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2216 800L2208 800L2197 803L2181 814L2164 830L2142 852L2117 877L2093 901L2071 923L2055 939L2039 950L2032 958L2024 966L2017 981L2013 989\",\"M1594 698L1594 698L1594 698L1594 698L1594 698L1594 698L1587 698L1582 698L1578 700L1571 708L1563 725L1561 765L1561 818L1561 864L1561 903L1563 929L1566 953L1566 968L1566 984L1566 990L1571 999L1575 1007L1583 1010\"] 

My final goal is to calculate the area covered by the blot. At this moment I'm stuck with this data format. Any ideas on how to convert this object to a format that enables spatial analysis in R?

Alper Kucukkomurler
  • 1,706
  • 2
  • 13
  • 19
dao
  • 13
  • 3

1 Answers1

0

In an SVG path "M" means "move to", and "L" means "line to". You have two paths, they both only use one M and then a bunch of Ls so you can decode them without having to understand and parse the full possibilities of SVG paths.

If you split that thing you posted into two R strings you can get a vector of two strings like this:

> s
[1] "M2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2227 800L2216 800L2208 800L2197 803L2181 814L2164 830L2142 852L2117 877L2093 901L2071 923L2055 939L2039 950L2032 958L2024 966L2017 981L2013 989"
[2] "M1594 698L1594 698L1594 698L1594 698L1594 698L1594 698L1587 698L1582 698L1578 700L1571 708L1563 725L1561 765L1561 818L1561 864L1561 903L1563 929L1566 953L1566 968L1566 984L1566 990L1571 999L1575 1007L1583 1010"           

Then you split the strings on M or L, that gives you the coordinates in characters like "1594 698", which you then split on space characters, which you then convert to numeric, which you can then turn into a two-column matrix. So for the first one:

> matrix(as.numeric(unlist(strsplit(strsplit(s[1],"[ML]")[[1]]," "))), ncol=2, byrow=TRUE)
      [,1] [,2]
 [1,] 2227  800
 [2,] 2227  800
 [3,] 2227  800
 [4,] 2227  800
 [5,] 2227  800

The first eleven points are all identical. Repeat that for s[1] using basic R data handling and you get two paths which look like this:

enter image description here

There might be a full SVG path handler in R - have you searched? I'm sure there's at least one in Python and there's probably several in Javascript. Use one of those if your paths are more complex and have more of the possible SVG path control characters beyond M and L.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • I recently worked on a [question](https://stackoverflow.com/questions/52243318/parsing-svg-paths-in-r/) where the the OP delegated the parsing task from R to node.js module [svg-path-parser](https://github.com/hughsk/svg-path-parser). (Note that the error turned out to be not in the parser but in his code.) – ccprog Oct 03 '18 at 12:40