R Leaflet not handling `tms=TRUE` for NASA JPL TMS provider

Given the verified (by NASA JPL!) tile provider...

https://mars.nasa.gov/mmgis-maps/M20/Layers/M20_HiRISE_RGB_mosaic_25cm_v4p2/{z}/{x}/{y}.png

...which uses TMS, R Leaflet does not correctly render the tiles when either setting tms=TRUE or using the {-y} trick. If we set TMS=FALSE, we see the y axis inverted, as expected (since the Mars M20 provider is definitely TMS!)

Here is a reprex, for the Jezero Crater area:

library(leaflet)

tmsSet <- TRUE # Should be TRUE normally
#tmsSet <- FALSE # Set to FALSE to demonstrate inverted y (and working endpoint!)

# Random test points in the Jezero area
points <- cbind(y=rnorm(40)/100 + 77.42151, x=rnorm(40)/100 + -18.44222)

leaflet(
  options =
    leafletOptions()
  ) %>%
  addTiles(
    # Surface tiles
    urlTemplate = "https://mars.nasa.gov/mmgis-maps/M20/Layers/M20_HiRISE_RGB_mosaic_25cm_v4p2/{z}/{x}/{y}.png",
    attribution = "Mars via <a href=\"https://mars.nasa.gov\">NASA</a>",
    options = tileOptions(minZoom = 11,
                          maxZoom = 19,
                          tms=tmsSet)
    ) %>%
  addTiles(
    # Place names tiles
    urlTemplate = "https://mars.nasa.gov/mmgis-maps/M20/Layers/M20_Placenames_175k_800dpi/{z}/{x}/{y}.png",
    attribution = "Mars via <a href=\"https://mars.nasa.gov\">NASA</a>",
    options = tileOptions(minZoom = 11,
                          maxZoom = 19,
                          tms=tmsSet)
    ) %>%
  setView(77.42151, -18.44222, zoom = 15) %>%
  addMarkers(data = points)

Here is some very simple code proving that JS Leaflet works perfectly with the NASA M20 provider, using the TMS=TRUE option...

<!DOCTYPE html>
<html lang="en">
<head>
        <base target="_top">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>TMS Mars example - Leaflet</title>

        <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />

    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
    <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin="" ></script>

        <style>
                html, body {
                        height: 100%;
                        margin: 0;
                }
                .header {
                 Access-Control-Allow-Origin: *
                 Content-Type: text/css
                }
                .leaflet-container {
                        height: 1000px;
                        width: 1000px;
                        max-width: 100%;
                        max-height: 100%;
                }
        </style>
</head>
<body>

<div id='map'></div>

<script type="text/javascript">

        const map = L.map('map', {
                center: [18.44222, 77.42151],
                zoom: 15,
        });

        const layer = L.tileLayer('https://mars.nasa.gov/mmgis-maps/M20/Layers/M20_HiRISE_RGB_mosaic_25cm_v4p2/{z}/{x}/{-y}.png').addTo(map);

</script>
</body>
</html>

Hi @olyerickson,
is there a reason the points in x are negative in the R example but not in the JS one ?
Using the same points as in the JS example the same output is achived (assuming the -18.44222 is wrong).

Full code:

library(leaflet)

tmsSet <- TRUE # Should be TRUE normally

# Random test points in the Jezero area
points <-
  cbind(y = rnorm(40) / 100 + 77.42151,
        x = rnorm(40) / 100 + 18.44222)

leaflet() %>%
  addTiles(
    # Surface tiles
    urlTemplate = 'https://mars.nasa.gov/mmgis-maps/M20/Layers/M20_HiRISE_RGB_mosaic_25cm_v4p2/{z}/{x}/{-y}.png',
    attribution = "Mars via <a href=\"https://mars.nasa.gov\">NASA</a>",
    options = tileOptions(
      minZoom = 11,
      maxZoom = 19,
      tms = tmsSet 
    )
  ) %>%
  addTiles(
    # Place names tiles
    urlTemplate = "https://mars.nasa.gov/mmgis-maps/M20/Layers/M20_Placenames_175k_800dpi/{z}/{x}/{-y}.png",
    attribution = "Mars via <a href=\"https://mars.nasa.gov\">NASA</a>",
    options = tileOptions(
      minZoom = 11,
      maxZoom = 19,
      tms = tmsSet 
    )
  ) %>%
  setView(lng = 77.42151, lat = 18.44222, zoom = 15) %>%
  addMarkers(data = points)

Thank you!

To answer your question, the {-y} "trick" is the original convention for telling Leaflet that the provider is TMS; the tms = TRUE option was added later.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.