Skip to content

Commit 830f85c

Browse files
authored
Add AttributeInteger for writing int values (#827)
1 parent 617dcb0 commit 830f85c

12 files changed

+108
-78
lines changed

docs/CONFIGURATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ To do that, you use these methods:
158158
* `Layer(layer_name, is_area)`: write this node/way to the named layer. This is how you put objects in your vector tile. is_area (true/false) specifies whether a way should be treated as an area, or just as a linestring.
159159
* `LayerAsCentroid(layer_name, algorithm, role, role...)`: write a single centroid point for this way to the named layer (useful for labels and POIs). Only the first argument is required. `algorithm` can be "polylabel" (default) or "centroid". The third arguments onwards specify relation roles: if you're processing a multipolygon-type relation (e.g. a boundary) and it has a "label" node member, then by adding "label" as an argument here, this will be used in preference to the calculated point.
160160
* `Attribute(key,value,minzoom)`: add an attribute to the most recently written layer. Argument `minzoom` is optional, use it if you do not want to write the attribute on lower zoom levels.
161-
* `AttributeNumeric(key,value,minzoom)`, `AttributeBoolean(key,value,minzoom)`: for numeric/boolean columns.
161+
* `AttributeNumeric(key,value,minzoom)`, `AttributeInteger(key,value,minzoom)`, `AttributeBoolean(key,value,minzoom)`: for numeric (floating-point), integer and boolean columns.
162162
* `Id()`: get the OSM ID of the current object.
163163
* `IsClosed()`: returns true if the current object is a closed area.
164164
* `IsMultiPolygon()`: returns true if the current object is a multipolygon.

include/attribute_store.h

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,53 +42,57 @@ class AttributeKeyStore {
4242
std::map<const std::string*, uint16_t, string_ptr_less_than> keys2index;
4343
};
4444

45-
enum class AttributePairType: uint8_t { String = 0, Float = 1, Bool = 2 };
45+
enum class AttributePairType: uint8_t { String = 0, Float = 1, Bool = 2, Int = 3 };
4646
// AttributePair is a key/value pair (with minzoom)
4747
#pragma pack(push, 1)
4848
struct AttributePair {
4949
unsigned short keyIndex : 9;
5050
AttributePairType valueType : 2;
5151
uint8_t minzoom : 5; // Support zooms from 0..31. In practice, we expect z16 to be the biggest minzoom.
5252
union {
53-
float floatValue_;
53+
double doubleValue_;
5454
PooledString stringValue_;
5555
};
5656

5757
AttributePair(uint32_t keyIndex, bool value, char minzoom)
58-
: keyIndex(keyIndex), valueType(AttributePairType::Bool), minzoom(minzoom), floatValue_(value ? 1 : 0)
58+
: keyIndex(keyIndex), valueType(AttributePairType::Bool), minzoom(minzoom), doubleValue_(value ? 1 : 0)
5959
{
6060
}
6161
AttributePair(uint32_t keyIndex, const PooledString& value, char minzoom)
6262
: keyIndex(keyIndex), valueType(AttributePairType::String), stringValue_(value), minzoom(minzoom)
6363
{
6464
}
65-
AttributePair(uint32_t keyIndex, float value, char minzoom)
66-
: keyIndex(keyIndex), valueType(AttributePairType::Float), minzoom(minzoom), floatValue_(value)
65+
AttributePair(uint32_t keyIndex, double value, char minzoom)
66+
: keyIndex(keyIndex), valueType(AttributePairType::Float), minzoom(minzoom), doubleValue_(value)
67+
{
68+
}
69+
AttributePair(uint32_t keyIndex, int value, char minzoom)
70+
: keyIndex(keyIndex), valueType(AttributePairType::Int), minzoom(minzoom), doubleValue_(static_cast<double>(value))
6771
{
6872
}
6973

7074
AttributePair(const AttributePair& other):
7175
keyIndex(other.keyIndex), valueType(other.valueType), minzoom(other.minzoom)
7276
{
73-
if (valueType == AttributePairType::Bool || valueType == AttributePairType::Float) {
74-
floatValue_ = other.floatValue_;
77+
if (valueType == AttributePairType::String) {
78+
stringValue_ = other.stringValue_;
7579
return;
7680
}
77-
78-
stringValue_ = other.stringValue_;
81+
82+
doubleValue_ = other.doubleValue_;
7983
}
8084

8185
AttributePair& operator=(const AttributePair& other) {
8286
keyIndex = other.keyIndex;
8387
valueType = other.valueType;
8488
minzoom = other.minzoom;
8589

86-
if (valueType == AttributePairType::Bool || valueType == AttributePairType::Float) {
87-
floatValue_ = other.floatValue_;
90+
if (valueType == AttributePairType::String) {
91+
stringValue_ = other.stringValue_;
8892
return *this;
8993
}
9094

91-
stringValue_ = other.stringValue_;
95+
doubleValue_ = other.doubleValue_;
9296
return *this;
9397
}
9498

@@ -100,30 +104,25 @@ struct AttributePair {
100104
if (valueType != other.valueType) return valueType < other.valueType;
101105

102106
if (hasStringValue()) return pooledString() < other.pooledString();
103-
if (hasBoolValue()) return boolValue() < other.boolValue();
104-
if (hasFloatValue()) return floatValue() < other.floatValue();
105-
throw std::runtime_error("Invalid type in attribute store");
107+
return doubleValue_ < other.doubleValue_;
106108
}
107109

108110
bool operator==(const AttributePair &other) const {
109111
if (minzoom!=other.minzoom || keyIndex!=other.keyIndex || valueType!=other.valueType) return false;
110-
if (valueType == AttributePairType::String)
111-
return stringValue_ == other.stringValue_;
112-
113-
if (valueType == AttributePairType::Float || valueType == AttributePairType::Bool)
114-
return floatValue_ == other.floatValue_;
115-
116-
return true;
112+
if (valueType == AttributePairType::String) return stringValue_ == other.stringValue_;
113+
return doubleValue_ == other.doubleValue_;
117114
}
118115

119116
bool hasStringValue() const { return valueType == AttributePairType::String; }
120117
bool hasFloatValue() const { return valueType == AttributePairType::Float; }
121118
bool hasBoolValue() const { return valueType == AttributePairType::Bool; }
119+
bool hasIntValue() const { return valueType == AttributePairType::Int; }
122120

123121
const PooledString& pooledString() const { return stringValue_; }
124122
const std::string stringValue() const { return stringValue_.toString(); }
125-
float floatValue() const { return floatValue_; }
126-
bool boolValue() const { return floatValue_; }
123+
float floatValue() const { return static_cast<float>(doubleValue_); }
124+
bool boolValue() const { return doubleValue_; }
125+
int intValue() const { return static_cast<int64_t>(doubleValue_); }
127126

128127
void ensureStringIsOwned();
129128

@@ -162,7 +161,9 @@ struct AttributePair {
162161
const char* data = pooledString().data();
163162
boost::hash_range(rv, data, data + pooledString().size());
164163
} else if(hasFloatValue())
165-
boost::hash_combine(rv, floatValue());
164+
boost::hash_combine(rv, doubleValue_);
165+
else if(hasIntValue())
166+
boost::hash_combine(rv, doubleValue_);
166167
else if(hasBoolValue())
167168
boost::hash_combine(rv, boolValue());
168169
else {
@@ -407,7 +408,8 @@ struct AttributeStore {
407408
void finalize();
408409

409410
void addAttribute(AttributeSet& attributeSet, std::string const &key, const protozero::data_view v, char minzoom);
410-
void addAttribute(AttributeSet& attributeSet, std::string const &key, float v, char minzoom);
411+
void addAttribute(AttributeSet& attributeSet, std::string const &key, double v, char minzoom);
412+
void addAttribute(AttributeSet& attributeSet, std::string const &key, int v, char minzoom);
411413
void addAttribute(AttributeSet& attributeSet, std::string const &key, bool v, char minzoom);
412414

413415
AttributeStore():

include/osm_lua_processing.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ class OsmLuaProcessing {
195195

196196
// Set attributes in a vector tile's Attributes table
197197
void Attribute(const std::string &key, const protozero::data_view val, const char minzoom);
198-
void AttributeNumeric(const std::string &key, const float val, const char minzoom);
198+
void AttributeNumeric(const std::string &key, const double val, const char minzoom);
199199
void AttributeBoolean(const std::string &key, const bool val, const char minzoom);
200+
void AttributeInteger(const std::string &key, const int val, const char minzoom);
200201
void MinZoom(const double z);
201202
void ZOrder(const double z);
202203

resources/process-debug.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function node_function()
9393
Layer("place", false)
9494
Attribute("class", place)
9595
MinZoom(mz)
96-
if rank then AttributeNumeric("rank", rank) end
96+
if rank then AttributeInteger("rank", rank) end
9797
SetNameAttributes()
9898
return
9999
end
@@ -107,7 +107,7 @@ function node_function()
107107
if natural == "peak" or natural == "volcano" then
108108
Layer("mountain_peak", false)
109109
SetEleAttributes()
110-
AttributeNumeric("rank", 1)
110+
AttributeInteger("rank", 1)
111111
Attribute("class", natural)
112112
SetNameAttributes()
113113
return
@@ -256,12 +256,12 @@ function way_function()
256256
if linkValues[highway] then
257257
splitHighway = split(highway, "_")
258258
highway = splitHighway[1]
259-
AttributeNumeric("ramp",1)
259+
AttributeInteger("ramp",1)
260260
end
261261

262262
local oneway = Find("oneway")
263263
if oneway == "yes" or oneway == "1" then
264-
AttributeNumeric("oneway",1)
264+
AttributeInteger("oneway",1)
265265
end
266266
if oneway == "-1" then
267267
-- **** TODO
@@ -282,7 +282,7 @@ function way_function()
282282
local ref = Find("ref")
283283
if ref~="" then
284284
Attribute("ref",ref)
285-
AttributeNumeric("ref_length",ref:len())
285+
AttributeInteger("ref_length",ref:len())
286286
end
287287
end
288288

@@ -326,7 +326,7 @@ function way_function()
326326
else
327327
Layer("waterway_detail", false)
328328
end
329-
if Find("intermittent")=="yes" then AttributeNumeric("intermittent", 1) else AttributeNumeric("intermittent", 0) end
329+
if Find("intermittent")=="yes" then AttributeInteger("intermittent", 1) else AttributeInteger("intermittent", 0) end
330330
Attribute("class", waterway)
331331
SetNameAttributes()
332332
SetBrunnelAttributes()
@@ -419,7 +419,7 @@ function way_function()
419419
LayerAsCentroid("poi_detail")
420420
SetNameAttributes()
421421
if write_name then rank=6 else rank=25 end
422-
AttributeNumeric("rank", rank)
422+
AttributeInteger("rank", rank)
423423
end
424424
end
425425

@@ -437,7 +437,7 @@ function WritePOI(obj,class,subclass,rank)
437437
if rank>4 then layer="poi_detail" end
438438
LayerAsCentroid(layer)
439439
SetNameAttributes(obj)
440-
AttributeNumeric("rank", rank)
440+
AttributeInteger("rank", rank)
441441
Attribute("class", class)
442442
Attribute("subclass", subclass)
443443
end

resources/process-example.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function node_function(node)
3838
if amenity~="" then Attribute("class",amenity)
3939
else Attribute("class",shop) end
4040
Attribute("name:latin", Find("name"))
41-
AttributeNumeric("rank", 3)
41+
AttributeInteger("rank", 3)
4242
end
4343

4444
-- Places go to a "place" layer
@@ -48,13 +48,13 @@ function node_function(node)
4848
Attribute("class", place)
4949
Attribute("name:latin", Find("name"))
5050
if place=="city" then
51-
AttributeNumeric("rank", 4)
51+
AttributeInteger("rank", 4)
5252
MinZoom(3)
5353
elseif place=="town" then
54-
AttributeNumeric("rank", 6)
54+
AttributeInteger("rank", 6)
5555
MinZoom(6)
5656
else
57-
AttributeNumeric("rank", 9)
57+
AttributeInteger("rank", 9)
5858
MinZoom(10)
5959
end
6060
end
@@ -86,7 +86,7 @@ function way_function()
8686
if waterway=="stream" or waterway=="river" or waterway=="canal" then
8787
Layer("waterway", false)
8888
Attribute("class", waterway)
89-
AttributeNumeric("intermittent", 0)
89+
AttributeInteger("intermittent", 0)
9090
end
9191

9292
-- Lakes and other water polygons

resources/process-openmaptiles.lua

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ function node_function()
177177
Layer("place", false)
178178
Attribute("class", place)
179179
MinZoom(mz)
180-
if rank then AttributeNumeric("rank", rank) end
181-
if capital then AttributeNumeric("capital", capital) end
180+
if rank then AttributeInteger("rank", rank) end
181+
if capital then AttributeInteger("capital", capital) end
182182
if place=="country" then
183183
local iso_a2 = Find("ISO3166-1:alpha2")
184184
while iso_a2 == "" do
@@ -203,7 +203,7 @@ function node_function()
203203
if natural == "peak" or natural == "volcano" then
204204
Layer("mountain_peak", false)
205205
SetEleAttributes()
206-
AttributeNumeric("rank", 1)
206+
AttributeInteger("rank", 1)
207207
Attribute("class", natural)
208208
SetNameAttributes()
209209
return
@@ -314,15 +314,15 @@ function write_to_transportation_layer(minzoom, highway_class, subclass, ramp, s
314314
if subclass and subclass ~= "" then
315315
Attribute("subclass", subclass)
316316
end
317-
AttributeNumeric("layer", tonumber(Find("layer")) or 0, accessMinzoom)
317+
AttributeInteger("layer", tonumber(Find("layer")) or 0, accessMinzoom)
318318
SetBrunnelAttributes()
319319
-- We do not write any other attributes for areas.
320320
if is_area then
321321
SetMinZoomByAreaWithLimit(minzoom)
322322
return
323323
end
324324
MinZoom(minzoom)
325-
if ramp then AttributeNumeric("ramp",1) end
325+
if ramp then AttributeInteger("ramp",1) end
326326

327327
-- Service
328328
if (is_rail or highway_class == "service") and (service and service ~="") then Attribute("service", service) end
@@ -331,7 +331,7 @@ function write_to_transportation_layer(minzoom, highway_class, subclass, ramp, s
331331
if is_road then
332332
local oneway = Find("oneway")
333333
if oneway == "yes" or oneway == "1" then
334-
AttributeNumeric("oneway",1)
334+
AttributeInteger("oneway",1)
335335
end
336336
if oneway == "-1" then
337337
-- **** TODO
@@ -398,7 +398,7 @@ function way_function()
398398
local pop = tonumber(Find("population")) or 0
399399
local capital = capitalLevel(Find("capital"))
400400
local rank = calcRank(place, pop, nil)
401-
if rank then AttributeNumeric("rank", rank) end
401+
if rank then AttributeInteger("rank", rank) end
402402
SetNameAttributes()
403403
end
404404

@@ -431,14 +431,14 @@ function way_function()
431431
end
432432

433433
Layer("boundary",false)
434-
AttributeNumeric("admin_level", admin_level)
434+
AttributeInteger("admin_level", admin_level)
435435
MinZoom(mz)
436436
-- disputed status (0 or 1). some styles need to have the 0 to show it.
437437
local disputed = Find("disputed")
438438
if disputed=="yes" then
439-
AttributeNumeric("disputed", 1)
439+
AttributeInteger("disputed", 1)
440440
else
441-
AttributeNumeric("disputed", 0)
441+
AttributeInteger("disputed", 0)
442442
end
443443
end
444444

@@ -543,7 +543,7 @@ function way_function()
543543
local ref = Find("ref")
544544
if ref~="" then
545545
Attribute("ref",ref)
546-
AttributeNumeric("ref_length",ref:len())
546+
AttributeInteger("ref_length",ref:len())
547547
end
548548
end
549549
end
@@ -623,7 +623,7 @@ function way_function()
623623
else
624624
Layer("waterway_detail", false)
625625
end
626-
if Find("intermittent")=="yes" then AttributeNumeric("intermittent", 1) else AttributeNumeric("intermittent", 0) end
626+
if Find("intermittent")=="yes" then AttributeInteger("intermittent", 1) else AttributeInteger("intermittent", 0) end
627627
Attribute("class", waterway)
628628
SetNameAttributes()
629629
SetBrunnelAttributes()
@@ -723,7 +723,7 @@ function way_function()
723723
LayerAsCentroid("poi_detail")
724724
SetNameAttributes()
725725
if write_name then rank=6 else rank=25 end
726-
AttributeNumeric("rank", rank)
726+
AttributeInteger("rank", rank)
727727
end
728728
end
729729

@@ -751,17 +751,17 @@ function WritePOI(class,subclass,rank)
751751
if rank>4 then layer="poi_detail" end
752752
LayerAsCentroid(layer)
753753
SetNameAttributes()
754-
AttributeNumeric("rank", rank)
754+
AttributeInteger("rank", rank)
755755
Attribute("class", class)
756756
Attribute("subclass", subclass)
757757
-- layer defaults to 0
758-
AttributeNumeric("layer", tonumber(Find("layer")) or 0)
758+
AttributeInteger("layer", tonumber(Find("layer")) or 0)
759759
-- indoor defaults to false
760760
AttributeBoolean("indoor", (Find("indoor") == "yes"))
761761
-- level has no default
762762
local level = tonumber(Find("level"))
763763
if level then
764-
AttributeNumeric("level", level)
764+
AttributeInteger("level", level)
765765
end
766766
end
767767

0 commit comments

Comments
 (0)