R Under development (unstable) (2013-06-11 r62941) -- "Unsuffered Consequences" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: x86_64-unknown-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > > # Read SVG > library(XML) > svg <- xmlParse("Multilingualism.svg") > > # Get all polygons > polys <- getNodeSet(svg, "//svg:polygon", + namespaces=c(svg="http://www.w3.org/2000/svg")) > > # Function to test polygon for inclusion > keep <- function(p) { + coords <- matrix(as.numeric(strsplit(xmlAttrs(p)["points"], + " |,")[[1]]), nrow=2) + all(coords[1,] > 300 & coords[1,] < 310 & + coords[2,] > 430 & coords[2,] < 440) + } > > retain <- sapply(polys, keep) > > # Function to scale polygons we want to keep > transform <- function(x) { + x <- sweep(x, 1, c(300, 400)) + x <- x*16 + x <- sweep(x, 1, c(90, 60), "+") + x + } > scale <- function(p) { + attrs <- xmlAttrs(p) + coords <- matrix(as.numeric(strsplit(attrs["points"], + " |,")[[1]]), nrow=2) + newcoords <- transform(coords) + attrs["points"] <- paste(apply(round(newcoords, 2), 2, + paste, collapse=","), + collapse=" ") + xmlAttrs(p) <- attrs + } > > invisible(lapply(polys[retain], scale)) > > # Function to remove polygon parent from its parent > remove <- function(p) { + removeChildren(xmlParent(xmlParent(p)), xmlParent(p)) + } > > invisible(lapply(polys[!retain], remove)) > > saveXML(svg, "Multilingualism-Auckland.svg") [1] "Multilingualism-Auckland.svg" > > proc.time() user system elapsed 157.089 0.080 157.601