0

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/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/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>

1 Answer 1

3

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')
4
  • This code works great! I do not get the setview to work properly. The zoom levels work but it does not matter what inpout I set for setview(). My ceter point is always above the map. Any idea how that would work?
    – Jelle
    Commented Aug 29, 2018 at 22:07
  • It looks like the tileserver from the example is no longer working. I don't speak dutch and i cant find a working link on their website. If you can provide me a functioning link i can look into the issue Commented Sep 3, 2018 at 16:26
  • Thanks for your reaction. it actually does. I've copied the code and it does work. Using 'addMouseCoordinates()' from the mapview library I figured out that the coordinates on the map are completely different that what they should be. This is the reason why my setView() did not work. Adjusting the origin adjusts the coordinates but I cannot find what the correct origin coordinates would be to get the correct lat lng coordinates. Any idea how I can figure out the correct origin?
    – Jelle
    Commented Sep 6, 2018 at 12:26
  • One addition, I use the ‘brtachtergrondkaart’ as layer name, instead of the ‘opentopoachtergrondkaart’. Both layers still work.
    – Jelle
    Commented Sep 6, 2018 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.