Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Data-driven styling should support arbitrary functions #7860

Description

@ericrwolfe

It should be possible to use D3.js-style blocks/callbacks for representing functions in data-driven styling.

I imagine this might be less performant than static functions since core would have to call back to platform code for every feature rendered, but would be more powerful for defining arbitrary interpolation functions as well as make custom mappings and data joins much easier.

A example in Swift: MGLStyleFunction(withBlock: (feature: MGLFeature, zoomLevel: Double) -> MGLStyleValue)

let highlightedFeatures = Set<String>()
circleLayer.circleColor = MGLStyleFunction(withBlock: { (feature, zoomLevel) in
    let attr = feature.attributes
    if highlightedFeatures.contains(attr["osm_id"]) {
        return MGLStyleValue(rawValue: UIColor.gold) 
    }
    if attr["category"] == "restaurant" {
        return MGLStyleValue(rawValue: UIColor.green)
    }
    return MGLStyleValue(rawValue: UIColor.blue)
})
symbolLayer.textFontSize = MGLStyleFunction(withBlock: { (feature, zoomLevel) in
    let size = round(9.0 + 2 * log10(feature.attributes["score"] as! Double + 1))
    return MGLStyleValue(rawValue: size)
})

This has been discussed before for JS in the following tickets: mapbox/mapbox-gl-style-spec#47, mapbox/mapbox-gl-function#28

Arbitrary function callbacks aren't possible in JS per @lucaswoj because modified styles need to be serialized to JSON so that they can be transferred to worker threads.

In gl-native, we shouldn't have this limitation.

/cc @jfirebaugh @boundsj @1ec5

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CoreThe cross-platform C++ core, aka mbgl

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions