ggmap
ggmap copied to clipboard
LonLat2XY not the inverse of XY2LonLat
Reproducible Example
The defaults for xpix and ypix are slightly different between the two conversion functions. One is 255 and the other 256. Was that intended?
I think this might make stamen maps not perfectly square. Google maps seem to be fine.
Example:
library(ggmap)
library(tidyverse)
my_zoom <- 4
test_coords <- data.frame(lon_in = 1, lat_in = 52)
# Default conversion to and back
tmp1 <- LonLat2XY(test_coords$lon_in,test_coords$lat_in,zoom = my_zoom) %>%
mutate(
lon_out = XY2LonLat(X = X, Y = Y, x = x, y = y,zoom = my_zoom)$lon,
lat_out = XY2LonLat(X = X, Y = Y, x = x, y = y,zoom = my_zoom)$lat
)
abs(test_coords$lon_in - tmp1$lon_out) < 1e-10 # FALSE
abs(test_coords$lat_in - tmp1$lat_out) < 1e-10 # FALSE
# Conversion to and back with defined xpix & ypix
my_xpix = 256
my_ypix = 256
tmp2 <- LonLat2XY(test_coords$lon_in,test_coords$lat_in,zoom = my_zoom,xpix = my_xpix,ypix = my_ypix) %>%
mutate(
lon_out = XY2LonLat(X = X, Y = Y, x = x, y = y,zoom = my_zoom,xpix = my_xpix,ypix = my_ypix)$lon,
lat_out = XY2LonLat(X = X, Y = Y, x = x, y = y,zoom = my_zoom,xpix = my_xpix,ypix = my_ypix)$lat
)
abs(test_coords$lon_in - tmp2$lon_out) < 1e-10 # TRUE
abs(test_coords$lat_in - tmp2$lat_out) < 1e-10 # TRUE
# Impact on maps
map <- get_map("london, uk",crop = T,zoom = 8,source = "stamen")
dim(map) # 640 639