Repository navigation
Description
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 asstreets. 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: trueorline: { 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 afillColor 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.
Activity
mourner commentedon Mar 24, 2014
I like these changes a lot. Quick question:
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?
tmcw commentedon Mar 24, 2014
/cc @sgillies as arbiter of JSON style.
Random thoughts:
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.point: true,could we not havetype: 'point'?structurewhat determines z-index and nothing else? Should it beorder?orderelement, it'll be riskyclassmeans hereid: {}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.edenh commentedon Mar 24, 2014
@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/centerSo 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.
I agree. Going with something more similar to SVG has advantages.
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 includeline: true. I could see how this may be confusing though.Order: 👍
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.
mourner commentedon Mar 24, 2014
@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?
@tmcw It also groups some layers into a named composite layer. That's not exactly "order", so I'm not sure.
We want to specify several types on one bucket. Do you suggest e.g.
type: ['line', 'fill']?tmcw commentedon Mar 24, 2014
Sure, seems reasonable.
edenh commentedon Mar 24, 2014
That's true. I'm wondering if we could rely on strokes being applied to composited layers in this case.
mourner commentedon Mar 24, 2014
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.
edenh commentedon Mar 25, 2014
Yep, #279 is better.
ansis commentedon Mar 25, 2014
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:sourceandlayer.layeris a concept from mapbox vector tiles.sourceis for llmr sources.If a layer object has a
layersproperty, it is a layer group. Thelayersproperty 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_motorwayandroad_motorway_casinghave 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.
edenh commentedon Mar 25, 2014
👍 looks great
edenh commentedon Mar 25, 2014
@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-namefor text?mourner commentedon Mar 25, 2014
@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.ansis commentedon Mar 25, 2014
Yeah, I think so. Each type of drawing would have its own prefix.
If we created a
sortofwideroadsclass that set motorways to a width of 7, and then didmap.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.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
kkaefer commentedon Mar 25, 2014
line: { ... }, point: { ... }approach. Lets us use the same data for multiple things, e.g. drawing wide outlines around a fill40 remaining items
tmcw commentedon Apr 4, 2014
@mourner there is at least one person on the internet who hates each thing on the internet.
springmeyer commentedon Apr 4, 2014
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
tmcw commentedon Apr 4, 2014
Okay, spitballing a simpler json encoding:
One lesson we learned the hard way in carto-land is that a comparison like
class = motorwayis 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.
edenh commentedon Apr 4, 2014
Can we just assume 'and' unless something else is specified? Something like:
tmcw commentedon Apr 4, 2014
@edenh you will always have someone who has a field that is named "and" or "or"
edenh commentedon Apr 4, 2014
@tmcw
mourner commentedon Apr 7, 2014
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).
mourner commentedon Apr 7, 2014
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
mourner commentedon Apr 11, 2014
@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:
This way we could simplify most of the filters while still allowing
$orand$and.mourner commentedon Apr 11, 2014
Looking at how more complex JSON filters would look like, compared to @ansis's first example:
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
mourner commentedon Apr 11, 2014
OK, I went ahead with simple JSON filters in migrations, we can change this later if needed: mapbox/mapbox-gl-style-spec#10
yhahn commentedon Jun 10, 2014
Capturing now in https://github.com/mapbox/mapbox-gl-style-spec
Merge pull request mapbox#356 from mapbox/fix-extra-nodes
Merge pull request #356 from mapbox/update-symbol-desc