OpenRoadCode
Public API reference for OpenRoadCode
Loading...
Searching...
No Matches
route_weather_style.hpp
1// SPDX-FileCopyrightText: 2026 Mark G. Russell
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include <rapidjson/document.h>
6#include <rapidjson/stringbuffer.h>
7#include <rapidjson/writer.h>
8#include <stdexcept>
9#include <string>
10
12inline std::string withRouteWeatherStyle(const std::string& style) {
13 rapidjson::Document document;
14 document.Parse(style.c_str());
15 if (document.HasParseError() || !document.IsObject() ||
16 !document.HasMember("sources") || !document["sources"].IsObject() ||
17 !document.HasMember("layers") || !document["layers"].IsArray()) {
18 throw std::runtime_error("map style has invalid sources or layers");
19 }
20 rapidjson::Document additions;
21 additions.Parse(R"({
22 "source":{"type":"geojson","data":{"type":"FeatureCollection","features":[]}},
23 "layers":[
24 {"id":"route-weather-points","type":"circle","source":"route-weather",
25 "paint":{"circle-radius":6,"circle-color":"#a879ff",
26 "circle-stroke-color":"#ffffff","circle-stroke-width":2}},
27 {"id":"route-weather-labels","type":"symbol","source":"route-weather",
28 "layout":{"text-field":["get","label"],"text-size":12,
29 "text-font":["KlokanTech Noto Sans CJK Regular"],
30 "text-anchor":"top","text-offset":[0,0.8]},
31 "paint":{"text-color":"#ffffff","text-halo-color":"#141d26","text-halo-width":2}}
32 ]})");
33 auto& allocator = document.GetAllocator();
34 if (!document["sources"].HasMember("route-weather")) {
35 rapidjson::Value source;
36 source.CopyFrom(additions["source"], allocator);
37 document["sources"].AddMember("route-weather", source, allocator);
38 for (const auto& layer : additions["layers"].GetArray()) {
39 rapidjson::Value copy;
40 copy.CopyFrom(layer, allocator);
41 document["layers"].PushBack(copy, allocator);
42 }
43 }
44 rapidjson::StringBuffer buffer;
45 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
46 document.Accept(writer);
47 return buffer.GetString();
48}