1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "../data/tag_store.h" 20#include "../data/utils.h" 25Generic_Statement_Maker< Convert_Statement > Convert_Statement::statement_maker("convert"); 28Convert_Statement::Convert_Statement 29 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings) 3022568 q : Output_Statement(line_number_), geom_evaluator(0), id_evaluator(0), multi_evaluator(0) 3222568 q std::map< std::string, std::string > attributes; 3422568 q attributes["from"] = "_"; 3522568 q attributes["into"] = "_"; 3622568 q attributes["type"] = ""; 3822568 q eval_attributes_array(get_name(), attributes, input_attributes); 4022568 q set_output(attributes["into"]); 4222568 q input = attributes["from"]; 4422568 q if (attributes["type"] == "") 450 add_static_error("The attribute type must be set to a nonempty string."); 4622568 q type = attributes["type"]; 50Convert_Statement::~Convert_Statement() 55void Convert_Statement::add_statement(Statement* statement, std::string text) 5792768 q Set_Prop_Statement* set_prop = dynamic_cast< Set_Prop_Statement* >(statement); 6092768 q if (set_prop->get_key()) 62200168 q for (std::vector< Set_Prop_Statement* >::const_iterator it = evaluators.begin(); it != evaluators.end(); ++it) 64135065 q if ((*it)->get_key() && *(*it)->get_key() == *set_prop->get_key()) 650 add_static_error(std::string("A key cannot be added twice to an element: \"") + *set_prop->get_key() + '\"'); 6827657 q else if (set_prop->get_mode() == Set_Prop_Task::set_id) 7114746 q id_evaluator = set_prop; 730 add_static_error("A convert statement can have at most one set-prop statement of subtype setting the id."); 7512935 q else if (set_prop->get_mode() == Set_Prop_Task::set_geometry) 787224 q geom_evaluator = set_prop; 800 add_static_error("A make statement can have at most one set-prop statement of subtype setting the geometry."); 825719 q else if (!multi_evaluator) 835719 q multi_evaluator = set_prop; 850 add_static_error("A convert statement can have at most one any-key set-prop statement."); 8692768 q evaluators.push_back(set_prop); 890 substatement_error(get_name(), statement); 93template< typename Index, typename Maybe_Attic > 94void generate_elems(const std::string& set_name, 95 const std::map< Index, std::vector< Maybe_Attic > >& items, Set_With_Context& context_from, 96 Owning_Array< Set_Prop_Task* >& tasks, const std::vector< std::string >& declared_keys, 97 Set& into, Resource_Manager& rman, const std::string& type) 9931461 q for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator it_idx = items.begin(); 1005772161 q it_idx != items.end(); ++it_idx) 1025740758 q for (typename std::vector< Maybe_Attic >::const_iterator it_elem = it_idx->second.begin(); 10354313258 q it_elem != it_idx->second.end(); ++it_elem) 10548572557 q Derived_Structure result(type, 0ull); 10648572557 q bool id_fixed = false; 108288509557 q for (uint i = 0; i < tasks.size(); ++i) 111239937057 q tasks[i]->process(context_from.get_context(it_idx->first, *it_elem), declared_keys, result, id_fixed); 1157141822 q result.id = rman.get_global_settings().dispense_derived_id(); 11748572557 q into.deriveds[Uint31_Index(0u)].push_back(result); 123void Convert_Statement::execute(Resource_Manager& rman) 1257462 q std::vector< std::string > declared_keys; 12637262 q for (std::vector< Set_Prop_Statement* >::const_iterator it = evaluators.begin(); it != evaluators.end(); ++it) 12920962 q declared_keys.push_back(*(*it)->get_key()); 1317462 q std::sort(declared_keys.begin(), declared_keys.end()); 1327462 q declared_keys.erase(std::unique(declared_keys.begin(), declared_keys.end()), declared_keys.end()); 1347462 q Requested_Context requested_context; 13537262 q for (std::vector< Set_Prop_Statement* >::const_iterator it = evaluators.begin(); it != evaluators.end(); ++it) 13629862 q requested_context.add((*it)->request_context()); 1377462 q requested_context.bind(input); 1397462 q Prepare_Task_Context context(requested_context, *this, rman); 1417462 q Owning_Array< Set_Prop_Task* > tasks; 14237262 q for (std::vector< Set_Prop_Statement* >::const_iterator it = evaluators.begin(); it != evaluators.end(); ++it) 14329862 q tasks.push_back((*it)->get_task(context, declared_keys)); 1467462 q Set_With_Context* context_from = context.get_set(input); 1487462 q if (context_from && context_from->base) 1507361 q generate_elems< Uint32_Index, Node_Skeleton >( 1517361 q context_from->name, context_from->base->nodes, *context_from, tasks, 1527361 q declared_keys, into, rman, type); 1537361 q if (!context_from->base->attic_nodes.empty()) 15466 q generate_elems< Uint32_Index, Attic< Node_Skeleton > >( 15566 q context_from->name, context_from->base->attic_nodes, *context_from, tasks, 15666 q declared_keys, into, rman, type); 1577361 q generate_elems< Uint31_Index, Way_Skeleton >( 1587361 q context_from->name, context_from->base->ways, *context_from, tasks, 1597361 q declared_keys, into, rman, type); 1607361 q if (!context_from->base->attic_ways.empty()) 16166 q generate_elems< Uint31_Index, Attic< Way_Skeleton > >( 16266 q context_from->name, context_from->base->attic_ways, *context_from, tasks, 16366 q declared_keys, into, rman, type); 1647361 q generate_elems< Uint31_Index, Relation_Skeleton >( 1657361 q context_from->name, context_from->base->relations, *context_from, tasks, 1667361 q declared_keys, into, rman, type); 1677361 q if (!context_from->base->attic_relations.empty()) 16854 q generate_elems< Uint31_Index, Attic< Relation_Skeleton > >( 16954 q context_from->name, context_from->base->attic_relations, *context_from, tasks, 17054 q declared_keys, into, rman, type); 1717361 q if (!context_from->base->areas.empty()) 17255 q generate_elems< Uint31_Index, Area_Skeleton >( 17355 q context_from->name, context_from->base->areas, *context_from, tasks, 17455 q declared_keys, into, rman, type); 1757361 q generate_elems< Uint31_Index, Derived_Structure >( 1767361 q context_from->name, context_from->base->deriveds, *context_from, tasks, 1777361 q declared_keys, into, rman, type); 1807462 q transfer_output(rman, into); 1817462 q rman.health_check(*this);