dkahle / dkahle/ggmap

LonLat2XY not the inverse of XY2LonLat

Open
#300 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
R
Stars
778
Forks
234
PR merge metrics
No merged PRs in 30d

Description

## 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:

```r
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
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.