1`LatLon2XY.centered` <-structure(function#computes the centered coordinate transformation from lat/lon to map tile coordinates
2### The function LatLon2XY.centered(MyMap, lat,lon,zoom) computes the coordinate transformation from lat/lon to map tile coordinates given a map object.
3(
4  MyMap, ##<< map object
5  lat, ##<< latitude values to transform
6  lon, ##<< longitude values to transform
7  zoom ##<< optional zoom level. If missing, taken from \code{MyMap}
8){
9
10  # transfXY<-function(MyMap,oldX, oldY)## only use for non proper coordinate transformations
11  # {
12  #   xlim<-c(MyMap[["BBOX"]][["ll"]][2],MyMap[["BBOX"]][["ur"]][2])
13  #   ylim<-c(MyMap[["BBOX"]][["ll"]][1],MyMap[["BBOX"]][["ur"]][1])
14  #   Rcoords<-list(newX=(oldX-xlim[1])/(xlim[2]-xlim[1]),
15  #                 newY=(oldY-ylim[1])/(ylim[2]-ylim[1]))
16  #   #browser()
17  #   return(Rcoords)
18  # }
19  #
20  # if (MyMap$url == "OSM") {
21  #   return(transfXY(MyMap,lon,lat))
22  # }
23   lat.center <- MyMap[[1]];
24   lon.center <- MyMap[[2]];
25   if (missing(zoom)) zoom <- MyMap[[3]];
26
27   #account for "jumps" at longitude boundaries -180/180
28   #browser()
29   lonLeft = as.numeric(MyMap$BBOX$ll)[2] #["lon"]
30   lonRight = as.numeric(MyMap$BBOX$ur)[2] #["lon"]
31
32   if (lonRight*lonLeft < 0 & FALSE){#sign change !
33     #print("fixing the jump at the boundaries")
34     if (MyMap$lon.center < 0){#subtract 360 from all positive longitude values:
35       lon[lon>0] = lon[lon>0]-360
36     } else if (MyMap$lon.center > 0){#add 360 to all negative longitude values:
37       lon[lon<0] = lon[lon<0]+360
38     }
39   }
40   mypoints <- LatLon2XY(lat,lon,zoom);
41   mycenter <- LatLon2XY(lat.center,lon.center,zoom);
42
43   Rcoords <- Tile2R(mypoints, mycenter);
44
45  ##seealso<< \link{LatLon2XY} \link{Tile2R}
46   return(list(newX=Rcoords$X,newY=Rcoords$Y));
47### properly scaled and centered (with respect to the center of \code{MyMap} ) coordinates
48###  \item{newX }{ transformed longitude}
49###  \item{newY }{transformed latitude}
50})
51
52
53