add token replacement system

fixes #212
This commit is contained in:
Konstantin Käfer
2014-05-30 21:05:25 +02:00
parent da29a16370
commit c5fd7a0cdd
2 changed files with 49 additions and 28 deletions

View File

@@ -266,7 +266,7 @@ module.exports = {
"field": "index",
"value": 10,
"path": "curve",
"text_field": "ele",
"text_field": "{{ele}}m",
"font": "Open Sans Regular, Arial Unicode MS Regular",
"fontSize": 10,
"feature_type": "line",
@@ -647,7 +647,7 @@ module.exports = {
"country_label": {
"source": "outdoors",
"layer": "country_label",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"font": "Open Sans Bold, Arial Unicode MS Bold",
"fontSize": 24,
@@ -664,7 +664,7 @@ module.exports = {
"layer": "marine_label",
"feature_type": "line",
"type": "text",
"text_field": "name",
"text_field": "{{name}}",
"path": "curve",
"font": "Open Sans Italic, Arial Unicode MS Regular",
"fontSize": 16
@@ -674,7 +674,7 @@ module.exports = {
"layer": "marine_label",
"feature_type": "point",
"type": "text",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"field": "labelrank",
"value": 1,
@@ -686,7 +686,7 @@ module.exports = {
"layer": "marine_label",
"feature_type": "point",
"type": "text",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"field": "labelrank",
"value": 2,
@@ -698,7 +698,7 @@ module.exports = {
"layer": "marine_label",
"feature_type": "point",
"type": "text",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"field": "labelrank",
"value": [3,4,5,6],
@@ -708,7 +708,7 @@ module.exports = {
"state_label": {
"source": "outdoors",
"layer": "state_label",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"font": "Open Sans Regular, Arial Unicode MS Regular",
"fontSize": 16,
@@ -721,7 +721,7 @@ module.exports = {
"layer": "place_label",
"field": "type",
"value": "city",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"font": "Open Sans Semibold, Arial Unicode MS Bold",
"fontSize": 20,
@@ -733,7 +733,7 @@ module.exports = {
"layer": "place_label",
"field": "type",
"value": "town",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"font": "Open Sans Semibold, Arial Unicode MS Bold",
"fontSize": 24,
@@ -745,7 +745,7 @@ module.exports = {
"layer": "place_label",
"field": "type",
"value": "village",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"font": "Open Sans Semibold, Arial Unicode MS Bold",
"fontSize": 22,
@@ -761,7 +761,7 @@ module.exports = {
"suburb",
"neighbourhood"
],
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"font": "Open Sans Semibold, Arial Unicode MS Bold",
"fontSize": 18,
@@ -774,7 +774,7 @@ module.exports = {
"layer": "road_label",
"field": "class",
"value": ["motorway","main"],
"text_field": "name",
"text_field": "{{name}}",
"path": "curve",
"padding": 2,
"font": "Open Sans Regular, Arial Unicode MS Regular",
@@ -788,7 +788,7 @@ module.exports = {
"layer": "road_label",
"field": "class",
"value": ["street","street_limited"],
"text_field": "name",
"text_field": "{{name}}",
"path": "curve",
"padding": 2,
"font": "Open Sans Regular, Arial Unicode MS Regular",
@@ -802,7 +802,7 @@ module.exports = {
"layer": "road_label",
"field": "class",
"value": ["service","driveway","path"],
"text_field": "name",
"text_field": "{{name}}",
"path": "curve",
"padding": 2,
"font": "Open Sans Regular, Arial Unicode MS Regular",
@@ -814,7 +814,7 @@ module.exports = {
"water_label": {
"source": "outdoors",
"layer": "water_label",
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"font": "Open Sans Semibold Italic, Arial Unicode MS Bold",
"fontSize": 12,
@@ -824,7 +824,7 @@ module.exports = {
"waterway_label": {
"source": "outdoors",
"layer": "waterway_label",
"text_field": "name",
"text_field": "{{name}}",
"path": "curve",
"font": "Open Sans Semibold Italic, Arial Unicode MS Bold",
"fontSize": 12,
@@ -876,7 +876,7 @@ module.exports = {
1,
2
],
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"padding": 2,
"maxWidth": 8,
@@ -892,7 +892,7 @@ module.exports = {
"layer": "poi_label",
"field": "scalerank",
"value": 3,
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"padding": 2,
"maxWidth": 8,
@@ -908,7 +908,7 @@ module.exports = {
"layer": "poi_label",
"field": "scalerank",
"value": 4,
"text_field": "name",
"text_field": "{{name}}",
"path": "horizontal",
"padding": 2,
"maxWidth": 8,

View File

@@ -16,6 +16,8 @@
#include <llmr/util/std.hpp>
#include <llmr/util/utf.hpp>
#include <regex>
using namespace llmr;
TileParser::TileParser(const std::string &data, VectorTileData &tile,
@@ -182,6 +184,9 @@ std::unique_ptr<Bucket> TileParser::createTextBucket(const VectorTileLayer &laye
tile.textVertexBuffer, tile.triangleElementsBuffer, bucket_desc, placement);
util::utf8_to_utf32 ucs4conv;
std::regex token_regex("\\{\\{(\\w+)\\}\\}");
const auto tokens_end = std::sregex_token_iterator();
std::vector<std::pair<std::u32string, pbf>> labels;
@@ -195,18 +200,34 @@ std::unique_ptr<Bucket> TileParser::createTextBucket(const VectorTileLayer &laye
return nullptr;
VectorTileFeature feature{feature_pbf, layer};
auto it_prop = feature.properties.find(bucket_desc.geometry.field);
if (it_prop == feature.properties.end()) {
// feature does not have the correct property
if (debug::labelTextMissingWarning) {
fprintf(stderr,
"[WARNING] feature doesn't have property '%s' required for labelling\n",
bucket_desc.geometry.field.c_str());
const std::string &field = bucket_desc.geometry.field;
std::string source_string;
source_string.reserve(field.size());
bool token = false;
for (auto token_it = std::sregex_token_iterator(field.begin(), field.end(), token_regex, {-1, 1}); token_it != tokens_end; ++token_it, token = !token) {
if (!token_it->matched) {
continue;
}
if (token) {
auto it_prop = feature.properties.find(token_it->str());
if (it_prop == feature.properties.end()) {
// feature does not have the correct property
if (debug::labelTextMissingWarning) {
fprintf(stderr,
"[WARNING] feature doesn't have property '%s' required for labelling\n",
token_it->str().c_str());
}
continue;
}
source_string += toString(it_prop->second);
} else {
source_string += token_it->str();
}
continue;
}
const std::u32string string = ucs4conv.convert(toString(it_prop->second));
const std::u32string string = ucs4conv.convert(source_string);
// Loop through all characters of this text and collect unique codepoints.
for (uint32_t chr : string) {