1
votes

How can i find correct leaflet tile on Server give longitude / latitude.

I follow the Formula on slide 19 of this presentation: https://www.tu-chemnitz.de/urz/stammtisch/rsrc/vortrag-stammtisch.pdf (sry German).

Goal: Calculate (zoom, x and y) given longitude and latitude

Additional Information:

I found this docu https://mapserver.org/mapcache/services.html, which states that x is column number in zxy naming scheme and y the tow number.

What i tried:

#London
long = 51.52
lat = -0.18
zoom = 5
lambda = pi/180*long
phi = pi/180*lat

x = 2^zoom*(lambda + pi)/(2*pi)
y = 2^zoom*(pi - log(tan(phi) + 1/(cos(phi))))/(2*pi)
x
y

glue("https://tile.openstreetmap.org/{zoom}/{x}/{y}.png")
1
I am afraid I cannot read German. So I am not sure what the slide is saying in that link. Anyway, Which package(s) are you using to draw a map with leaflet in R? Are you trying to choose a title?jazzurro
Thank you for your answer. I am using leaflet. I would like to understand the steps from Input (latitude, longitude) to the corresponding "tile-url": tile.openstreetmap.org{zoom}/{x}/{y}.png. So i would have to calculate zoom, x and y. Sry for providing a German link, but it was the best source i found to calculate These three Parameters.Tlatwork
So you want to know how data points are projected on a leaflet map. Is that right?jazzurro
thanks for the fast Replies! that would be a broader description yes! More concrete would be to calculate. (zoom, x and y) given longitude and latitude.Tlatwork
I see. That's beyond my knowledge. I hope someone can help you out.jazzurro

1 Answers

0
votes

You can wrap the leaflet map in a really simple shiny app, and calculate on the fly the tile using leaflet shiny map events (info in the Inputs/Events section)

library(shiny)
library(leaflet)
library(glue)

ui <- fluidPage(
  leafletOutput('map'),
  textOutput('tile')
)

server <- function(input, output, session) {

  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles(options = tileOptions(noWrap = TRUE))
  })

  tile_finder <- eventReactive(input$map_click, {
    long <- input$map_click$lng
    lat <- input$map_click$lat
    zoom <- input$map_zoom
    lambda <- pi/180*long
    phi <- pi/180*lat

    x <- (2^zoom*(lambda + pi)/(2*pi))%/%1
    y <- (2^zoom*(pi - log(tan(phi) + 1/(cos(phi))))/(2*pi))%/%1

    glue("https://tile.openstreetmap.org/{zoom}/{x}/{y}.png")
  })

  output$tile <- renderText({
    tile_finder()
  })

}

shinyApp(ui, server)

You will get the tile info when clicking over the map, and the info appears below

enter image description here

The result for Paris, give this link https://tile.openstreetmap.org/8/129/88.png