1`RGB2GRAY` <-structure(function#translates an RGB image matrix to gray scale
2 ###  This function translates the rgb values of the array myTile into a scalar matrix with just one gray value per pixel.
3 (
4   myTile ##<< rgb image matrix, usually array with 3 dimensions
5 ){
6   #Gray scale intensity = 0.30R + 0.59G + 0.11B
7   stopifnot(attr(myTile, "type") != "gray");
8   f = NULL
9   ##details<< Gray scale intensity defined as  0.30R + 0.59G + 0.11B
10   if (class(myTile)[1] == 'nativeRaster'){
11     # comparing nativeRaster values is not easy, so let's do write/read again
12     f = file.path(tempdir(), "tmpMap.png")
13     writePNG(myTile, f)
14     myTile = readPNG(f)
15   }
16
17   if (class(myTile)[1] == 'array'){
18     #       if (0) {
19     #         tmp = matrix(0.7, ncol=dim(myTile)[2], nrow=dim(myTile)[1] )
20     #         for (i in 1:nrow(tmp))
21     # 		for (j in 1:ncol(tmp))
22     # 			tmp[i,j] = .3*myTile[i,j,1] + .59*myTile[i,j,2] + .11*myTile[i,j,3]
23     #          myTile = tmp
24     #        }
25     #or in vectorized form:
26     ncol=dim(myTile)[2]; nrow=dim(myTile)[1]
27     dim(myTile) <- c(prod(dim(myTile)[1:2]),dim(myTile)[3])
28     tmp = .3*myTile[,1] + .59*myTile[,2] + .11*myTile[,3]
29     myTile = matrix(tmp, ncol=ncol, nrow=nrow )
30
31   }
32   if (!is.null(f)){
33     writePNG(myTile, f)
34     myTile = readPNG(f, native = TRUE)
35   }
36   return(myTile);
37   ### image tile
38}, ex=function(){
39  if (0){
40    BrooklynLatLon = getGeoCode("Brooklyn")
41    mapBrooklyn <- GetMap(center=BrooklynLatLon, destfile = file.path(tempdir(), "Brooklyn.png"),
42                   zoom=11, size = c(240,240))
43    mapBrooklynBW$myTile = RGB2GRAY(mapBrooklyn$myTile)
44    PlotOnStaticMap(mapBrooklynBW)
45  }
46})
47
48