Skip to content

Stylesheet restructure #356

Closed
Closed

Description

@edenh

After working with the stylesheet structure a bit, I think there are a few improvements we can make without losing configuration flexibility.

This is my personal wishlist, which draws from many of the previously discussed ideas in #244, #279, and #276. I'm not sure if this should be the content for a preprocessor, a replacement for the current stylesheet, or a step towards a styling language.

Here is an example to illustrate the idea:

{
    buckets: {
        'streets': {
            'poi': {
                layer: 'poi_label',
                point: true,
                children: {
                    'poi_park': {
                        filter: { 'maki': 'park' }
                        point: true,
                        text: {
                            textField: 'name',
                            path: 'curve',
                            font: 'Open Sans',
                            fontSize: 18
                        }
                    },
                    'poi_other': {
                        filter: { 'maki': ['airport', 'bus'] }
                        point: true,
                    }
                }
            },
            'road': {
                layer: 'road',
                line: true,
                'road_motorway': {
                    filter: { 'class': ['motorway', 'motorway_link'] },
                    line: {
                        cap: 'round',
                        join: 'bevel'
                    }
                },
                'road_main': {
                    filter: {
                        'class': 'main',
                        'oneway': 1
                    },
                    line: {
                        cap: 'round',
                        join: 'bevel'
                    }
                }
            },
            'building': {
                layer: 'building',
                fill: true
                stroke: {
                    cap: 'round',
                    join: 'round'
                }
            },
            'wetland': {
                layer: 'landuse_overlay',
                filter: { 'class': 'wetland' },
                fill: true
            }
        }
    },
    structure: [
        'wetland',
        'poi',
        'poi_park',
        'poi_other',
        { 
            composite: 'road',
            layers: [
                'road_motorway',
                'road_main'
            ] 
        },
        'building'
    ],
    layers: {
        'default': {
            'background': { fillColor: 'black' },
            'poi': {
                fillColor: 'white',
                pointRadius: 5
            },
            'poi_park': {
                pointImage: 'park-18',
                textColor: 'white',
                textTranslate: [40, 0]
            },
            'poi_other': {
                pointImage: 'star-stroked-18'
            },
            'road': {
                opacity: 0.9
            },
            'road_motorway': {
                fillColor: 'white',
                lineWidth: 3,
                strokeColor: 'green',
                strokeWidth: 1,
                transition: {
                    fillColor: { duration: 300 },
                    strokeColor: { 
                        duration: 300,
                        delay: 100
                    }
                }
            },
            'road_main': {
                fillColor: 'white',
                lineWidth: 1
            },
            'building': {
                fillColor: '#333333',
                fillOpacity: 0.8,
                strokeColor: '#555555',
                strokeWidth: 3
            }
        }
    }
}

Buckets

Bucket structure

- Nest buckets under sources, such as streets. This avoids unneeded repetition. - Allow for 'child' buckets that filter the parent bucket. These filters should support multiple dimensions, such as:
filter: {
    'class': ['motorway', 'motorway_link'],
    'oneway': 1
}
  • Specify type using fill: true or line: { cap: 'round', join: 'bevel' }

Multiple types in buckets

Specifying multiple types in a bucket can alleviate a lot of the confusion around line casings and fill strokes, cut down on repeating bucket code, more closely resemble carto patterns, and most importantly, remove the need for multiple layers pointing at a single bucket.

  • Allow for stroke control on fill buckets, which can later be styled in classes, such as:
'building': {
    layer: 'building',
    fill: true
    stroke: {
        cap: 'round',
        join: 'round'
    }
}
  • Allow for points and text to be specified in a single bucket:
'poi_park': {
    filter: { 'maki': 'park' }
    point: true,
    text: {
        textField: 'name',
        path: 'curve',
        font: 'Open Sans',
        fontSize: 18
    }
}

Structure

By baking multiple types in bucket specification and solving for the road casing issue in the class (see layer updates below), there doesn't seem to be a need for multiple layers pointing to a bucket. We can move to a flatter array for the structure:

