OpenRoadCode
Public API reference for OpenRoadCode
Loading...
Searching...
No Matches
poi_brand_style.hpp
1// SPDX-FileCopyrightText: 2026 Mark G. Russell
2// SPDX-License-Identifier: MIT
3#pragma once
4#include <rapidjson/document.h>
5#include <rapidjson/stringbuffer.h>
6#include <rapidjson/writer.h>
7#include <string>
8
9inline std::string withPoiBrandStyle(const std::string& style) {
10 rapidjson::Document document;
11 document.Parse(style.c_str());
12 if (!document.IsObject() || !document.HasMember("sources") ||
13 !document["sources"].IsObject() || !document["sources"].HasMember("poi-results") ||
14 !document.HasMember("layers") || !document["layers"].IsArray()) return style;
15 for (const auto& layer : document["layers"].GetArray())
16 if (layer.IsObject() && layer.HasMember("id") && layer["id"].IsString() &&
17 std::string(layer["id"].GetString()) == "poi-results-brand") return style;
18 rapidjson::Document addition;
19 addition.Parse(R"({"id":"poi-results-brand","type":"symbol","source":"poi-results","minzoom":7,
20 "filter":["==",["get","brand_icon_ready"],true],"layout":{"icon-image":["get","brand_icon"],
21 "icon-size":["interpolate",["linear"],["zoom"],7,0.9,11,1.125,15,1.25],
22 "icon-allow-overlap":true,"icon-ignore-placement":true}})");
23 // Insert immediately before the result labels so labels stay legible.
24 auto& allocator = document.GetAllocator();
25 auto& layers = document["layers"];
26 rapidjson::Value result(rapidjson::kArrayType);
27 bool inserted = false;
28 for (const auto& layer : layers.GetArray()) {
29 if (!inserted && layer.IsObject() && layer.HasMember("id") && layer["id"].IsString() &&
30 std::string(layer["id"].GetString()) == "poi-results-label") {
31 rapidjson::Value copy; copy.CopyFrom(addition, allocator);
32 result.PushBack(copy, allocator); inserted = true;
33 }
34 rapidjson::Value copy; copy.CopyFrom(layer, allocator);
35 if (copy.IsObject() && copy.HasMember("id") && copy["id"].IsString()) {
36 const std::string id = copy["id"].GetString();
37 if (id == "poi-results-label" && copy.HasMember("layout") && copy["layout"].IsObject()) {
38 rapidjson::Document offset;
39 offset.Parse(R"(["case",["==",["get","brand_icon_ready"],true],["literal",[0,2.1]],["literal",[0,1.15]]])");
40 rapidjson::Value value; value.CopyFrom(offset, allocator);
41 if (copy["layout"].HasMember("text-offset")) copy["layout"]["text-offset"] = std::move(value);
42 else copy["layout"].AddMember("text-offset", value, allocator);
43 }
44 if (id == "poi-results-badge" || id == "poi-results-glow" || id == "poi-results-icon") {
45 rapidjson::Document fallback;
46 fallback.Parse(R"(["!=",["get","brand_icon_ready"],true])");
47 rapidjson::Value filter;
48 filter.CopyFrom(fallback, allocator);
49 if (copy.HasMember("filter")) {
50 rapidjson::Value combined(rapidjson::kArrayType);
51 combined.PushBack(rapidjson::Value("all", allocator), allocator);
52 combined.PushBack(copy["filter"], allocator);
53 combined.PushBack(filter, allocator);
54 copy["filter"] = std::move(combined);
55 } else copy.AddMember("filter", filter, allocator);
56 }
57 }
58 result.PushBack(copy, allocator);
59 }
60 if (!inserted) { rapidjson::Value copy; copy.CopyFrom(addition, allocator); result.PushBack(copy, allocator); }
61 layers = std::move(result);
62 rapidjson::StringBuffer buffer;
63 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
64 document.Accept(writer);
65 return buffer.GetString();
66}