Skip to content

Update timeslider to support expression-based stylesheets #855

Closed

Description

@jeffreyameyer

Bug & Feature description
Now that our stylesheets have been upgraded to expressions (#775), our timeslider needs to be updated to support this change. I believe the old timeslider was based on filters that are no longer available. (Staging was down, so I could not verify at this time)

In addition, while doing this work, we may want to move to supporting a library for time filtering such as the one described in #840.

Activity

  1. 1ec5 commented on Jul 25, 2024

    @1ec5
    Member

    gramps-project/gramps-web#454 (comment) illustrates the changes that would need to take place. Unfortunately, the leaflet-ohm-timeslider-v2 code is much more complex than all the other time filtering implementations, so it might be more difficult to modify it to support expressions. My guess is that it would go somewhere in here:

    https://github.com/OpenHistoricalMap/leaflet-ohm-timeslider-v2/blob/73e14c116aaa474e30cec2eec33cd0bc8c56a062/leaflet-ohm-timeslider.js#L645-L735

    If we publish a dedicated library for time filtering, I’d suggest starting with one of the simpler implementations and adding special cases (if any) for parity with leaflet-ohm-timeslider-v2.

  2. danrademacher commented on Jul 26, 2024

    @danrademacher

    This might seem like a silly question, but: How do we know that the timeslider in fact needs to be refactored?

    I asked Gregor about this a few days ago, and his conclusion was that the timeslider was already using expressions to filter, regardless of the fact that the underlying stylesheet was not using expressions to style.

    As a way to verify this, I updated the static map style in the Leaflet timeslider demo with the latest version of main.json,, and found that it seems to work fine.

    That demo is here: https://openhistoricalmap.github.io/leaflet-ohm-timeslider-v2/

    I realized there are other desires at play here, such as moving away from Leaflet and modularizing the method of filtering years, but in terms of a blocker to multilingual labels, I don't see a pressing need to refactor the timeslider.

  3. jeffreyameyer commented on Jul 26, 2024

    @jeffreyameyer
    MemberAuthor

    I believe this was thought to be the case, but that thought could be wrong.

    This should be quickly verifiable:
    If the expression-based stylesheet has been deployed to staging, the timeslider there shouldn't work.

    I believe that the expression-based stylesheet has been deployed to staging and by my quick check, the timeslider is working.

    @1ec5 - do you know if there was something else that might trigger errors or if there are some edge cases we might want to doublecheck?

  4. 1ec5 commented on Aug 4, 2024

    @1ec5
    Member

    That’s because the style’s filters haven’t been converted to use expressions yet. For example, this filter on the roads_residential layer is using the legacy syntax:

    [
      "all",
      ["in", "type", "residential", "service", "unclassified"]
    ]

    The equivalent expression would be:

    [
      "all",
      ["in", ["get", "type"], ["residential", "service", "unclassified"]]
    ]

    This code would combine legacy filter syntax into whatever the style layer currently has as a filter. But mixing the legacy filter syntax and an expression-based filter into a single filter is guaranteed to trigger an exception. So yes, I do think the time slider will break once any of the filters is upgraded to expressions.

  5. 1ec5 commented on Aug 6, 2024

    @1ec5
    Member

    In addition, while doing this work, we may want to move to supporting a library for time filtering such as the one described in #840.

    maplibre-gl-dates now supports both the legacy filter syntax and expression-based filters. Once we publish it to NPM, we’ll be able to migrate both ohm-embed-1yr and leaflet-ohm-timeslider-v2 to use it. However, neither project currently uses NPM, so we’d need to rely on a copy of the plugin vendored into ohm-website and hosted on openhistoricalmap.org. It shouldn’t be too difficult to migrate ohm-embed-1yr to NPM. If we can also migrate leaflet-ohm-timeslider-v2 to NPM, it would be much more reusable by other projects.

  6. added a commit that references this issue on Nov 11, 2024
  7. gregallensworth commented on Nov 11, 2024

    @gregallensworth

    See new branch maplibre-expressions-20241111

    The changes required for the mapstyle.js and the timeslider were not about expressions vs functions per se, but about Maplibre 3.6.2's treatment of expressions and literals. The timeslider demo was still written in Mapbox GL JS API 0.54 so first I updated it to Maplibre 3.6.2 to match OHM, and make the testing more representative.

    The error messages boiled down to three sets of changes, one to the style and two to the slider:

    expected one of [==, !=, >, >=, <, <=, in, !in, all, any, none, has, !has, within], "match" found

    It seems that the use of "match" statements in filters, is not supported, or at least its documentation is confusing.

    When matching a value as being among an array of values, these are better written as in or == statements instead, because in means literally what is intended here: this value is among the list of values.

    Some layers using a match to a single value, in which are better written as common ==

    But the big problem was the lack of literal when using an array

    // single value match, use ==
    
    // verbose
    ["match", ["get", "type"], ["cliff"], true, false],
    ["match", ["get", "surface"], ["water"], true, false]
    
    // succinct
    ["==", ["get", "type"], "cliff"],
    ["==", ["get", "surface"], "water"],
    
    // not working in filters, also lacking "literal" so it tries to interpret as an expression
    ["match", ["get", "type"], ["ditch", "drain"], true, false]
    
    // use a "in" statement instead, and don't forget "literal"
    ["in", ["get", "type"], ["literal", ["ditch", "drain"]]],
    

    filters[2][1] expected a string and got an array

    The TimeSlider's placeholder filter added by _addDateFiltersTovectorLayers() was as follows:

    // any ol' thing that is always true
    ['any', ['>', '1', '0']]
    

    But later Maplibre is more strict, and since the intent here was for it to compare literal strings, so I should use literal:

    ['any', ['>', ['literal', '1'], ['literal', '0']]]
    

    This was not caught and reported, but instead was treating either the '1' or '0' as some sort of reference to something else? Maplibre's error reporting is not very helpful sometimes.

    But since this is just a placeholder expression that should always return true, this is even simpler:

    ["==", "nodatefilter", ["literal", "nodatefilter"]]
    
    filter[1][2][1][2]: Cannot compare types 'string' and 'number'.
    

    Simple explanation: See _applyDateFilterToLayer() The decimaldate to be used in the filter is a float number, while the start_decdate and end_decdate are strings. Using to-number seems eadsy... but then again some features lack dates by design, or have a blank value for the attribute, so this will taske a little bit of fussing.

    And here we go:

    const datesubfilter = [
        'all',
        [
            'any',
            ['!', ['has', 'start_decdate']],
            ['<=', ['to-number', ['get', 'start_decdate'], -Infinity], ['literal', deccurrent]]
        ],
        [
            'any',
            ['!', ['has', 'end_decdate']],
            ['>=', ['to-number', ['get', 'end_decdate'], Infinity], ['literal', deccurrent]]
        ],
    ];
    
  8. 1ec5 commented on Nov 11, 2024

    @1ec5
    Member

    It seems that the use of "match" statements in filters, is not supported, or at least its documentation is confusing.

    You can use any valid expression as a filter, but the expression needs to evaluate to a Boolean true or false. As long as the existing filter is an expression, you can only wrap it in an expression, not the legacy filter syntax. That’s why it ends up requiring literal and being stricter about types – none of that was part of the legacy filter syntax.

    The decimaldate to be used in the filter is a float number, while the start_decdate and end_decdate are strings.

    Oh, in that case, I’m unsure how maplibre-gl-dates avoids an error. (That library does some additional gymnastics, since it can’t assume start_decdate and end_decdate are present.) Do you suspect a bug in the SQL statement that generates these fields? Could it be specific to a particular layer?

    Longer-term, we should try to unify the implementation in maplibre-gl-dates so that the embed site and third-party websites can also benefit from any improvements. (It’s available on NPM and jsDelivr too.) In the meantime, if OpenHistoricalMap/leaflet-ohm-timeslider-v2#15 is working, then we’ll need to coordinate a deployment of that at the same time as the upgraded styles, since that PR also removes support for legacy filters.

  9. added a commit that references this issue on Nov 11, 2024
  10. danrademacher commented on Dec 17, 2024

    @danrademacher

    This was merged and deployed here OpenHistoricalMap/ohm-deploy#428

  11. added theissue type on Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions