1`qbbox` <-structure(function#computes bounding box
2### The function qbbox computes a bounding box for the given lat,lon
3### points with a few additional options such as quantile boxes, additional margins, etc.
4(
5  lat, ##<<  latitude values
6  lon, ##<<  longitude values
7  TYPE = c("all", "quantile")[1], ##<<
8  margin = list(m=c(1,1,1,1), TYPE = c("perc", "abs")[1]), ##<< relative or absolute margin around the data. Set to NULL if no margin desired.
9  q.lat = c(0.1,0.9), ##<< latitude quantile trimming, the tails will be trimmed from the bounding box
10  q.lon = c(0.1,0.9), ##<< longitude quantile trimming,
11  verbose=0 ##<<
12){
13 	if (TYPE == "all"){
14 	  latR <- range(lat,na.rm=TRUE);
15   	  lonR <- range(lon,na.rm=TRUE)
16 	} else if (TYPE == "quantile"){
17 	  latR <- quantile(lat, q.lat, na.rm=TRUE);
18      lonR <- quantile(lon, q.lon, na.rm=TRUE);
19 	}
20 	if (!is.null(margin)){
21 	  m <- margin$m;
22 	  lat.center <- latR[1] + diff(latR)/2;
23      lon.center <- lonR[1] + diff(lonR)/2;
24      if (margin$TYPE == "perc"){
25      	dlon <- c(-1,1)*(1+m[c(2,4)]/100)*diff(lonR)/2;
26      	dlat <- c(-1,1)*(1+m[c(1,3)]/100)*diff(latR)/2;
27      } else if (margin$TYPE == "abs"){
28      	dlon <- c(-1,1)*(m[c(2,4)] + diff(lonR)/2);
29      	dlat <- c(-1,1)*(m[c(1,3)] + diff(latR)/2);
30      }
31      lonR.margin <- lon.center + dlon;
32      latR.margin <- lat.center + dlat;
33      if (verbose>1) {
34      	cat("old/new lon range:");print(lonR);print(lonR.margin);
35      	cat("old/new lat range:");print(latR);print(latR.margin);
36      }
37      return(list(latR=latR.margin, lonR=lonR.margin))
38 	}
39
40 	return(list(latR=latR, lonR=lonR))
41### \item{latR }{latitude range}
42###  \item{lonR }{longitude range}
43
44 }, ex = function(){
45  lat = 37.85 + rnorm(100, sd=0.001);
46  lon = -120.47 + rnorm(100, sd=0.001);
47  #add a few outliers:
48  lat[1:5] <- lat[1:5] + rnorm(5, sd =.01);
49  lon[1:5] <- lon[1:5] + rnorm(5, sd =.01);
50
51  #range, discarding the upper and lower 10% of the data
52  qbbox(lat, lon, TYPE = "quantile");
53  #full range:
54  qbbox(lat, lon, TYPE = "all");
55  #add a 10% extra margin on all four sides:
56  qbbox(lat, lon, margin = list(m = c(10, 10, 10, 10), TYPE = c("perc", "abs")[1]));
57
58})
59
60
61