Reputation: 2621
I am trying to import an online SVG map into R, to see if I can reproduce the results in R.
First, I can download the html code of the SVGs using the following code:
svg <- html("https://dce2015.thestandnews.com") %>% html_nodes("g")
and further use
... %>% html_nodes("polygon") %>% html_attr("points")
... %>% html_nodes("polyline") %>% html_attr("points")
... %>% html_nodes("path") %>% html_attr("d")
to get the coordinates in a character strings.
I am able to deal with <polygon ...>
and <polyline ...>
but when I try to extract the coordinates inside <path ...>
, I find there are so many curve command (e.g. s
, S
, c
etc). Is there any method get some smoothed points lying on the curve?
Also, is there any more elegant way to import online SVG in R as polygons?
Thanks!
Upvotes: 1
Views: 839
Reputation: 78832
You would be much better off trying to find an actual shapefile of the electoral districts (I didn't really try to find one but a quick google suggested that it does exist). However, this was an interesting idea.
I think it'd be ultimately cool to port svg-to-wkt to R from DOM-javascript. But, if this is a one-off solution then you can do something like:
library(rvest)
library(rgeos)
URL <- "https://dce2015.thestandnews.com/"
pg <- read_html(URL)
svg_map <- html_nodes(pg, "svg#hkmap")
writeLines(as.character(svg_map), "hk.txt")
# use http://svg-to-wkt.linfiniti.com/ with svg from hk.txt
# saving it to hk.wkt
wkt_txt <- paste0(readLines("hk.wkt"), sep="", collapse="")
svg_wkt <- readWKT(wkt_txt)
svg_wkt
is a SpatialCollections
object that won't work with ggplot2. But, most of the SVG got converted to polygons:
plot(svg_wkt@polyobj)
Note that there are some elements in svg_wkt@lineobj
(which you would run into even if you did a full conversion in R IMO). Those will need to be dealt with.
You can pass in IDs to readWKT
in order to have some hope of being able to more with this in R (i.e. plot the election data it seems you want to plot) but I have to go back to my original suggestion of advocating the use of a proper shapefile if you really do want to plot the election data.
Upvotes: 3