5#include <rapidjson/document.h>
6#include <rapidjson/stringbuffer.h>
7#include <rapidjson/writer.h>
12class MapFeatureHover {
14 explicit MapFeatureHover(std::string property =
"weather_city_id",
15 std::string prefix =
"weather-city:",
16 std::string flag =
"weather_city_hover")
17 : identityProperty(std::move(property)), identityPrefix(std::move(prefix)), hoverFlag(std::move(flag)) {}
19 bool setData(
const std::string& data) {
21 return select(identity);
24 bool select(
const std::string& candidate) {
25 rapidjson::Document input;
26 input.Parse(featuresData.c_str());
27 rapidjson::Document output;
29 auto& allocator = output.GetAllocator();
30 output.AddMember(
"type",
"FeatureCollection", allocator);
31 rapidjson::Value features(rapidjson::kArrayType);
33 if (!candidate.empty() && !input.HasParseError() && input.IsObject() &&
34 input.HasMember(
"features") && input[
"features"].IsArray()) {
35 for (
const auto& feature : input[
"features"].GetArray()) {
36 if (!feature.IsObject() || !feature.HasMember(
"properties") || !feature[
"properties"].IsObject())
38 const auto& properties = feature[
"properties"];
39 if (!properties.HasMember(identityProperty.c_str()) || !properties[identityProperty.c_str()].IsString() ||
40 candidate != properties[identityProperty.c_str()].GetString() || candidate.rfind(identityPrefix, 0) != 0)
42 rapidjson::Value copy;
43 copy.CopyFrom(feature, allocator);
44 copy[
"properties"].RemoveMember(hoverFlag.c_str());
45 copy[
"properties"].AddMember(rapidjson::Value(hoverFlag.c_str(), allocator), rapidjson::Value(
true), allocator);
46 features.PushBack(copy, allocator);
51 output.AddMember(
"features", features, allocator);
52 rapidjson::StringBuffer buffer;
53 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
54 output.Accept(writer);
55 const std::string next = buffer.GetString();
63 const std::string& data()
const {
return geojson; }
66 std::string identityProperty, identityPrefix, hoverFlag;
67 std::string featuresData = R
"({"type":"FeatureCollection","features":[]})";
69 std::string geojson = R"({"type":"FeatureCollection","features":[]})";