Showing posts with label Maps. Show all posts
Showing posts with label Maps. Show all posts

Sunday, September 20, 2015

Retrieve GBIF Species Occurrence Data with Function from dismo Package

..The dismo package is awesome: with some short lines of code you can read & map species distribution data from GBIF (the global biodiversity information facility) easily:






library(dismo)

# get GBIF data with function:
myrger <- gbif("Myricaria", "germanica", geo = T)

# check:
str(myrger)

# plot occurrences:
library(maptools)
data(wrld_simpl)
plot(wrld_simpl, col = "light yellow", axes = T)
points(myrger$lon, myrger$lat, col = "red", cex = 0.5)
text(-140, -50, "MYRICARIA\nGERMANICA")
Read more »

Displaying any Google Maps Content in Google Earth via Network-Links

This is a handy option for displaying Google Maps in Google Earth. This could be any of your own maps created in Google Maps, a route or whatever. One nice feature of GE's Network-Links is that if you or any other contributor of a Google map modifies it (in Google Maps), it will be refreshed also in Google Earth.To do so, just grab the link from Google Maps. Like this one for a route:

https://maps.google.at/maps?saddr=Tivoli+Stadion+Tirol,+Innsbruck&daddr=Haggen,+Sankt+Sigmund+im+Sellrain&hl=de&ll=47.227496,11.250687&spn=0.12007,0.338173&sll=47.230526,11.250825&sspn=0.120063,0.338173&geocode=FagR0QIdYR-uACGQHB8W0fIkjCkfsEUhQ2mdRzGQHB8W0fIkjA%3BFZhY0AId6CypACnvxXqyRz2dRzG6da9r3hgbNA&oq=hagge&t=h&dirflg=h&mra=ls&z=12


Then go to GE and choose "Add" from main menu and "Network-Link" from the submenu. Then paste the above link and append "&output=kml" to it. That's it!
Read more »

A Short Example with R-Package osmar..

Following up my last post in which I praised the capabilities of the osmar-package I give a short example...

ps: You can also find this example at GitHub HERE.








library(osmar)

# this pulls the data from the OSM-Api:
mydistrict <- get_osm(relation(85647), full = TRUE)

# make a spatial object:
mydistrict_sp <- as_sp(mydistrict, what = 'lines')
summary(mydistrict_sp)

# just for fun i'll plot some bubbles on my map:
mydata <- data.frame(x = runif(100, 12.300, 12.550),
y = runif(100, 47.300, 47.550),
data = sample(1:30, 100, replace = T))
mydata_sp <- SpatialPointsDataFrame(mydata[, c('x', 'y')], data=mydata,
proj4string=CRS('+proj=longlat +datum=WGS84'))
# see what's in there:
summary(mydata_sp)

# plot:
setwd(tempdir())
pdf("testOSM.pdf")
par(oma = rep(0, 4))
mycex = (mydata_sp@data[,'data']/max(mydata_sp@data[,'data'])*2)^2
plot(mydistrict_sp, axes = T)
colpts = rgb(0.2, 0.5, 0.4, alpha = 0.6)
points(x = mydata_sp@coords[,'x'],
y = mydata_sp@coords[,'y'],
cex = mycex, col = 0,
pch = 21, bg = colpts)
title('MyData in MyDistrict')

# legend:
l1 = min(mydata_sp@data[,'data'])
l2 = round(mean(mydata_sp@data[,'data']), 0)
l3 = max(mydata_sp@data[,'data'])
l = c(l1, l2, l3)
lcex = (c(l/l3*2))^2
points(x = rep(12, 3), y = seq(47.7, 47.6, -.05),
cex = lcex, col = 0,
pch = 21, bg = colpts)
text(l, x = rep(12, 3) +.045, y = seq(47.7, 47.6, -.05))

graphics.off()

# open map:
shell.exec("testOSM.pdf")
Read more »

Nice Species Distribution Maps with GBIF-Data in R

Here's an example of how to easily produce real nice distribution maps from GBIF-data in R with package maps...















# go to gbif and download a csv file with the species you 
# want (example data)
# ..in R first set working directory to your download directory..
data <- read.csv("occurrence-search-1316347695527.CSV",
header = T, sep = ",")

str(data)
require(maps)
pdf("myr_germ.pdf")
map("world", resolution = 75)
points(data$Longitude, data$Latitude, col = 2, cex = 0.05)
text(-140, -50, "MYRICARIA\nGERMANICA")
dev.off()

# that's all there is to it..
Read more »

Producing Google Map Embeds with R Package googleVis

(1) for producing html code for a Google Map with R-package googleVis do something like:
 

library(googleVis)
df <- data.frame(Address = c("Innsbruck", "Wattens"),
Tip = c("My Location 1", "My Location 2"))
mymap <- gvisMap(df, "Address", "Tip", options = list(showTip = TRUE, mapType = "normal",
enableScrollWheel = TRUE))
plot(mymap) # preview
(2) then just copy-paste the html to your blog or website after customizing for your purpose..
Read more »

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.








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/
Read more »

Some Fun with googleVis - Mapping Blog Visits on Google Map

See stand-alone code to produce this map below.


require(RJSONIO)
require(RCurl)
require(plyr)
library(googleVis)

# function to retrieve coords from ip-addresses
# (https://github.com/rtelmore/RDSTK/blob/master/src/RDSTK/R/functions.R)
ip2coordinates <- function(ip, session=getCurlHandle()) {
api <- "http://www.datasciencetoolkit.org/ip2coordinates/"
get.ips <- getURL(paste(api, URLencode(ip), sep=""), curl=session)
result <- ldply(fromJSON(get.ips), data.frame)
names(result)[1] <- "ip.address"
return(result)
}

# read log-file:
setwd(tempdir())
download.file("http://docs.google.com/uc?export=download&id=0B2wAunwURQNsZjY4Njc1OTYtYWE3My00ZjNjLTg1YzEtNDNlNmYyYmNiYmI0",
destfile = "google_docs.csv", mode = "wb")
log <- read.csv(paste(tempdir(),"/", "google_docs.csv", sep = ""),
header = T, stringsAsFactors = F)

# create dataframe to collect coords:
nr = nrow(log)

Lon <- as.numeric(rep(NA, nr))
Lat <- Lon
Coords <- data.frame(Lon, Lat)

# some will not be found (I will dismiss these rows later):
for (i in 1:nr){
try(
Coords[i, 1:2] <- ip2coordinates(log$IP.Address[i])[c("longitude", "latitude")]
)
}

# append to log-file:
log <- data.frame(log, Lat = Coords$Lat, Long = Coords$Lon,
LatLong = paste(round(Coords$Lat, 1),
round(Coords$Lon, 1),
sep = ":"))

log_gmap <- log[!is.na(log$Lat), ]
gmap <- gvisMap(log_gmap, "LatLong",
options = list(showTip = TRUE, enableScrollWheel = TRUE,
mapType = 'hybrid', useMapTypeControl = TRUE,
width = 550, height = 300))
plot(gmap)
Read more »

Saturday, September 19, 2015

..A Quick Geo-Trick for GoogleMaps in R (using dismo)

... I thought this geocoding-bit might be worth sharing (found HERE when searching the web for dismo-documentation).










library(dismo)

x <- geocode("Bozner Platz, Innsbruck, Tirol, Austria")
e <- extent(unlist(x[4:7]))
g <- gmap(e, type = "satellite")

plot(g)
Read more »

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)
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()
ps: Check THESE nice maps, too!
Read more »