Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get times directly from WMS Layer #188

Open
JackLidge opened this issue Apr 1, 2020 · 5 comments
Open

Get times directly from WMS Layer #188

JackLidge opened this issue Apr 1, 2020 · 5 comments

Comments

@JackLidge
Copy link

I'm trying to take some of the functionality defined within the TimeDimension module to do my own time selection code in Leaflet. I've been looking through the source code, and if I run the following in a console on a map which is already initialised:

var tdLayer = L.timeDimension.layer.wms( mapLayer );
tdLayer._requestTimeDimensionFromCapabilities()
var times = tdLayer._availableTimes

Then times is an array of data objects, taken from the time values in the WMS Capabilities layer. However, if I try to put this into a function to do this inside my javascript file for initialising the Leaflet site, like so:

function getLayerTimes ( layer ) {
        var tdLayer = L.timeDimension.layer.wms(layer);
        tdLayer._requestTimeDimensionFromCapabilities();
        var times = tdLayer._availableTimes;
        return times;
        };

This returns the default value for _availableTimes on initialisation of [] instead of a filled array of date objects. Checking this with console logging, it seems that tdLayer has been updated to show all of the times and _availableTimes in the layer is filled, but it is impossible to get _availableTimes out of the layer as a method.

Is there something I'm missing here about getting the _availableTimes array out?

@r1m
Copy link
Contributor

r1m commented Apr 1, 2020

_requestTimeDimensionFromCapabilities is asynchronous. You cannot get the times just after.

If you want, you can register for time changes events :

dLayer._requestTimeDimensionFromCapabilities();
timedimension.on('availabletimeschanged', (e)->{console.log(e)})

This only works if your layer has option updateTimeDimension set to true

@r1m
Copy link
Contributor

r1m commented Apr 1, 2020

If you want to build your own player component, I suggest you to check Player Control code

And demo 3, 10 & 13

@JackLidge
Copy link
Author

I'm more interested in just getting a list of times from the WMS GetCapabilities doc than building a player component in at the moment. I did think it might be an asynchronous function which would explain the _availableTimes array not being maintained outside the function (although after calling _requestTimeDimensionFromCapabilities I can see the filled _availableTimes values, I just can't get them out into a variable).

Is the timedimension you mentioned above invoked by calling L.timeDimension.on() or is it applied directly to the time dimension layer? (In my example at the top tdLayer).

@r1m
Copy link
Contributor

r1m commented Apr 1, 2020

you need to get a reference to your timedimension instance. Either you create it yourself :

var timedimension = L.timeDimension({/*options*/});
timedimension.addTo(map);
// works with `timeDimension:false` in your map options

OR you use the one auto created with timeDimension:true

map.timeDimension.on('availabletimeschanged', function(event) {})

@JackLidge
Copy link
Author

Thanks for the advice. I'm looking into it now, but I can't work out exaclty which bit of code is needed to update the displayed layer with its new time value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants