# the blighty package by David Lucy - plots the coastline of the British Isles
# sadly doesn't do Ireland as I hadn't a map of it, nor the Northern Isles.

blighty <- function(place="UK", grid=FALSE, xlimits, ylimits)
{

# get the vector of objectnames
objectnames <- getobjectnames()

# assign a vector of objects given the area of the UK selected
switch(place,
"UK" = objects <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27),
"Scotland" = objects <- c(1,5,8,11,13,14,15,16,17,18,19,21,22,23,24,25),
"Wales" = objects <- c(2,10,12),
"England" = objects <- c(1,2,3,4,6,7,9,27),
objects <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,16,27))

# assign a vector of x-ordinates dependent on the area of the UK selected
if(missing(xlimits)){
	switch(place,
	"UK" = xlimits <- c(0, 750),
	"Scotland" = xlimits <- c(0, 450),
	"Wales" = xlimits <- c(150, 400),
	"England" = xlimits <- c(100, 650),
	xlimits <- c(0, 750))}

# assign a vector of y-ordinates dependent on the area of the UK selected
if(missing(ylimits)){
	switch(place,
	"UK" = ylimits <- c(0, 1000),
	"Scotland" = ylimits <- c(500, 1000),
	"Wales" = ylimits <- c(150, 400),
	"England" = ylimits <- c(0, 650),
	ylimits <- c(0, 750))}

# calculate suitable plot limits to keep the map with a more or less
# truly square grid
lims <- sqlimits(xlimits, ylimits)

# setup the plot extremes
par(usr = c(lims$xlims[1], lims$xlims[2], lims$ylims[1], lims$ylims[2]))

# add to the plot all requested objects
	for(ctr in objects)
		{
		data(list = objectnames[ctr])
		points(get(objectnames[ctr]), type="l")
		}

# if gridding has been requested put on the OS coordinates and gridlines and box
if(grid == "TRUE")
	{
	box()
	xmarks <- pretty(lims$xlims)
	ymarks <- pretty(lims$ylims)

	axis(1, at=xmarks, labels=TRUE)
	abline(v=xmarks)
	axis(2, at=ymarks, labels=TRUE)
	abline(h=ymarks)
	}

return("Map complete")
}
# function which gets limits which make the plot square
# regardless of the shape of the plot area
# would stand being generalised to producing an arbitary
# aspect ratio for any plot for any plot window
#
# works by calculating new xlimts for any plot window
# to keep even axis within the plot
# it's main use is to get maps and plans with
# the correct aspect ratio - but can be used for
# other reasons
#
# input of two vectors one for the x one for the y
# in the order min x, max x -and- min y, max y
# output is in the same order only the new limits
#
# the output isn't quite accurate I suspect due to margins
# not being accounted for
sqlimits <- function(xlim, ylim)
{

# get the existing x and y limits
x1 <- xlim[1]
x2 <- xlim[2]
y1 <- ylim[1]
y2 <- ylim[2]

frame()
# calculate the existing figure aspect ratio
fig.ratio <- (x2 - x1)/(y2 - y1)
# grab the aspect ratio of the existing plot window
plot.ratio <- par("pin")[1] / par("pin")[2]

# if the x for the plot is larger than that for the
# existing window then we fix the x's and calculate
# new limits for the y
if(fig.ratio >= plot.ratio)
	{
	x1lim <- x1; x2lim <- x2
	ydist <- y2 - y1
	total.ydist <- (ydist * fig.ratio * (1/plot.ratio))
	diff.ydist <- (total.ydist - ydist) / 2
	y1lim <- y1 - diff.ydist; y2lim <- y2 + diff.ydist
	}

# if the x for the plot is smaller than that for the
# existing window then we fix the y's and calculate
# new limits for the x
if(fig.ratio < plot.ratio)
	{
	y1lim <- y1; y2lim <- y2
	xdist <- x2 - x1
	total.xdist <- (xdist * (1/fig.ratio) * plot.ratio)
	diff.xdist <- (total.xdist - xdist) / 2
	x1lim <- x1 - diff.xdist; x2lim <- x2 + diff.xdist
	}

xlims <- c(x1lim, x2lim)
ylims <- c(y1lim, y2lim)

return(xlims, ylims)
}




# just loads up a vector of all the map names
getobjectnames <- function()
{
objectnames <- c("Border.of.England.and.Scotland",
"Border.of.England.and.Wales",
"Coastline.of.Cornwall",
"Coastline.of.East.Anglia",
"Coastline.of.Eastern.Scotland",
"Coastline.of.North.East.England",
"Coastline.of.North.West.England",
"Coastline.of.Northern.Scotland",
"Coastline.of.South.England",
"Coastline.of.Wales",
"Coastline.of.Western.Scotland",
"Isle.of.Anglesea",
"Isle.of.Arran",
"Isle.of.Benbecular",
"Isle.of.Coll",
"Isle.of.Colonsay",
"Isle.of.Islay",
"Isle.of.Jura",
"Isle.of.Lewis",
"Isle.of.Man",
"Isle.of.Mull",
"Isle.of.North.Uist",
"Isle.of.Rum",
"Isle.of.Skye",
"Isle.of.South.Uist",
"Isle.of.Tiree",
"Isle.of.Wight")
return(objectnames)
}

cat("\nLoading blighty version 1.00\n")
cat("Copyright Lucy 2001\n\n")
