1`GetOsmMap` <- structure(function#Query the Open Street Map server for map tiles instead of Google Maps
2### The querying parameters for Open Street Maps are somewhat different in this version.
3### Instead of a zoom, center and size, the user supplies a scale parameter and a lat/lon bounding box.
4### The scale determines the image size.
5(
6  lonR=c(-74.02132,-73.98622), ##<< longitude range
7  latR= c(40.69983,40.72595), ##<< latitude range
8  scale= 20000, ##<< Open Street map scale parameter. The larger this value, the smaller the resulting map tile in memory. There is a balance to be struck between the lat/lon bounding box and the scale parameter.
9  destfile = "MyTile.png", ##<<  File to load the map image from or save to, depending on \code{NEWMAP}.
10  format = 'png', ##<< (optional) defines the format of the resulting image.
11  RETURNIMAGE = TRUE, ##<< return image yes/no default: TRUE
12  GRAYSCALE =FALSE, ##<< Boolean toggle; if TRUE the colored map tile is rendered into a black & white image, see \link{RGB2GRAY}
13  NEWMAP = TRUE, ##<< if TRUE, query the Google server and save to \code{destfile}, if FALSE load from destfile.
14  verbose=1, ##<< level of verbosity,
15  ... ##<< extra arguments to be used in future versions
16){
17   ##note<<The OSM maptile server is frequently too busy to accomodate every request, so patience is warranted.
18 	 options(scipen = 12);#to suppress scientific notation on the scale parameter
19
20 	OSMbbox <- paste(lonR[1],latR[1],lonR[2],latR[2], sep=",")
21 	#http://tile.openstreetmap.org/cgi-bin/export?bbox=-4.54,47.49,4.37,52.45&scale=14000000&format=png
22    OSMurl <- 'http://tile.openstreetmap.org/cgi-bin/export?';
23 	url <- paste(OSMurl, "bbox=", OSMbbox, "&scale=", scale, "&format=", format, sep="")
24 	#OR, use zoom level (e.g. z=12 ):
25 	# http://tah.openstreetmap.org/MapOf/index.php?long=-74.02132&lat=40.69983&z=12&w=256&h=256&format=png
26
27 	if (verbose) print(url);
28
29 	if (NEWMAP) ret <- download.file(url, destfile, mode="wb", quiet = FALSE);
30    #BBOX <- list(ll = c(lonR[1], latR[1]), ur = c(lonR[2], latR[2]) );
31    #thanks to Julien Barnier who corrected this bug:
32    BBOX <- list(ll = c(latR[1], lonR[1]), ur = c(latR[2], lonR[2]))
33	MetaInfo <- list(lat.center = mean(latR), lon.center  = mean(lonR), zoom = NULL, url = "OSM", BBOX = BBOX, scale=scale, SCALE = 1);
34	save(MetaInfo, file = paste(destfile,"rda",sep="."));
35 	if (verbose == -1) browser();
36
37 	if (RETURNIMAGE){
38 	  myMap <- ReadMapTile(destfile);
39 	  if (GRAYSCALE) {
40     	     myMap$myTile <- RGB2GRAY(myMap$myTile);
41        }
42	  return(myMap);
43 	} else {
44	  invisible(url)
45	}
46### map structure or URL used to download the tile.
47 }, ex = function(){
48if (0) {
49 	CologneMap <- GetOsmMap(lonR= c(6.89, 7.09), latR = c(50.87, 51), scale = 150000,
50                            destfile = "Cologne.png");
51	PlotOnStaticMap(CologneMap, mar=rep(4,4), NEWMAP = FALSE, TrueProj = FALSE, axes= TRUE);
52
53	PrincetonMap <- GetOsmMap(lonR= c(-74.67102, -74.63943), latR = c(40.33804,40.3556),
54                             scale = 12500, destfile = "Princeton.png");
55	png("PrincetonWithAxes.png", 1004, 732)
56      PlotOnStaticMap(PrincetonMap, axes = TRUE, mar = rep(4,4));
57    dev.off()
58 }
59})
60
61
62