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) 343368195 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) 518816 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) 67437141 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; 76436141 q set_usage.push_back(Set_Usage(set_name, usage)); 81Requested_Context& Requested_Context::add_usage(uint usage_) 831466187 q object_usage |= usage_; 88Requested_Context& Requested_Context::add_role_names() 903831 q role_names_requested = true; 95Requested_Context& Requested_Context::add_user_names() 975843 q user_names_requested = true; 102void Requested_Context::add(const Requested_Context& rhs) 1041677168 q for (std::vector< Set_Usage >::const_iterator rit = rhs.set_usage.begin(); rit != rhs.set_usage.end(); ++rit) 106107457 q for (std::vector< Set_Usage >::iterator it = set_usage.begin(); it != set_usage.end(); ++it) 10866850 q if (it->set_name == rit->set_name) 11066850 q it->usage |= rit->usage; 11540657 q set_usage.push_back(*rit); 1181271168 q object_usage |= rhs.object_usage; 1191271168 q role_names_requested |= rhs.role_names_requested; 1201271168 q user_names_requested |= rhs.user_names_requested; 124void Requested_Context::bind(const std::string& set_name) 126316121 q add_usage(set_name, object_usage); 131void Set_With_Context::prefetch(uint usage, const Set& set, const Statement& stmt, Resource_Manager& rman) 133522202 q Transaction& transaction = *rman.get_transaction(); 136522202 q if (usage & Set_Usage::TAGS) 138282152 q tag_store_nodes = new Tag_Store< Uint32_Index, Node_Skeleton >(transaction); 139282152 q tag_store_nodes->prefetch_all(set.nodes); 141282152 q if (!set.attic_nodes.empty()) 1432219 q tag_store_attic_nodes = new Tag_Store< Uint32_Index, Node_Skeleton >(transaction); 1442219 q tag_store_attic_nodes->prefetch_all(set.attic_nodes); 147282152 q tag_store_ways = new Tag_Store< Uint31_Index, Way_Skeleton >(transaction); 148282152 q tag_store_ways->prefetch_all(set.ways); 150282152 q if (!set.attic_ways.empty()) 1522420 q tag_store_attic_ways = new Tag_Store< Uint31_Index, Way_Skeleton >(transaction); 1532420 q tag_store_attic_ways->prefetch_all(set.attic_ways); 156282152 q tag_store_relations = new Tag_Store< Uint31_Index, Relation_Skeleton >(transaction); 157282152 q tag_store_relations->prefetch_all(set.relations); 159282152 q if (!set.attic_relations.empty()) 1611712 q tag_store_attic_relations = new Tag_Store< Uint31_Index, Relation_Skeleton >(transaction); 1621712 q tag_store_attic_relations->prefetch_all(set.attic_relations); 167107 q tag_store_areas = new Tag_Store< Uint31_Index, Area_Skeleton >(transaction); 168107 q tag_store_areas->prefetch_all(set.areas); 171282152 q tag_store_deriveds = new Tag_Store< Uint31_Index, Derived_Structure >(transaction); 172282152 q tag_store_deriveds->prefetch_all(set.deriveds); 175522202 q if (usage & Set_Usage::GEOMETRY) 17915164 q way_geometry_store = new Way_Geometry_Store(set.ways, stmt, rman); 18015164 q if (!set.attic_ways.empty()) 18177 q attic_way_geometry_store = new Way_Geometry_Store(set.attic_ways, stmt, rman); 18315164 q relation_geometry_store = new Relation_Geometry_Store(set.relations, stmt, rman); 18415164 q if (!set.attic_relations.empty()) 185105 q attic_relation_geometry_store = new Relation_Geometry_Store(set.attic_relations, stmt, rman); 188522202 q if (usage & Set_Usage::META) 19021897 q meta_collector_nodes = new Meta_Collector< Node::Index, Node_Skeleton::Id_Type >( 19121897 q set.nodes, transaction, current_meta_file_properties< Node_Skeleton >()); 19321897 q if (!set.attic_nodes.empty()) 19598 q meta_collector_attic_nodes = new Attic_Meta_Collector< Node::Index, Node_Skeleton >( 19698 q set.attic_nodes, transaction, true); 19921897 q meta_collector_ways = new Meta_Collector< Way::Index, Way_Skeleton::Id_Type >( 20021897 q set.ways, transaction, current_meta_file_properties< Way_Skeleton >()); 20221897 q if (!set.attic_ways.empty()) 2041713 q meta_collector_attic_ways = new Attic_Meta_Collector< Way::Index, Way_Skeleton >( 2051713 q set.attic_ways, transaction, true); 20821897 q meta_collector_relations = new Meta_Collector< Relation::Index, Relation_Skeleton::Id_Type >( 20921897 q set.relations, transaction, current_meta_file_properties< Relation_Skeleton >()); 21121897 q if (!set.attic_relations.empty()) 213129 q meta_collector_attic_relations = new Attic_Meta_Collector< Relation::Index, Relation_Skeleton >( 214129 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) 22540197424 q delete current_geometry; 22640197424 q current_geometry = new Point_Geometry(::lat(index.val(), elem.ll_lower), ::lon(index.val(), elem.ll_lower)); 228242296383 q const OSM_Element_Metadata_Skeleton< Node_Skeleton::Id_Type >* meta 229242296383 q = (meta_collector_nodes ? meta_collector_nodes->get(index, elem.id) : 0); 230242296383 q return Element_With_Context< Node_Skeleton >(&elem, 231242296383 q tag_store_nodes ? tag_store_nodes->get(index, elem) : 0, 233242296383 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) 24238605 q delete current_geometry; 24338605 q current_geometry = new Point_Geometry(::lat(index.val(), elem.ll_lower), ::lon(index.val(), elem.ll_lower)); 2451603128 q const OSM_Element_Metadata_Skeleton< Node_Skeleton::Id_Type >* meta 2461603128 q = (meta_collector_attic_nodes ? meta_collector_attic_nodes->get(index, elem.id, elem.timestamp) : 0); 2471603128 q return Element_With_Context< Attic< Node_Skeleton > >(&elem, 2481603128 q tag_store_attic_nodes ? tag_store_attic_nodes->get(index, elem) : 0, 2501603128 q meta, meta && parent ? parent->get_user_name(meta->user_id) : 0); 254Opaque_Geometry* new_opaque_geometry(const std::vector< Quad_Coord >& geometry) 25621207735 q bool is_complete = true; 257355722235 q for (std::vector< Quad_Coord >::const_iterator it = geometry.begin(); it != geometry.end(); ++it) 258334514535 q is_complete &= (it->ll_upper != 0 || it->ll_lower != 0); 26221207735 q std::vector< Point_Double > coords; 263355722235 q for (std::vector< Quad_Coord >::const_iterator it = geometry.begin(); it != geometry.end(); ++it) 264334514535 q coords.push_back(Point_Double(::lat(it->ll_upper, it->ll_lower), ::lon(it->ll_upper, it->ll_lower))); 26521207735 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) 2862580020 q bool is_complete = true; 2872580020 q for (std::vector< std::vector< Quad_Coord > >::const_iterator it = geometry.begin(); 28857461020 q it != geometry.end(); ++it) 29234313220 q else if (it->size() == 1) 2932060215 q is_complete &= ((*it)[0].ll_upper != 0 || (*it)[0].ll_lower != 0); 296914012319 q for (std::vector< Quad_Coord >::const_iterator it2 = it->begin(); it2 != it->end(); ++it2) 297881759319 q is_complete &= (it2->ll_upper != 0 || it2->ll_lower != 0); 3032462920 q Compound_Geometry* cp_geom = new Compound_Geometry(); 3052462920 q for (std::vector< std::vector< Quad_Coord > >::const_iterator it = geometry.begin(); 30633335520 q it != geometry.end(); ++it) 3090 cp_geom->add_component(new Null_Geometry()); 31030872620 q else if (it->size() == 1) 311929815 q cp_geom->add_component(new Point_Geometry( 312929815 q ::lat(it->front().ll_upper, it->front().ll_lower), 313929815 q ::lon(it->front().ll_upper, it->front().ll_lower))); 31629942819 q std::vector< Point_Double > coords; 317859828019 q for (std::vector< Quad_Coord >::const_iterator it2 = it->begin(); it2 != it->end(); ++it2) 318829885219 q coords.push_back(Point_Double( 319829885219 q ::lat(it2->ll_upper, it2->ll_lower), 320829885219 q ::lon(it2->ll_upper, it2->ll_lower))); 32229942819 q cp_geom->add_component(new Linestring_Geometry(coords)); 328117110 q else if (!geometry.empty()) 330117110 q Partial_Relation_Geometry* pr_geom = new Partial_Relation_Geometry(); 332117110 q for (std::vector< std::vector< Quad_Coord > >::const_iterator it = geometry.begin(); 33324125510 q it != geometry.end(); ++it) 33620567810 q pr_geom->add_placeholder(); 3373440610 q else if (it->size() == 1 && ((*it)[0].ll_upper != 0 || (*it)[0].ll_lower != 0)) 3383698 q pr_geom->add_point(Point_Double( 3393698 q ::lat(it->front().ll_upper, it->front().ll_lower), 3403698 q ::lon(it->front().ll_upper, it->front().ll_lower))); 34456371310 q for (std::vector< Quad_Coord >::const_iterator it2 = it->begin(); it2 != it->end(); ++it2) 34652967610 q if (it2->ll_upper != 0 || it2->ll_lower != 0) 34751874110 q pr_geom->add_way_point(Point_Double( 34851874110 q ::lat(it2->ll_upper, it2->ll_lower), 34951874110 q ::lon(it2->ll_upper, it2->ll_lower))); 351109353 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) 36821097533 q delete current_geometry; 36921097533 q current_geometry = new_opaque_geometry(way_geometry_store->get_geometry(elem)); 371142350999 q const OSM_Element_Metadata_Skeleton< Way_Skeleton::Id_Type >* meta 372142350999 q = (meta_collector_ways ? meta_collector_ways->get(index, elem.id) : 0); 373142350999 q return Element_With_Context< Way_Skeleton >(&elem, 374142350999 q tag_store_ways ? tag_store_ways->get(index, elem) : 0, 376142350999 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) 38511027 q delete current_geometry; 38611027 q current_geometry = new_opaque_geometry(attic_way_geometry_store->get_geometry(elem)); 38810419133 q const OSM_Element_Metadata_Skeleton< Way_Skeleton::Id_Type >* meta 38910419133 q = (meta_collector_attic_ways ? meta_collector_attic_ways->get(index, elem.id, elem.timestamp) : 0); 39010419133 q return Element_With_Context< Attic< Way_Skeleton > >(&elem, 39110419133 q tag_store_attic_ways ? tag_store_attic_ways->get(index, elem) : 0, 39310419133 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) 4022562718 q delete current_geometry; 4032562718 q current_geometry = new_opaque_geometry(relation_geometry_store->get_geometry(elem)); 40529139559 q const OSM_Element_Metadata_Skeleton< Relation_Skeleton::Id_Type >* meta 40629139559 q = (meta_collector_relations ? meta_collector_relations->get(index, elem.id) : 0); 40729139559 q return Element_With_Context< Relation_Skeleton >(&elem, 40829139559 q tag_store_relations ? tag_store_relations->get(index, elem) : 0, 41029139559 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) 4191734 q delete current_geometry; 4201734 q current_geometry = new_opaque_geometry(attic_relation_geometry_store->get_geometry(elem)); 4221225915 q const OSM_Element_Metadata_Skeleton< Relation_Skeleton::Id_Type >* meta 4231225915 q = (meta_collector_attic_relations ? meta_collector_attic_relations->get(index, elem.id, elem.timestamp) : 0); 4241225915 q return Element_With_Context< Attic< Relation_Skeleton > >(&elem, 4251225915 q tag_store_attic_relations ? tag_store_attic_relations->get(index, elem) : 0, 4271225915 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) 4342208 q return Element_With_Context< Area_Skeleton >(&elem, 4352208 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) 4429799715 q return Element_With_Context< Derived_Skeleton >(&elem, 4439799715 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) 449352213 q : contexts(requested.set_usage.size()), relation_member_roles_(0), users(0) 451788213 q for (std::vector< Set_Usage >::const_iterator it = requested.set_usage.begin(); it != requested.set_usage.end(); ++it) 453436141 q Set_With_Context& context = contexts[std::distance(requested.set_usage.begin(), it)]; 454436141 q context.name = it->set_name; 457436141 q const Set* input = rman.get_set(context.name); 459413138 q context.prefetch(it->usage, *input, stmt, rman); 460436141 q if (it->usage & Set_Usage::SET_KEY_VALUES) 461323 q context.set_key_values = rman.get_set_key_values(context.name); 464352213 q if (requested.role_names_requested) 4653331 q relation_member_roles_ = &relation_member_roles(*rman.get_transaction()); 467352213 q if (requested.user_names_requested) 472Set_With_Context* Prepare_Task_Context::get_set(const std::string& set_name) 474437141 q for (uint i = 0; i < contexts.size(); ++i) 476437141 q if (contexts[i].name == set_name) 483uint32 Prepare_Task_Context::get_role_id(const std::string& role) const 4853530 q if (!relation_member_roles_) 4860 return std::numeric_limits< uint32 >::max(); 4883530 q for (std::map< uint32, std::string >::const_iterator it = relation_member_roles_->begin(); 489371330 q it != relation_member_roles_->end(); ++it) 491371330 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 50228105131 q std::map< uint32, std::string >::const_iterator it = users->find(user_id); 50328105131 q if (it == users->end())