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" 24const uint Set_Usage::SKELETON = 1; 25const uint Set_Usage::TAGS = 2; 26const uint Set_Usage::GEOMETRY = 4; 27const uint Set_Usage::META = 8; 28const uint Set_Usage::SET_KEY_VALUES = 128; 31bool assert_element_in_context(Error_Output* error_output, 32 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context) 343660212 q if (tree_context != Statement::elem_eval_possible && tree_context != Statement::member_eval_possible) 360 const std::string* func_name = tree_it.function_name(); 380 error_output->add_parse_error((func_name ? *func_name + "(...)" : "Void function") 390 + " must be called in a context where it can evaluate an element", 400 tree_it->line_col.first); 48bool assert_member_in_context(Error_Output* error_output, 49 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context) 5111220 q if (tree_context != Statement::member_eval_possible) 530 const std::string* func_name = tree_it.function_name(); 550 error_output->add_parse_error((func_name ? *func_name + "(...)" : "Void function") 560 + " must be called in a context where it can evaluate an element's member", 570 tree_it->line_col.first); 65Requested_Context& Requested_Context::add_usage(const std::string& set_name, uint usage) 67619147 q for (std::vector< Set_Usage >::iterator it = set_usage.begin(); it != set_usage.end(); ++it) 6911 q if (it->set_name == set_name) 7111 q it->usage |= usage; 76618147 q set_usage.push_back(Set_Usage(set_name, usage)); 81Requested_Context& Requested_Context::add_usage(uint usage_) 831762203 q object_usage |= usage_; 88Requested_Context& Requested_Context::add_role_names() 904335 q role_names_requested = true; 95Requested_Context& Requested_Context::add_user_names() 975139 q user_names_requested = true; 102void Requested_Context::add(const Requested_Context& rhs) 1042057178 q for (std::vector< Set_Usage >::const_iterator rit = rhs.set_usage.begin(); rit != rhs.set_usage.end(); ++rit) 106125859 q for (std::vector< Set_Usage >::iterator it = set_usage.begin(); it != set_usage.end(); ++it) 10870552 q if (it->set_name == rit->set_name) 11070552 q it->usage |= rit->usage; 11555359 q set_usage.push_back(*rit); 1181504178 q object_usage |= rhs.object_usage; 1191504178 q role_names_requested |= rhs.role_names_requested; 1201504178 q user_names_requested |= rhs.user_names_requested; 124void Requested_Context::bind(const std::string& set_name) 126321129 q add_usage(set_name, object_usage); 131void Set_With_Context::prefetch(uint usage, const Set& set, const Statement& stmt, Resource_Manager& rman) 133711217 q Transaction& transaction = *rman.get_transaction(); 136711217 q if (usage & Set_Usage::TAGS) 138298171 q tag_store_nodes = new Tag_Store< Uint32_Index, Node_Skeleton >(transaction); 139298171 q tag_store_nodes->prefetch_all(set.nodes); 141298171 q if (!set.attic_nodes.empty()) 1432721 q tag_store_attic_nodes = new Tag_Store< Uint32_Index, Node_Skeleton >(transaction); 1442721 q tag_store_attic_nodes->prefetch_all(set.attic_nodes); 147298171 q tag_store_ways = new Tag_Store< Uint31_Index, Way_Skeleton >(transaction); 148298171 q tag_store_ways->prefetch_all(set.ways); 150298171 q if (!set.attic_ways.empty()) 1523324 q tag_store_attic_ways = new Tag_Store< Uint31_Index, Way_Skeleton >(transaction); 1533324 q tag_store_attic_ways->prefetch_all(set.attic_ways); 156298171 q tag_store_relations = new Tag_Store< Uint31_Index, Relation_Skeleton >(transaction); 157298171 q tag_store_relations->prefetch_all(set.relations); 159298171 q if (!set.attic_relations.empty()) 1611413 q tag_store_attic_relations = new Tag_Store< Uint31_Index, Relation_Skeleton >(transaction); 1621413 q tag_store_attic_relations->prefetch_all(set.attic_relations); 167117 q tag_store_areas = new Tag_Store< Uint31_Index, Area_Skeleton >(transaction); 168117 q tag_store_areas->prefetch_all(set.areas); 171298171 q tag_store_deriveds = new Tag_Store< Uint31_Index, Derived_Structure >(transaction); 172298171 q tag_store_deriveds->prefetch_all(set.deriveds); 175711217 q if (usage & Set_Usage::GEOMETRY) 17916066 q way_geometry_store = new Way_Geometry_Store(set.ways, stmt, rman); 18016066 q if (!set.attic_ways.empty()) 18176 q attic_way_geometry_store = new Way_Geometry_Store(set.attic_ways, stmt, rman); 18316066 q relation_geometry_store = new Relation_Geometry_Store(set.relations, stmt, rman); 18416066 q if (!set.attic_relations.empty()) 18583 q attic_relation_geometry_store = new Relation_Geometry_Store(set.attic_relations, stmt, rman); 188711217 q if (usage & Set_Usage::META) 190192100 q meta_collector_nodes = new Meta_Collector< Node::Index, Node_Skeleton::Id_Type >( 191192100 q set.nodes, transaction, current_meta_file_properties< Node_Skeleton >()); 193192100 q if (!set.attic_nodes.empty()) 1951312 q meta_collector_attic_nodes = new Attic_Meta_Collector< Node::Index, Node_Skeleton >( 1961312 q set.attic_nodes, transaction, true); 199192100 q meta_collector_ways = new Meta_Collector< Way::Index, Way_Skeleton::Id_Type >( 200192100 q set.ways, transaction, current_meta_file_properties< Way_Skeleton >()); 202192100 q if (!set.attic_ways.empty()) 2041611 q meta_collector_attic_ways = new Attic_Meta_Collector< Way::Index, Way_Skeleton >( 2051611 q set.attic_ways, transaction, true); 208192100 q meta_collector_relations = new Meta_Collector< Relation::Index, Relation_Skeleton::Id_Type >( 209192100 q set.relations, transaction, current_meta_file_properties< Relation_Skeleton >()); 211192100 q if (!set.attic_relations.empty()) 21397 q meta_collector_attic_relations = new Attic_Meta_Collector< Relation::Index, Relation_Skeleton >( 21497 q set.attic_relations, transaction, true); 220Element_With_Context< Node_Skeleton > Set_With_Context::get_context( 221 const Uint32_Index& index, const Node_Skeleton& elem) 225165280019 q delete current_geometry; 226165280019 q current_geometry = new Point_Geometry(::lat(index.val(), elem.ll_lower), ::lon(index.val(), elem.ll_lower)); 228488495474 q const OSM_Element_Metadata_Skeleton< Node_Skeleton::Id_Type >* meta 229488495474 q = (meta_collector_nodes ? meta_collector_nodes->get(index, elem.id) : 0); 230488495474 q return Element_With_Context< Node_Skeleton >(&elem, 231488495474 q tag_store_nodes ? tag_store_nodes->get(index, elem) : 0, 233488495474 q meta, meta && parent ? parent->get_user_name(meta->user_id) : 0); 237Element_With_Context< Attic< Node_Skeleton > > Set_With_Context::get_context( 238 const Uint32_Index& index, const Attic< Node_Skeleton >& elem) 242172904 q delete current_geometry; 243172904 q current_geometry = new Point_Geometry(::lat(index.val(), elem.ll_lower), ::lon(index.val(), elem.ll_lower)); 2459630628 q const OSM_Element_Metadata_Skeleton< Node_Skeleton::Id_Type >* meta 2469630628 q = (meta_collector_attic_nodes ? meta_collector_attic_nodes->get(index, elem.id, elem.timestamp) : 0); 2479630628 q return Element_With_Context< Attic< Node_Skeleton > >(&elem, 2489630628 q tag_store_attic_nodes ? tag_store_attic_nodes->get(index, elem) : 0, 2509630628 q meta, meta && parent ? parent->get_user_name(meta->user_id) : 0); 254Opaque_Geometry* new_opaque_geometry(const std::vector< Quad_Coord >& geometry) 25648033526 q bool is_complete = true; 257486294226 q for (std::vector< Quad_Coord >::const_iterator it = geometry.begin(); it != geometry.end(); ++it) 258438260726 q is_complete &= (it->ll_upper != 0 || it->ll_lower != 0); 26248033526 q std::vector< Point_Double > coords; 263486294226 q for (std::vector< Quad_Coord >::const_iterator it = geometry.begin(); it != geometry.end(); ++it) 264438260726 q coords.push_back(Point_Double(::lat(it->ll_upper, it->ll_lower), ::lon(it->ll_upper, it->ll_lower))); 26548033526 q return new Linestring_Geometry(coords); 2690 Partial_Way_Geometry* pw_geom = new Partial_Way_Geometry(); 2700 for (std::vector< Quad_Coord >::const_iterator it = geometry.begin(); it != geometry.end(); ++it) 2720 if (it->ll_upper != 0 || it->ll_lower != 0) 2730 pw_geom->add_point(Point_Double(::lat(it->ll_upper, it->ll_lower), ::lon(it->ll_upper, it->ll_lower))); 2750 pw_geom->add_point(Point_Double(100., 200.)); 2800 return new Null_Geometry(); 284Opaque_Geometry* new_opaque_geometry(const std::vector< std::vector< Quad_Coord > >& geometry) 286903315 q bool is_complete = true; 287903315 q for (std::vector< std::vector< Quad_Coord > >::const_iterator it = geometry.begin(); 288149560115 q it != geometry.end(); ++it) 29277260015 q else if (it->size() == 1) 2936679614 q is_complete &= ((*it)[0].ll_upper != 0 || (*it)[0].ll_lower != 0); 296743929615 q for (std::vector< Quad_Coord >::const_iterator it2 = it->begin(); it2 != it->end(); ++it2) 297673349215 q is_complete &= (it2->ll_upper != 0 || it2->ll_lower != 0); 303845414 q Compound_Geometry* cp_geom = new Compound_Geometry(); 305845414 q for (std::vector< std::vector< Quad_Coord > >::const_iterator it = geometry.begin(); 30643299514 q it != geometry.end(); ++it) 3090 cp_geom->add_component(new Null_Geometry()); 31042454114 q else if (it->size() == 1) 3115945313 q cp_geom->add_component(new Point_Geometry( 3125945313 q ::lat(it->front().ll_upper, it->front().ll_lower), 3135945313 q ::lon(it->front().ll_upper, it->front().ll_lower))); 31636508814 q std::vector< Point_Double > coords; 317502385314 q for (std::vector< Quad_Coord >::const_iterator it2 = it->begin(); it2 != it->end(); ++it2) 318465876514 q coords.push_back(Point_Double( 319465876514 q ::lat(it2->ll_upper, it2->ll_lower), 320465876514 q ::lon(it2->ll_upper, it2->ll_lower))); 32236508814 q cp_geom->add_component(new Linestring_Geometry(coords)); 32857910 q else if (!geometry.empty()) 33057910 q Partial_Relation_Geometry* pr_geom = new Partial_Relation_Geometry(); 33257910 q for (std::vector< std::vector< Quad_Coord > >::const_iterator it = geometry.begin(); 333106260610 q it != geometry.end(); ++it) 33671396810 q pr_geom->add_placeholder(); 3373480599 q else if (it->size() == 1 && ((*it)[0].ll_upper != 0 || (*it)[0].ll_lower != 0)) 33840018 q pr_geom->add_point(Point_Double( 33940018 q ::lat(it->front().ll_upper, it->front().ll_lower), 34040018 q ::lon(it->front().ll_upper, it->front().ll_lower))); 3433440589 q pr_geom->start_way(); 34424221279 q for (std::vector< Quad_Coord >::const_iterator it2 = it->begin(); it2 != it->end(); ++it2) 34620780699 q if (it2->ll_upper != 0 || it2->ll_lower != 0) 34720747279 q pr_geom->add_way_point(Point_Double( 34820747279 q ::lat(it2->ll_upper, it2->ll_lower), 34920747279 q ::lon(it2->ll_upper, it2->ll_lower))); 35133426 q pr_geom->add_way_placeholder(); 3590 return new Null_Geometry(); 363Element_With_Context< Way_Skeleton > Set_With_Context::get_context( 364 const Uint31_Index& index, const Way_Skeleton& elem) 36847693825 q delete current_geometry; 36947693825 q current_geometry = new_opaque_geometry(way_geometry_store->get_geometry(elem)); 371141558579 q const OSM_Element_Metadata_Skeleton< Way_Skeleton::Id_Type >* meta 372141558579 q = (meta_collector_ways ? meta_collector_ways->get(index, elem.id) : 0); 373141558579 q return Element_With_Context< Way_Skeleton >(&elem, 374141558579 q tag_store_ways ? tag_store_ways->get(index, elem) : 0, 376141558579 q meta, meta && parent ? parent->get_user_name(meta->user_id) : 0); 380Element_With_Context< Attic< Way_Skeleton > > Set_With_Context::get_context( 381 const Uint31_Index& index, const Attic< Way_Skeleton >& elem) 38533974 q delete current_geometry; 38633974 q current_geometry = new_opaque_geometry(attic_way_geometry_store->get_geometry(elem)); 3887699731 q const OSM_Element_Metadata_Skeleton< Way_Skeleton::Id_Type >* meta 3897699731 q = (meta_collector_attic_ways ? meta_collector_attic_ways->get(index, elem.id, elem.timestamp) : 0); 3907699731 q return Element_With_Context< Attic< Way_Skeleton > >(&elem, 3917699731 q tag_store_attic_ways ? tag_store_attic_ways->get(index, elem) : 0, 3937699731 q meta, meta && parent ? parent->get_user_name(meta->user_id) : 0); 397Element_With_Context< Relation_Skeleton > Set_With_Context::get_context( 398 const Uint31_Index& index, const Relation_Skeleton& elem) 402903315 q delete current_geometry; 403903315 q current_geometry = new_opaque_geometry(relation_geometry_store->get_geometry(elem)); 40587695260 q const OSM_Element_Metadata_Skeleton< Relation_Skeleton::Id_Type >* meta 40687695260 q = (meta_collector_relations ? meta_collector_relations->get(index, elem.id) : 0); 40787695260 q return Element_With_Context< Relation_Skeleton >(&elem, 40887695260 q tag_store_relations ? tag_store_relations->get(index, elem) : 0, 41087695260 q meta, meta && parent ? parent->get_user_name(meta->user_id) : 0); 414Element_With_Context< Attic< Relation_Skeleton > > Set_With_Context::get_context( 415 const Uint31_Index& index, const Attic< Relation_Skeleton >& elem) 4190 delete current_geometry; 4200 current_geometry = new_opaque_geometry(attic_relation_geometry_store->get_geometry(elem)); 4224018714 q const OSM_Element_Metadata_Skeleton< Relation_Skeleton::Id_Type >* meta 4234018714 q = (meta_collector_attic_relations ? meta_collector_attic_relations->get(index, elem.id, elem.timestamp) : 0); 4244018714 q return Element_With_Context< Attic< Relation_Skeleton > >(&elem, 4254018714 q tag_store_attic_relations ? tag_store_attic_relations->get(index, elem) : 0, 4274018714 q meta, meta && parent ? parent->get_user_name(meta->user_id) : 0); 431Element_With_Context< Area_Skeleton > Set_With_Context::get_context( 432 const Uint31_Index& index, const Area_Skeleton& elem) 4346588 q return Element_With_Context< Area_Skeleton >(&elem, 4356588 q tag_store_areas ? tag_store_areas->get(index, elem) : 0, 0, 0, 0); 439Element_With_Context< Derived_Skeleton > Set_With_Context::get_context( 440 const Uint31_Index& index, const Derived_Structure& elem) 442227284820 q return Element_With_Context< Derived_Skeleton >(&elem, 443227284820 q tag_store_deriveds ? tag_store_deriveds->get(index, elem) : 0, elem.get_geometry(), 0, 0); 447Prepare_Task_Context::Prepare_Task_Context( 448 const Requested_Context& requested, const Statement& stmt, Resource_Manager& rman) 449466229 q : contexts(requested.set_usage.size()), relation_member_roles_(0), users(0) 4511084229 q for (std::vector< Set_Usage >::const_iterator it = requested.set_usage.begin(); it != requested.set_usage.end(); ++it) 453618147 q Set_With_Context& context = contexts[std::distance(requested.set_usage.begin(), it)]; 454618147 q context.name = it->set_name; 457618147 q const Set* input = rman.get_set(context.name); 459590144 q context.prefetch(it->usage, *input, stmt, rman); 460618147 q if (it->usage & Set_Usage::SET_KEY_VALUES) 4611203 q context.set_key_values = rman.get_set_key_values(context.name); 464466229 q if (requested.role_names_requested) 4653735 q relation_member_roles_ = &relation_member_roles(*rman.get_transaction()); 467466229 q if (requested.user_names_requested) 472Set_With_Context* Prepare_Task_Context::get_set(const std::string& set_name) 474619147 q for (uint i = 0; i < contexts.size(); ++i) 476619147 q if (contexts[i].name == set_name) 483uint32 Prepare_Task_Context::get_role_id(const std::string& role) const 4854034 q if (!relation_member_roles_) 4860 return std::numeric_limits< uint32 >::max(); 4884034 q for (std::map< uint32, std::string >::const_iterator it = relation_member_roles_->begin(); 489543334 q it != relation_member_roles_->end(); ++it) 491543334 q if (it->second == role) 4940 return std::numeric_limits< uint32 >::max(); 498const std::string* Prepare_Task_Context::get_user_name(uint32 user_id) const 502102423626 q std::map< uint32, std::string >::const_iterator it = users->find(user_id); 503102423626 q if (it == users->end())