Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use IUCN API with R & XPath

DZone's Guide to

How to Use IUCN API with R & XPath

· Big Data Zone
Free Resource

Need to build an application around your data? Learn more about dataflow programming for rapid development and greater creativity. 

Thanks to a posting on R-sig-eco mailing list I learned of the IUCN-API. Here's a simple example for what can be done with it (output as pdf is HERE):
require(XML)
require(maptools)
require(jpeg)
 
input = "panthera-uncia"
h <- htmlParse(paste("http://api.iucnredlist.org/go/",
               input, sep = ""))
 
distr1 <- xpathSApply(h, '//ul[@class="countries"]', xmlValue)
distr2 <- unlist(strsplit(distr1, "\n"))
 
pop <-xpathSApply(h, '//div[@id="population"]/text()[preceding-sibling::br]', xmlValue)
status <- xpathSApply(h, '//div[@id="red_list_category_code"]', xmlValue)
 
data(wrld_simpl)
 
pdf("IUCN_map.pdf", width = 10, height = 10, pointsize = 20)
par(mar = c(3, 3, 1, 1))
plot(wrld_simpl, col = "grey98", xlim=c(-170, 170), axes = T)
plot(wrld_simpl[wrld_simpl$NAME %in% distr2,], col = "grey94", add = T)
text(0, 150, gsub("-", " ", toupper(input)), font = 3)
text(0, 130, paste("--Status: ", status, "--", sep = ""))
text(0, -98, "--Population--", cex = 0.5, font = 2)
text(0, -140, paste(strwrap(pop, width = 30), collapse = "\n"), cex = 0.4)
 
# download image:
myjpg <- paste(tempdir(), "pan_unc.jpg", sep = "/")
download.file("http://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Uncia_uncia.jpg/399px-Uncia_uncia.jpg",
              myjpg, mode = "wb")
 
# read and plot image:
img <- readJPEG(myjpg)
w <- dim(img)[2]/7
h <- dim(img)[1]/7
 
# print img to plot region:
rasterImage(img, 129, 100, 129+w, 100+h)
 
graphics.off()
 

Check out the Exaptive data application Studio. Technology agnostic. No glue code. Use what you know and rely on the community for what you don't. Try the community version.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}