Reading-eScience-Centre / leaflet-coverage

A Leaflet plugin for visualizing coverages
https://covjson.org/playground/
Other
10 stars 6 forks source link

When Modifying existing CoverageJSON and re-creating layer, only original CoverageJSON layer loads #11

Closed ShaneMill1 closed 3 years ago

ShaneMill1 commented 3 years ago

Hello all,

At the National Weather Service, we are implementing the OGC Environmental Data Retrieval API which outputs CoverageJSON.

We are developing a client that will make a data query from the EDR-API, and is able to successfully plot the data using the leaflet-coverage library.

Now, based on a threshold, we want to replace the data within the CoverageJSON that doesn't meet the threshold with "null". We seem to be able to create an updated CoverageJSON to do this.

The problem is when we try to plot the new CoverageJSON, the old layer appears, not the new updated data. My instinct is that the original layer has the name "Grid" and the new layer also has the name "Grid", so maybe it is taking cache from the older layer.

Has anyone experienced this before, or what are the best practices for deleting an old layer completely and adding a new layer. I have tried the leaflet removeLayer function, tried deleting all layers, tried recreating the map altogether, and the layer never updates.

jonblower commented 3 years ago

Hi @ShaneMill1 - sorry for the slow reply to this, which does sound like a bug. I think we might have to wait until @guygriffiths is back from leave before getting a proper answer, apologies

guygriffiths commented 3 years ago

Apologies, I hadn't seen this. For some reason I wasn't getting notifications for this repository - probably because I never actually contributed to it? Anyway, I can certainly take a look. @ShaneMill1 - do you have some sample code you could share which demonstrates this issue?

ShaneMill1 commented 3 years ago

@guygriffiths Thanks a lot for the response! I created the following codepen that replicates the issue we are having elsewhere. It attempts to alter the data but adding a new data array into the CoverageJSON when clicking the button above the map:

https://codepen.io/shanemill1/pen/VwbReXg

It may be that I am not altering the data as intended? Any feedback would be very much appreciated!

guygriffiths commented 3 years ago

@ShaneMill1 - The issue is that the leaflet coverage plugin caches the values, so when you simply modify the existing object, it still reads from the cache (which is at covJSON.ranges.temperature._ndarr.data) when plotting. So your choices are:

However, if you do the latter, your example will still not work because you are adding a new layer on top of the existing one (so both layers will be visible). You need to remove that layer first.

You can see an adjusted working version here: https://codepen.io/guygriffiths/pen/YzQoYxB This extracts the fetch of the data into a function, then uses that twice (once initially, then once again when it removes the values), and calls the necessary layer-removing code.

Hope this helps.

ShaneMill1 commented 3 years ago

Thank you for the clarification!! This is very very helpful. I will give this a try and confirm the approach on me end. Thanks again!