我不知道一個通用的解析器,我想我會試着看規範,看看它會是多麼複雜。這些粗略的第一遍是將這些命令轉換爲點。首先,一個輔助函數的兩個計算第二度貝塞爾曲線:
c_curve<-function(start, c1, c2, end, n=100) {
c1 <- start+c1
c2 <- start+c2
end <- start+end
sapply((0:n)/n, function(i) {
q1 <- start+i*(c1-start)
q2 <- c1+i*(c2-c1)
q3 <- c2+i*(end-c2)
r1 <- q1+i*(q2-q1)
r2 <- q2+i*(q3-q2)
r1+i*(r2-r1)
})
}
然後你就可以用
parse_svg_path <- function(path) {
parts <- regmatches(path, gregexpr("[A-Za-z]|,|-?[0-9.]+", path, perl=T))[[1]]
parts <- parts[parts!=","]
vals <- suppressWarnings(as.numeric(parts))
i <- 1
points <- matrix(ncol=0, nrow=2)
while(i < length(parts)) {
if (parts[i]=="M") {
points <- cbind(points, c(vals[i+1], vals[i+2]))
i <- i+3
} else if (parts[i]=="c") {
cpoints <- c_curve(
points[, ncol(points)],
c(vals[i+1], vals[i+2]),
c(vals[i+3], vals[i+4]),
c(vals[i+5], vals[i+6])
)
points <- cbind(points, cpoints)
i <- i+7
} else {
stop(paste("unrecognized command", parts[i]))
}
}
points
}
解析字符串,你可以用
path <- "M12.967,349.469c15.107-87.283,25.932-180.142,54.214-264.61c31.17-93.095,54.138,17.688,65.096,53.934c11.354,37.558,23.177,74.976,34.309,112.6c26.534,89.679,79.275-25.286,92.183-45.57c11.749-18.462,20.938-43.699,69.798-48.289c70.298-6.604,177.054-4.848,224.858-5.774"
pp <- parse_svg_path(path)
如果運行我繪製我得到
plot(pp[1,], pp[2,], type="l")
# ggplot version:
# ggplot(data.frame(x=pp[1,], y=pp[2,])) + geom_path(aes(x,y))
我希望這是正確的形狀?
你可以寫一點Javascript,使用'getPointAtLength()'來提取線上各點的曲線座標。 –
使用:https://www.npmjs.com/package/svg2geojson,然後使用生成的shapefile – hrbrmstr
看看grImport https://sjp.co.nz/projects/grimport2/ – baptiste