0

I have WMTS map and I'm using proj4leaflet for nonstadard crs. This map has different origins for different zoom levels. First two levels have the same origin, then the next two levels have also the same origin but different from the first two and so on. As consequence every second zoom map not only zooms but also moves.

Here is GetCapabilites request for my wmts service: https://prostor4.gov.si/ows2-gwc-pub/service/wmts?service=wmts&request=GetCapabilities

My layer is SI.GURS.DK:DTK5_D48 and tilematrixset EPSG:3912_DTK5.

The whole code:

var myUrl = 'http://prostor4.gov.si:80/ows2-gwc-pub/service/wmts?&layer=SI.GURS.DK:DTK5_D48&style=default&tilematrixset=EPSG:3912_DTK5&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/png&TileMatrix=EPSG:3912_DTK5:{z}&TileCol={x}&TileRow={y}';

var myLayer = new L.TileLayer(myUrl, {
    maxZoom: 11,
    minZoom: 0
});

var crsResol = [420, 210, 140, 70, 42, 28, 14, 7, 2.8, 1.4, 0.7];

var crs3912 = new L.Proj.CRS('EPSG:3912',
             '+proj=tmerc +lat_0=0 +lon_0=15 +k=0.9999 +x_0=500000 +y_0=-5000000 +ellps=bessel +towgs84=682,-203,480,0,0,0,0 +units=m +no_defs',
             {
               origin: [370210.0, 235895.0],
               resolutions: crsResol
             });

var myMap = new L.Map('map', {
  crs: crs3912
});

myMap.addLayer(myLayer);
myMap.setView([46.047711, 14.507136], 0);

I tried to dynamically change crs attributes (origin and transform) but it didn't help. Is there any solution for this?

  • are the origin changes shown in the tilematrix sent in the getCapabilities? if so Leaflet should be reading and using that information – Ian Turton Dec 21 '18 at 9:05
  • Yes, origins are shown in wmts getCapabilites. I suppose Leaflet would be using them if I would be using leaflet.TileLayer.WMTS plugin. I'm not using it since in the past I was not able to get it working together with proj4leaflet plugin. I'm using standard zyx tile call, modified to wmts syntax. – TomazicM Dec 21 '18 at 17:52
  • That's probably your problem then – Ian Turton Dec 21 '18 at 18:13
  • Definitely my problem, but not in a sense that I created it with my code. – TomazicM Dec 21 '18 at 19:02
  • @IanTurton I cooked up answer to my problem, see below. – TomazicM Dec 25 '18 at 10:12
0

After going through Leaflet and proj4leaflet internals I cooked up answer for my problem. Probably not the most elegant and correct one, but it works.

I set up a hook to zoomstart event where I set crs origin to origin of new zoom. There is one problem here. Info available from the event is only the current zoom, but not the new zoom or zoom direction. To get zoom direction I extended the internal Leaflet method L.Map._resetView so that it gives zoom direction in new L.Map._zoomType property.

Finally, since crs origin is changed before zoom starts, zoom animation does not appear corectly, so I added the option zoomAnimation: false to map definition.

Here ise the final working code:

L.MyMap = L.Map.extend({
  _resetView: function (center, zoom) {
    var newZoom = this._limitZoom(zoom); 

    if (this._zoom !== newZoom)
      this._zoomType = (newZoom > this._zoom) ? 1 : -1;
    else {
      this._zoomType = 0;
    }

    L.Map.prototype._resetView.call(this, center, zoom);
  }
});

var initZoom = 0;

var myUrl = 'http://prostor4.gov.si:80/ows2-gwc-pub/service/wmts?&layer=SI.GURS.DK:DTK5_D48&style=default&tilematrixset=EPSG:3912_DTK5&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/png&TileMatrix=EPSG:3912_DTK5:{z}&TileCol={x}&TileRow={y}';

var myLayer = new L.TileLayer(myUrl, {
    maxZoom: 10,
    minZoom: 0
});

var crsResol = [420, 210, 140, 70, 42, 28, 14, 7, 2.8, 1.4, 0.7];

var zoomOrigin = [
  [370210.0, 235895.0],
  [370210.0, 235895.0],
  [370210.0, 200055.0],
  [370210.0, 200055.0],
  [370210.0, 203639.0],
  [370210.0, 200055.0],
  [370210.0, 196471.0],
  [370210.0, 196471.0],
  [370210.0, 195037.0],
  [370210.0, 195037.0],
  [370210.0, 195037.0]
];

var crs3912 = new L.Proj.CRS('EPSG:3912',
             '+proj=tmerc +lat_0=0 +lon_0=15 +k=0.9999 +x_0=500000 +y_0=-5000000 +ellps=bessel +towgs84=682,-203,480,0,0,0,0 +units=m +no_defs',
             {
               origin: zoomOrigin[initZoom],
               resolutions: crsResol
             });

var myMap = new L.MyMap('map', {
  zoomAnimation: false,
  crs: crs3912
});

myMap.addLayer(myLayer);


myMap.setView([46.047711, 14.507136], initZoom);

myMap.on('zoomstart', function(event) {
  var newZoom = event.target._zoom + event.target._zoomType;
  myMap.options.crs.transformation._b = -zoomOrigin[newZoom][0];
  myMap.options.crs.transformation._d = zoomOrigin[newZoom][1];
});

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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