Thursday, April 13, 2017

ArcGIS tile + dynamic layer with automatic switchover

This was another interesting one to puzzle out, and turned out to be surprisingly elegant once I had wrapped my head around it.

  • The client has an ArcGIS Server with a tile service. We set up a Leaflet map for them showing these tiles, a couple of years back.
  • They now have dynamic services. They are not fast enough for production use at far-out zoom levels, but they are available at deeper scales than the tiles.
  • Can we have the map detect that they've zoomed in far enough, and silently switch between the two? Tiles when far-out and dynamic when close-in?
The answer was a comparatively simple wrapper class:
https://github.com/GreenInfo-Network/L.TileLayer.TiledAndESRIDynamicTandem

This uses the ESRI-Leaflet adapter, and creates within itself both a L.TileLayer and a L.ESRI.DynamicMapLayer. It then watches the map's zoomend event and does what it should do.

Our needs here were pretty specific, so there are a few things it does and does not support. If needs change, or someone else is interested, maybe this could be developed further.
  • It has a setOpacity() method which will apply to both sub-layers. During this I discovered and had to work around some behaviors of the dynamicMapLayer class, e.g. one cannot set its opacity if it is not in the map at that time (one can with L.TileLayer).
  • Options are passed to both layers as-given, e.g. the *attribution* is the same for both. In this case that's perfect.
Maybe this will fit a need of yours, and maybe it won't. But for me, it was a fun and interesting puzzle, and the clients couldn't be happier.