@@ -42,7 +42,6 @@ void gazetteer_style_t::clear()
4242 m_main.clear ();
4343 m_names.clear ();
4444 m_extra.clear ();
45- m_metadata.clear ();
4645 m_address.clear ();
4746 m_operator = nullptr ;
4847 m_admin_level = MAX_ADMINLEVEL;
@@ -73,11 +72,6 @@ void gazetteer_style_t::load_style(std::string const &filename)
7372 }
7473}
7574
76- void gazetteer_style_t::set_metadata (const bool enabled)
77- {
78- m_metadata_enabled = enabled;
79- }
80-
8175gazetteer_style_t ::flag_t gazetteer_style_t::parse_flags (std::string const &str)
8276{
8377 flag_t out = 0 ;
@@ -134,6 +128,25 @@ gazetteer_style_t::flag_t gazetteer_style_t::parse_flags(std::string const &str)
134128 return out;
135129}
136130
131+ bool gazetteer_style_t::add_metadata_style_entry (std::string const &key,
132+ std::string const &value)
133+ {
134+ if (key == " osm_version" ) {
135+ m_metadata_fields.set_version (true );
136+ } else if (key == " osm_timestamp" ) {
137+ m_metadata_fields.set_timestamp (true );
138+ } else if (key == " osm_changeset" ) {
139+ m_metadata_fields.set_changeset (true );
140+ } else if (key == " osm_uid" ) {
141+ m_metadata_fields.set_uid (true );
142+ } else if (key == " osm_user" ) {
143+ m_metadata_fields.set_user (true );
144+ } else {
145+ return false ;
146+ }
147+ return true ;
148+ }
149+
137150void gazetteer_style_t::add_style_entry (std::string const &key,
138151 std::string const &value,
139152 gazetteer_style_t ::flag_t flags)
@@ -177,6 +190,13 @@ void gazetteer_style_t::add_style_entry(std::string const &key,
177190 }
178191 }
179192
193+ if (add_metadata_style_entry (key, value)) {
194+ if (!value.empty ()) {
195+ throw std::runtime_error (" Style error. Rules for OSM metadata "
196+ " attributes must have an empty value.\n " );
197+ }
198+ return ;
199+ }
180200 if (value.empty ()) {
181201 m_matcher.emplace_back (key, flags, matcher_t ::MT_KEY);
182202 } else {
@@ -355,45 +375,6 @@ void gazetteer_style_t::process_tags(osmium::OSMObject const &o)
355375 } else if (postcode_fallback && postcode) {
356376 m_main.emplace_back (" place" , " postcode" , SF_MAIN | SF_MAIN_FALLBACK);
357377 }
358-
359- // add metadata fields as tags if enabled
360- if (m_metadata_enabled) {
361- if (o.version ()) {
362- add_metadata_field_num<osmium::object_version_type>(" osm_version" , o.version ());
363- }
364- if (o.uid ()) {
365- add_metadata_field_num<osmium::user_id_type>(" osm_uid" , o.uid ());
366- }
367-
368- if (o.user () && *(o.user ()) != ' \0 ' ) {
369- std::string username = o.user ();
370- add_metadata_field (" osm_user" , std::move (username));
371- }
372-
373- if (o.changeset ()) {
374- add_metadata_field_num<osmium::changeset_id_type>(" osm_changeset" , o.changeset ());
375- }
376-
377- if (o.timestamp ()) {
378- add_metadata_field (" osm_timestamp" , std::move (o.timestamp ().to_iso ()));
379- }
380- }
381- }
382-
383- void gazetteer_style_t::add_metadata_field (const std::string&& field, const std::string&& value) {
384- // We have to work with std::string, not char* because metadata fields converted to char*
385- // would require an allocation on heap and a cleanup at the end.
386- flag_t flag = find_flag (field.c_str (), value.c_str ());
387- if (flag & SF_EXTRA) {
388- m_metadata.emplace_back (std::move (field), std::move (value));
389- }
390- }
391-
392- template <typename T>
393- void gazetteer_style_t::add_metadata_field_num (const std::string&& field, const T value) {
394- // This method is not linked to from outside this class. Therefore, it can stay in the source file.
395- std::string value_str = std::to_string (value);
396- add_metadata_field (std::move (field), std::move (value_str));
397378}
398379
399380void gazetteer_style_t::copy_out (osmium::OSMObject const &o,
@@ -501,15 +482,30 @@ bool gazetteer_style_t::copy_out_maintag(pmaintag_t const &tag,
501482 buffer.finish_hash ();
502483 }
503484 // extra tags
504- if (m_extra.empty () && m_metadata. empty ()) {
485+ if (m_extra.empty () && m_metadata_fields. none ()) {
505486 buffer.add_null_column ();
506487 } else {
507488 buffer.new_hash ();
508489 for (auto const &entry : m_extra) {
509490 buffer.add_hash_elem (entry.first , entry.second );
510491 }
511- for (auto const &entry : m_metadata) {
512- buffer.add_hash_elem (entry.first , entry.second );
492+ if (m_metadata_fields.version () && o.version ()) {
493+ buffer.add_hstore_num_noescape <osmium::object_version_type>(
494+ " osm_version" , o.version ());
495+ }
496+ if (m_metadata_fields.uid () && o.uid ()) {
497+ buffer.add_hstore_num_noescape <osmium::user_id_type>(" osm_uid" , o.uid ());
498+ }
499+ if (m_metadata_fields.user () && o.user () && *(o.user ()) != ' \0 ' ) {
500+ buffer.add_hash_elem (" osm_user" , o.user ());
501+ }
502+ if (m_metadata_fields.changeset () && o.changeset ()) {
503+ buffer.add_hstore_num_noescape <osmium::changeset_id_type>(
504+ " osm_changeset" , o.changeset ());
505+ }
506+ if (m_metadata_fields.timestamp () && o.timestamp ()) {
507+ std::string timestamp = o.timestamp ().to_iso ();
508+ buffer.add_hash_elem_noescape (" osm_timestamp" , timestamp.c_str ());
513509 }
514510 buffer.finish_hash ();
515511 }
0 commit comments