Showing posts with label maptools. Show all posts
Showing posts with label maptools. Show all posts
Sunday, September 20, 2015
Convert OpenStreetMap Objects to KML with R
A quick geo-tip:
With the osmar and maptools package you can easily pull an OpenStreetMap object and convert it to KML, like below (thanks to adibender helping out on SO). I found the relation ID by googling for it (www.google.at/search?q=openstreetmap+relation+innsbruck).
Read more »
With the osmar and maptools package you can easily pull an OpenStreetMap object and convert it to KML, like below (thanks to adibender helping out on SO). I found the relation ID by googling for it (www.google.at/search?q=openstreetmap+relation+innsbruck).
# get OSM data
library(osmar)
library(maptools)
innsbruck <- get_osm(relation(113642), full = T)
sp_innsbruck <- as_sp(innsbruck, what = "lines")
# convert to KML
for( i in seq_along(sp_innsbruck) ) {
kmlLine(sp_innsbruck@lines[[i]], kmlfile = "innsbruck.kml",
lwd = 3, col = "blue", name = "Innsbruck")
}
shell.exec("innsbruck.kml")
A Simple Example for the Use of Shapefiles in R
A simple example for drawing an occurrence-map (polygons with species' points) with the R-packages maptools and sp using shapefiles.
HERE is the example data.
To cite package ‘maptools’ in publications use:
Nicholas J. Lewin-Koh, Roger Bivand, contributions by Edzer J.
Pebesma, Eric Archer, Adrian Baddeley, Hans-Jörg Bibiko, Stéphane
Dray, David Forrest, Michael Friendly, Patrick Giraudoux, Duncan
Golicher, Virgilio Gómez Rubio, Patrick Hausmann, Karl Ove
Hufthammer, Thomas Jagger, Sebastian P. Luque, Don MacQueen, Andrew
Niccolai, Tom Short, Ben Stabler and Rolf Turner (2011). maptools:
Tools for reading and handling spatial objects. R package version
0.8-10. http://CRAN.R-project.org/package=maptools
To cite package ‘sp’ in publications use:
Pebesma, E.J., R.S. Bivand, 2005. Classes and methods for spatial
data in R. R News 5 (2), http://cran.r-project.org/doc/Rnews/.
Roger S. Bivand, Edzer J. Pebesma, Virgilio Gomez-Rubio, 2008.
Applied spatial data analysis with R. Springer, NY.
http://www.asdar-book.org/
Read more »
HERE is the example data.
library(maptools)
library(sp)
# Note that for each shapefile, you only need to read the .shp component
# the others will be read in at the same time automatically.
# TIRIS.BEZIDX_PL.shp contains the political districts of North-Tyrol
# Limodorum.shp contains points with occurences of the plant species
# Limodorum abortivum.
# Note that my layers use the same geographic coordinate systems (gcs),
# using other data you would need to check if the gcs and
# projection of all layers are the same!
# set dir to where you downloaded the data:
setwd("E:/R/Data/Maps/Example.1_Data")
Limodorum.shp <- readShapePoints(file.choose())
TIRIS.BEZIDX_PL.shp <- readShapePoly(file.choose())
# examine points:
summary(Limodorum.shp)
attributes(Limodorum.shp@data)
# examine polygons:
summary(TIRIS.BEZIDX_PL.shp)
attributes(TIRIS.BEZIDX_PL.shp@data)
# limits:
# to customize ylim in plot-call
# seems not to work here...
xlim <- TIRIS.BEZIDX_PL.shp@bbox[1, ]
ylim <- TIRIS.BEZIDX_PL.shp@bbox[2, ]
par(mai = rep(.1, 4))
plot(TIRIS.BEZIDX_PL.shp, col = "grey93", axes = F,
xlim = xlim, ylim = ylim, bty = "n")
points(Limodorum.shp, pch = 16,
col = 2, cex = .5)
mtext("Limodorum abortivum", 3, line = -7,
at = -17000, adj = 0, cex = 2, font = 3)
legend("bottomleft", inset = c(0.4, 0.2),
legend = c("Fundpunkte", "Polit. Bezirke"),
bty = "n", pch = c(16,-1), # bty = "n": no box
col = c(2, 1), pt.cex = c(.5, 1),
lty = c(-1, 1))
To cite package ‘maptools’ in publications use:
Nicholas J. Lewin-Koh, Roger Bivand, contributions by Edzer J.
Pebesma, Eric Archer, Adrian Baddeley, Hans-Jörg Bibiko, Stéphane
Dray, David Forrest, Michael Friendly, Patrick Giraudoux, Duncan
Golicher, Virgilio Gómez Rubio, Patrick Hausmann, Karl Ove
Hufthammer, Thomas Jagger, Sebastian P. Luque, Don MacQueen, Andrew
Niccolai, Tom Short, Ben Stabler and Rolf Turner (2011). maptools:
Tools for reading and handling spatial objects. R package version
0.8-10. http://CRAN.R-project.org/package=maptools
To cite package ‘sp’ in publications use:
Pebesma, E.J., R.S. Bivand, 2005. Classes and methods for spatial
data in R. R News 5 (2), http://cran.r-project.org/doc/Rnews/.
Roger S. Bivand, Edzer J. Pebesma, Virgilio Gomez-Rubio, 2008.
Applied spatial data analysis with R. Springer, NY.
http://www.asdar-book.org/
Saturday, September 19, 2015
Use IUCN API with R & XPath
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)ps: Check THESE nice maps, too!
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"))
distr2[distr2 == "Russian Federation"] <- "Russia"
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 = "grey75", 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(), "/", input, ".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, 115, 95, 115+w, 95+h)
graphics.off()
Subscribe to:
Posts (Atom)
