Skip to content

Style layers by data

A layer property contains a constant or an expression. An expression reads each feature’s data, allowing one rule to style every feature. A property value belongs to the loaded style. Set it again after every later style load, as Load a style describes.

Style properties use style-spec JSON values. A constant is a scalar, and an expression is an array whose first element names the operator. The remaining elements are its arguments. The C API accepts the complete UTF-8 JSON value as one buffer view.

style-by-data.c
static mln_buffer_view view(const char* text) {
return (mln_buffer_view){.data = text, .size = strlen(text)};
}

["get", "mag"] reads the feature property named mag, and ["linear"] names the interpolation curve. The snippet embeds both in the complete expression.

style-by-data.c
const char radius[] =
"[\"interpolate\",[\"linear\"],[\"get\",\"mag\"],1,4,6,24]";

The stop pairs scale a circle from 4 pixels at magnitude 1 to 24 pixels at magnitude 6. A buffer view carries the expression without rebuilding its nodes at the C boundary.

style-by-data.c
const mln_buffer_view radius_json = view(radius);

Setting a property takes one layer and one property name, and the name is the one that the style specification defines for that layer’s type.

style-by-data.c
const mln_status status = mln_map_set_layer_property(
map, view(layer_id), view("circle-radius"), radius_json
);
if (status != MLN_STATUS_OK) return status;

A filter is a boolean expression that selects the source features to draw. It uses the same value representation as a style property. An empty filter clears the layer’s filter. The snippet passes the complete filter expression.

style-by-data.c
// [">=", ["get", "mag"], 2.5]
const mln_buffer_view filter = view("[\">=\",[\"get\",\"mag\"],2.5]");
return mln_map_set_layer_filter(map, view(layer_id), &filter);

The host owns the byte storage. MapLibre parses or copies a property value or filter before the call returns.

Both calls run on the map’s owner thread and apply to the live style immediately. A host that pumps the runtime and renders on update draws the new appearance in its next frame.