[GIS] Recreating coordinate transformation from Javascript leaflet map in R Leaflet

coordinate systemleafletprojr

At the bottom is Javascript code and it manages to add a map in the Dutch coordinate system (Rijksdriehoekstelsel). I tried to remake it in Leaflet in R, but for some reason I only get a grey screen…

Most of these options worked for this different Tilelayer I had, but that one was wanky at certain resolution lvl's so I really need to get this one to work.

library(leaflet)

resolutions <- c(3440.640, 1720.320, 860.160, 430.080, 215.040, 107.520,     53.760, 26.880, 13.440, 6.720, 3.360, 1.680, 0.840, 0.420)


epsg28992 <- leafletCRS(crsClass = 'L.CRS.Simple', code = 'EPSG:28992',
                   proj4def = '+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.4171,50.3319,465.5524,1.9342,-1.6677,9.1019,4.0725 +units=m +no_defs ',
                   resolutions = resolutions,
                 projectedBounds = c(-285401.92, 22598.08, 595401.9199999999, 903401.9199999999))

leaflet(options = leafletOptions( infinite=FALSE,
                              crs = epsg28992, 
                              maxZoom =14)) %>%
setView(5.389,52, zoom = 5) %>%
addTiles('http://geodata.nationaalgeoregister.nl/wmts/?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=opentopoachtergrondkaart&TILEMATRIXSET=EPSG:28992&TILEMATRIX=EPSG:28992:{z}&TILEROW={y}&TILECOL={x}&FORMAT=image/png',
tileOptions(minZoom=0,maxZoom=14))

The proj4def, resolutions, tile, tileOptions, projected bounds, crsClass, and code are all the same… only thing that is different is the Project Unproject part, but I don't know what the equivalent is in R, nor do i think that it is important.

<pre>var kaart = L.map("kaart", {
    crs: L.CRS.RD,
});</pre>
<div id="rd_kaart" style="width: 100%; height: 450px"></div>

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js"></script>

<script type="text/javascript">
    var def = '+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.4171,50.3319,465.5524,1.9342,-1.6677,9.1019,4.0725 +units=m +no_defs ';
    var proj4RD = proj4('WGS84', def);

    var maxZoom = 16; // Alleen de schalen t/m 14 zijn officieel vastgesteld
    var scale = 3440.640;
    var scales = []

    for (var i = 0; i <= maxZoom; i++) {
        scales.push(1 / scale);
        scale /= 2;
    };

    L.Projection.RD = {
        project: function(latlng) {
            var point = proj4RD.forward([latlng.lng, latlng.lat]);
            return new L.Point(point[0], point[1]);
        },
        unproject: function(point) {
            var latlng = proj4RD.inverse([point.x, point.y]);
            return L.latLng(latlng[1], latlng[0]);
        },

        bounds: L.bounds([482.06, 308914.15], [275902.39, 636381.86]), // Begrenzing in RD(?)

        proj4def: def
    };

    L.CRS.RD = L.extend({}, L.CRS.Simple, {
        code: 'EPSG:28992',
        infinite: false,
        projection: L.Projection.RD,
        transformation: new L.Transformation(1, 285401.920, -1, 903401.920),

        scale: function(zoom) {
            return scales[zoom];
        },

        zoom: function(scale) {
            return scales.indexOf(scale);
        },
    });
</script>

<script type="text/javascript">
    var kaart = L.map("rd_kaart", {
        center: [52.156, 5.389],
        zoom: 5,
        crs: L.CRS.RD
    });

    var openbasiskaart = L.tileLayer('http://geodata.nationaalgeoregister.nl/wmts/?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=opentopoachtergrondkaart&TILEMATRIXSET=EPSG:28992&TILEMATRIX=EPSG:28992:{z}&TILEROW={y}&TILECOL={x}&FORMAT=image/png', {
        maxZoom: 14,
        minZoom: 0,
        attribution: 'Kaartgegevens: Instituut Fysieke Veiligheid, bijdragers OpenStreetMap en NLExtract'
    }).addTo(kaart);

</script>

Best Answer

Maybe i didn't understand your question, but it is working with the code from yesterday. You just have to change the link to the new wmts and remove options = tileOptions(tms=TRUE).

So the entire code would look something like this:

library(leaflet)

resolutions <- c(3440.640, 1720.320, 860.160, 430.080, 215.040, 107.520, 53.760, 26.880, 13.440, 6.720, 3.360, 1.680, 0.840, 0.420)

epsg28992 <- leafletCRS(crsClass = 'L.Proj.CRS.TMS', code = 'EPSG:28992',
                       proj4def = '+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.417,50.3319,465.552,-0.398957,0.343988,-1.8774,4.0725 +units=m +no_defs',
                       resolutions = resolutions,
                       projectedBounds = c(-285401.92, 22598.08, 595401.9199999999, 903401.9199999999))

leaflet(options = leafletOptions(
                                  crs = epsg28992, 
                                  minZoom = 0, maxZoom = 14)) %>%
  setView(5.092098, 52.093992, zoom = 4) %>%
  addTiles('http://geodata.nationaalgeoregister.nl/wmts/?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=opentopoachtergrondkaart&TILEMATRIXSET=EPSG:28992&TILEMATRIX=EPSG:28992:{z}&TILEROW={y}&TILECOL={x}&FORMAT=image/png')