structure: [
    'wetland',
    'poi',
    'poi_park',
    'poi_other',
    { 
        composite: 'road',
        layers: [
            'road_motorway',
            'road_main'
        ] 
    },
    'building'
]

This requires layer names to match up with bucket names.

Layers

- Rename classes to layers. - Use type-prepended style properties when appropriate. - Every layer has a fillColor property regardless of type. - Images on points use pointImage and tiled patterns on fills use pattern. - Properties such as translate are type-prepended: textTranslate: [40, 0] - Line casings are solved using stroke: strokeWitdth, strokeColor, strokeOpacity, etc. - Transition propertied are grouped like:
transition: {
    fillColor: { duration: 300 },
    strokeColor: { 
        duration: 300,
        delay: 100
    }
}

With these changes, I tried to cover all current functionality while simplifying the process conceptually.

cc @nickidlugash @ansis @mourner @kkaefer @tmcw

Activity

  1. mourner commented on Mar 24, 2014

    @mourner
    Member

    I like these changes a lot. Quick question:

    Line casings are solved using stroke: strokeWidth, strokeColor, strokeOpacity, etc.

    How would you specify the z-order of the casings in the structure? How do you know from the current style example where to render them?

  2. tmcw commented on Mar 24, 2014

    @tmcw
    Contributor

    /cc @sgillies as arbiter of JSON style.

    Random thoughts:

    • The fill/line attributes seem really close to SVG terminology, which is the same as simplestyle-spec but differ just slightly and I'm not sure they match anything else. Same with font - close to CSS, but just a little different. Naming sucks but I'd love to strive for similarity where it's cheap to do.
    • Instead of point: true, could we not have type: 'point'?
    • Is structure what determines z-index and nothing else? Should it be order?
    • Keep in mind that json and javascript objects are unordered, so if we don't have an order element, it'll be risky
    • Hard to tell what the use of class means here
    • The id: {} pattern is concise and guarantees no duplicate ids, but it can be weird to deal with, since you can't easily pull out an element from the object and refer to it after that. Doing more like [{ id: foo, ... }] might be preferable.
    • I'm not convinced we need to support a wide range of color formats here. This is a low-level, strict representation, and simplestyle-spec & kml both specify one valid color encoding.
  3. edenh commented on Mar 24, 2014

    @edenh
    ContributorAuthor

    @mourner it would probably always be an outer-stroke and be drawn on top of the line layer, but having more control there would also be nice: strokePosition: outer/inner/center

    So if lineWidth is 5 and strokeWidth is 1, the 'casing' would be automatically offset with width of the line, instead of drawing a casing at 6 and a line on top at 5.

    The fill/line attributes seem really close to SVG terminology, which is the same as simplestyle-spec but differ just slightly and I'm not sure they match anything else. Same with font - close to CSS, but just a little different. Naming sucks but I'd love to strive for similarity where it's cheap to do.

    I agree. Going with something more similar to SVG has advantages.

    Instead of point: true, could we not have type: 'point'?

    We could, but when defining a line or text type, it would be nice to be able to nest the attributes specific to that type like line: { cap: 'round', join: 'bevel' }, without having to also include line: true. I could see how this may be confusing though.

    Is structure what determines z-index and nothing else? Should it be order?

    Order: 👍

    Hard to tell what the use of class means here

    This is specific field in mb streets: https://a.tiles.mapbox.com/v3/mapbox.mapbox-streets-v4.json
    Not sure how to clarify/name this better.

  4. mourner commented on Mar 24, 2014

    @mourner
    Member

    it would probably always be an outer-stroke and be drawn on top of the line layer, but having more control there would also be nice: strokePosition: outer/inner/center

    @edenh what I mean is that in many cases with several road types you first need to render all the casings of each type, and then render all the lines. You can't just assume casing-road-casing-road everywhere, as far as I understand. @ajashton right?

    Is structure what determines z-index and nothing else? Should it be order?

    @tmcw It also groups some layers into a named composite layer. That's not exactly "order", so I'm not sure.

    Instead of point: true, could we not have type: 'point'?

    We want to specify several types on one bucket. Do you suggest e.g. type: ['line', 'fill']?

  5. tmcw commented on Mar 24, 2014

    @tmcw
    Contributor

    Do you suggest e.g. type: ['line', 'fill']?

    Sure, seems reasonable.

  6. edenh commented on Mar 24, 2014

    @edenh
    ContributorAuthor

    You can't just assume casing-road-casing-road everywhere, as far as I understand.

    That's true. I'm wondering if we could rely on strokes being applied to composited layers in this case.

  7. mourner commented on Mar 24, 2014

    @mourner
    Member

    I'm wondering if we could rely on strokes being applied to composited layers in this case.

    Looks a bit fishy. Besides imagine 4 different road types where we want to render 2 casings, 2 roads, 2 casings, 2 roads. This is why I suggested postfixes here #279 — covers cases like this and allows for fine-grained control.

  8. edenh commented on Mar 25, 2014

    @edenh
    ContributorAuthor

    Yep, #279 is better.

  9. ansis commented on Mar 25, 2014

    @ansis
    Contributor

    I think we need to get rid of the concept of buckets for outside users. We should only have layers and styles. Layers handle z order and data selection. Styles handle everything about appearance.

    @edenh's example would look something like this:

    {
        "layers": [
            {
                "streets": { "source": "mapbox-streets" },
                "layers": [
                {
                    "wetland": { "layer": "landuse" }
                },
                {
                    "poi": { "layer": "poi_label" },
                    "layers": [
                        { "poi_park": { "maki": "park" } },
                        { "poi_other": { "maki": ["airport", "bus"] } }
                    ]
                },
                {
                    "road": { "layer": "road" },
                    "layers": [
                        { "road_motorway_casing": { "class": ["motorway", "motorway_link"] } },
                        { "road_main_casing": { "class": "main", "oneway": 1 } },
                        { "road_motorway": { "class": ["motorway", "motorway_link"] } },
                        { "road_main": { "class": "main", "oneway": 1 } }
                    ]
                },
                {
                    "building": { "layer": "building" }
                }
            ]}
        ],
        "styles": [
            { "": {
                "wetland": {
                    "fill-color": "#00ff00"
                },
                "poi": {
                    "fill-clor": "#ffffff",
                    "point-radius": 5
                },
                "poi_park": {
                    "point-image": "park-18",
                    "text-color": "#ffffff",
                    "text-field": "name",
                    "text-path": "curve",
                    "text-font": "Open Sans",
                    "text-size": 18,
                    "text-translate": [0, 40]
                },
                "poi_other": {
                    "point-image": "start-stroked-18"
                },
                "road": {
                    "opacity": 0,
                    "line-cap": "round",
                    "line-join": "bevel"
                },
                "road_motorway_casing": {
                    "fill-color": "#000000",
                    "line-width": 3
                },
                "road_main_casing": {
                    "fill-color": "#000000",
                    "line-width": 1
                },
                "road_motorway": {
                    "fill-color": "#ffffff",
                    "line-width": 3
                },
                "road_main": {
                    "fill-color": "#ffffff",
                    "line-width": 1
                },
                "building": {
                    "fill-color": "#333333",
                    "fill-opacity": 0.8,
                    "line-color": "#555555",
                    "line-width": 3
                }
            }},
            { "superwideroads": {
                "streets": {
                    "transition-line-width": { "duration": 2000, "delay": 500 }
                },
                "road_motorway_casing": {
                    "line-width": 10
                },
                "road_motorway": {
                    "line-width": 8
                }
            }}
        ]
    }

    Layers

    Each layer looks like this: { "layername": filterobject }. The filter object describes how to select the data for that layer. The key/values in the object would be matched against feature tags. There are two special keys: source and layer. layer is a concept from mapbox vector tiles. source is for llmr sources.

    If a layer object has a layers property, it is a layer group. The layers property is an array of layers. The filters from a layergroup get automatically included in its child layers' filters, to make them more concise.

    Styles

    Styles describe appearance. I think the example explains things fairly well. In the example superwideroads is a class that could be added with map.addClass("superwideroads") to transition motorways to a wider width. Styles on layer groups are included in their children's styles. Some of the style properties used to be found on buckets.

    abstracting away buckets

    Exposing buckets has only one potential "advantage". Forcing more of the underlying details on the designer may help them create less duplicate buckets which may make a noticeable performance impact. But in most cases it should be possible to automatically dedupe and share buckets. In the example above, road_motorway and road_motorway_casing have the same filters and linecaps, which would be automatically detected.

    This style would be processed into an expanded renderer-friendly version with buckets similar to what we have now.

    I think this approach reduces the number of concepts someone needs to know to write a style, and is pretty concise.

  10. edenh commented on Mar 25, 2014

    @edenh
    ContributorAuthor

    I think we need to get rid of the concept of buckets for outside users. We should only have layers and styles. Layers handle z order and data selection. Styles handle everything about appearance.

    👍 looks great

  11. edenh commented on Mar 25, 2014

    @edenh
    ContributorAuthor

    @ansis how would bucket/layer types be defined?

    With the proposed layout, I take it features would be automatically drawn based on the existence of style attributes, such as text-name for text?

  12. mourner commented on Mar 25, 2014

    @mourner
    Member

    @ansis I think this looks great. One minor thing is that we don't need to specify order of the classes, so we can just do "styles": {"myclass": { .... The idea to move all the geometry-specific styles out of bucket config is really great too. Should simplify the style a lot. I also like how you define the global "streets" layer and set a transition on it all at once.

  13. ansis commented on Mar 25, 2014

    @ansis
    Contributor

    I take it features would be automatically drawn based on the existence of style attributes, such as text-name for text?

    Yeah, I think so. Each type of drawing would have its own prefix.

    we don't need to specify order of the classes

    If we created a sortofwideroads class that set motorways to a width of 7, and then did map.addClass('sortofwideroads').addClass('superwideroads') what would be the width of motorways? Would it be based on the order the classes were added? I think css precedence is based on the order rules are defined in the stylesheet.

    define the global "streets" layer and set a transition on it all at once

    There should probably also be a magic layer below everything that could be used for default styles across sources. #327

    We might want to support everything mapnik does for filters (and, or, not, =, !=, <, <=, >, >=, regex), but I'm not sure what that would look like here.

    { "motorway": ["or", ["=", "class", "motorway"], ["=", "class", "motorway_link"]] }

    or maybe?

    { "motorway": "[class]='motorway' or [class]='motorway_link'" }

    not sure

  14. kkaefer commented on Mar 25, 2014

    @kkaefer
    Member
    • Think about adding data/ids to the buffers we create from bucket information so that we can e.g. draw all poi icons in one go. Not sure how we could do this on a generic level. Think a about things like assigning colors/widths/icons/textures/translations based on a feature property. It gets complicated when you add zoom-level/latitude/longitude dependent functions into the mix, e.g. line width that depends on feature property, zoom level, latitude.
    • Mapnik has a boost spirit expression parser for the filters; we could do something similar in JS/native (e.g. use Lua/LuaJIT, parse the expressions ourselves...?). Are we going to support buckets that pull data from multiple datasources or datasource layers? (Think drawing overground roads, tunnels and bridges in one go)
    • I like the line: { ... }, point: { ... } approach. Lets us use the same data for multiple things, e.g. drawing wide outlines around a fill
    • Selecting data independent of layers/stylesheet allows less repetition if you have large/complex filters
    • Color functions might be interesting too, e.g. manipulate the hue or saturation based on zoom level (parks/forests fading in over zoom levels). currently we "fake" this with opacity functions, which is really just a special case for modifying a color component
    • To make it easier conceptually, we could also think about moving all of the geometry-related properties which are currently in buckets to the style, then parse the style and create the appropriate buckets. It's going to be hard to explain why line width is in the style and line-cap in the bucket.
  15. 40 remaining items

  16. tmcw commented on Apr 4, 2014

    @tmcw
    Contributor

    @mourner there is at least one person on the internet who hates each thing on the internet.

  17. springmeyer commented on Apr 4, 2014

    @springmeyer
    Contributor

    String tokenization/expression parsing is complicated. If we can avoid it, we should. Mapnik's use of spirit for the expression parsing adds a lot of complexity and compile time.

    I agree on complexity. Wrt to compile time and c++ specifically I presume we could do better with a fresh start + spirit is oddball because it was implemented before c++11

  18. tmcw commented on Apr 4, 2014

    @tmcw
    Contributor

    Okay, spitballing a simpler json encoding:

    [{
        "and": [
            {"=": ["@class", "motorway_link"]},
            {"=": ["@oneway", "1"]}
        ]
    }]

    One lesson we learned the hard way in carto-land is that a comparison like class = motorway is extremely problematic: is class the column, or motorway? Is motorway a field, a string, or a variable?

    Would strongly advise using sigils or something similar to make that clearer.

  19. edenh commented on Apr 4, 2014

    @edenh
    ContributorAuthor

    Can we just assume 'and' unless something else is specified? Something like:

    "filter": {
        "layer": "waterway",
        "or": [
              {"type": "ditch"},
              {"type": "drain"}
        ]
    }
  20. tmcw commented on Apr 4, 2014

    @tmcw
    Contributor

    @edenh you will always have someone who has a field that is named "and" or "or"

  21. edenh commented on Apr 4, 2014

    @edenh
    ContributorAuthor

    @tmcw

    "filter": {
        "layer": "waterway",
        "$or": [
              {"type": "ditch"},
              {"type": "drain"}
        ]
    }
  22. mourner commented on Apr 7, 2014

    @mourner
    Member

    Another option we have for filters is keeping the string syntax and then parsing it into JSON (however ugly it may be) on the preprocessing step (with a JS script).

  23. mourner commented on Apr 7, 2014

    @mourner
    Member

    FYI there's now a script in the gl-style repo that converts user-friendly styles into renderer-friendly styles. It generates buckets and structure from layers (handling duplicates), detects bucket types and moves bucket-related styles into bucket config.

    Sample output (OSM Bright): https://github.com/mapbox/gl-style/blob/master/test/styles/bright-raw.js

  24. mourner commented on Apr 11, 2014

    @mourner
    Member

    @edenh if we go with the JSON filter approach, I'd go further and assume "or" for any array values, so your example would turn to:

    {
      "layer": "waterway",
      "type": ["ditch", "drain"]
    }

    This way we could simplify most of the filters while still allowing $or and $and.

  25. mourner commented on Apr 11, 2014

    @mourner
    Member

    Looking at how more complex JSON filters would look like, compared to @ansis's first example:

    // string-based
    "(maki == 'park' or maki == 'nope' and maki != 'airport') and maki =~ '.*'"
    
    // JSON
    {
        "$or": [{
            "maki": "park"
        }, {
            "maki": "nope",
            "$not": {"maki": "airport"}
        }],
        "$match": {"maki": ".*"}
    }

    The JSON one looks much more verbose. On the other hand, I don't think complex filters are a common use case — most will be trivial, like here: https://github.com/mapbox/gl-style/blob/master/test/styles/bright-v1.js

  26. mourner commented on Apr 11, 2014

    @mourner
    Member

    OK, I went ahead with simple JSON filters in migrations, we can change this later if needed: mapbox/mapbox-gl-style-spec#10

  27. added a commit that references this issue on Oct 24, 2016
  28. added a commit that references this issue on Jan 11, 2017
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions