TransWikia.com

Leaflet activearea interferes with esri-lealet imagelayer

Geographic Information Systems Asked on January 17, 2021

I am creating a leaflet application that uses various data sources / map layers. I found a great source for global ground cover from esri. It is an imageLayer. Using an esri-leaflet imageLayer, I am able to add this layer to my map. My application will likely need to draw data from various raster layers like this one. I am also able to load an urban-wildland boundary layer as an esri-leaflet featureLayer.

I am also using leaflet-active-area, which is a plugin that shifts the center and bounds of the map to the div of your choosing. I love this plugin for creating a better UI experience.

The featureLayer works just fine in conjunction with the activeArea. However, the imageLayer is having problems. It seems that the imageLayer is calculating what section of the layer should be currently in the map based on the original map bounds, but then squishing the image itself into the activeArea div. Check it out:

enter image description here

Working demo of the problem

I made a codesandbox that shows this problem. There is a ‘toggle activearea’ button there, so you can see how the imageLayer works properly when not using the activearea, but has this issue when using it.

What might be going wrong here? What is it about an imageLayer (or its underlying L.ImageOverlay) that it knows to sample the correct bounds from the entire map, but then it squishes it into the activearea, instead of the total area? I tried using other types of esri layers (L.esri.tiledMapLayer, L.esri.dynamicMapLayer, etc.) but these don’t work as they are not the correct functions to recieve and apply this layer source. I don’t want to give up my activearea, but I’m going to need to apply and ready from raster layers in my application. Ideas?

Thanks for reading.

Edit: altering codesandbox to use public esri imagemaplayer

I had been using an auth-requires esri imagemaplayer in my initial example, which had my personal client_id and client_secret exposed. I changed it to use a public layer for esri so people can still view the problem. Also,

Here is a codesandbox with the patch / fix js file

But thanks to TomazicM for getting to the crux of the matter.

2 Answers

Problem stems from the fact that esri-leaflet image layer is calling Leaflet map getBounds() method, which is modified by leaflet-active-area plugin to return only active area bounds instead of map bounds. As a consequence ESRI image layer is rendered inside active area only.

Brute force method to remedy this is to modify getBounds() method again and in case of ESRI layer use normal getBounds() procedure. To detect that getBounds() method is called from esri-leaflet plugin, I'm using hacking method of checking call stack for esri string. This is dependant on implementation and should be changed accordingly.

So to achieve desired behavior, the following code has to be placed before map definition:

L.Map.include({
  getBounds: function() {
    var trace = new Error().stack;
    if (this._viewport && (trace.search(/esri/i) == 0)) {
      return this.getViewportLatLngBounds();
    } else {
      var bounds = this.getPixelBounds(),
      sw = this.unproject(bounds.getBottomLeft()),
      ne = this.unproject(bounds.getTopRight());
      return new L.LatLngBounds(sw, ne);
    }
  }
});

Correct answer by TomazicM on January 17, 2021

I finally got around to making an npm package to fix this issue. You can check it out here:

esri-leaflet-active-area-patch

Maybe I'll get around to putting in a PR to the esri-leaflet repo one day so that you don't need to use the patch. Until then, its available for the one in a million chance someone else needs it.

Answered by Seth Lutske on January 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP