Map of cruise of the Derwent River taken on the Lady Jane, July 22, 2012
# Set some Knitr options, ther should be a better place to do this.
opts_knit$set(upload.fun = imgur_upload)
opts_knit$set(results = "asis") # upload all images to imgur.com
# Load in the necessary libraries.
require(rgdal)
## Loading required package: rgdal
## Warning: package 'rgdal' was built under R version 2.15.1
## Loading required package: sp
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.9.0, released 2011/12/29 Path to GDAL shared
## files:
## /Library/Frameworks/R.framework/Versions/2.15/Resources/library/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480] Path to
## PROJ.4 shared files:
## /Library/Frameworks/R.framework/Versions/2.15/Resources/library/rgdal/proj
require(ggmap)
## Loading required package: ggmap
## Loading required package: ggplot2
require(RgoogleMaps)
## Loading required package: RgoogleMaps
## Loading required package: png
## Attaching package: 'RgoogleMaps'
## The following object(s) are masked from 'package:sp':
##
## degAxis
# Define the GPX file containing the points, load into memory.
filed = "/Users/grant/Documents/Development/cruise.gpx"
pts = readOGR(filed, layer = "tracks")
## OGR data source with driver: GPX
## Source: "/Users/grant/Documents/Development/cruise.gpx", layer: "tracks"
## with 1 features and 12 fields
## Feature type: wkbMultiLineString with 2 dimensions
# Simplyfy things by extracting the track as a matrix of coordinates
mtrx = pts@lines[[1]]@Lines[[1]]@coords
mtrx = data.frame(mtrx)
names(mtrx) = c("Long", "Lat")
Normally I would try to use the ggmap library, which is based on ggplot2 and is friendlier but due to a bug it isn't compatible with knitr.
# Product map using ggmap library SHmap <- qmap('Hobart, Australia',
# maptype='hybrid',source = 'google', zoom=12) SHmap +
# geom_path(aes(x=Long, y=Lat), data=mtrx,col='red')
# Produce map using RGoogleMaps library
mean_long = mean(mtrx$Long)
mean_lat = mean(mtrx$Lat)
bb <- qbbox(mtrx$Lat, mtrx$Long, TYPE = "all", margin = list(m = rep(1, 4),
TYPE = c("perc", "abs")[1]))
MyMap <- GetMap.bbox(bb$lonR, bb$latR, destfile = "MyTile3.png", maptype = "hybrid")
## [1] "http://maps.google.com/maps/api/staticmap?center=-42.86718035,147.3424213&zoom=13&size=640x640&maptype=hybrid&format=png32&sensor=true"
tmp <- PlotOnStaticMap(MyMap, lat = mtrx$Lat, lon = mtrx$Long, fun = lines,
cex = 0.5, pch = 20, col = "blue", add = F)
## Warning: "fun" is not a graphical parameter